4台机器做为实例,机器的用户名都是hadoop,ip地址分别是:
192.168.8.231 nn1.hadoop
192.168.8.232 nn2.hadoop
192.168.8.233 dn1.hadoop
192.168.8.234 dn2.hadoop
192.168.8.235 dn3.hadoop
**
干货(不想看解释可直接拷贝命令)
分别在每台机器上执行如下命令:
ssh-keygen
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@nn1.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@nn2.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn1.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn2.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn3.hadoop
配置结束
**
开始解释了
上面的命令其实只有两个步骤,生成密钥文件,拷贝公钥文件到其他机器
1.ssh的安装与配置(每台台机器做同样的设置)
1.1 ssh服务的安装
打开终端,输入:
sudo apt-get install openssh-server
sudo apt-get install openssh-client
程序自动安装。
1.2 ssh服务的配置
关闭防火墙:sudo ufw disable
生成本机密钥文件,执行命令:
ssh-keygen
按提示先yes然后回车回车 就会生成/root/.ssh/id_rsa和/root/.ssh/id_rsa.pub文件
ssh-keygen命令就是生成本机的密钥文件和私钥文件,无密码登录就是将本机的公钥文件发送到远程机器,本机就可以无密码登录到远程机器了。
那么,本机发给远程机器的公钥文件如何存储呢?
存储在远程机器的.ssh目录下authorized_keys文件中(如果不存在可以手工创建)
以ssh登录本机为例,将刚才生成的公钥文件内容放在authorized_keys文件中,实现ssh本机无密码登录,也可以验证刚才安装的ssh服务是否正确:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
这个命令就是把公钥文件id_rsa.pub存放至authorized_keys文件中,也可以使用
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@nn1.hadoop
把本机的公钥文件拷贝到目标机器,并且将内容追加到远程机器的authorized_keys中(因为方便,所以后面的例子都使用ssh-copy-id命令)
验证本机是否可以无密码登录
ssh localhost
.ssh 目录权限要求700 authorized_keys权限要求600
2.无密码登录其他主机(每台服务器都要做)
修改hosts文件:
sudo gedit /etc/hosts
向其他机器复制公钥文件,并合并到authorized_keys文件:
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@nn1.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@nn2.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn1.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn2.hadoop
ssh-copy-id -i /home/hadoop/.ssh/id_rsa.pub hadoop@dn3.hadoop
验证ssh登录
ssh hadoop@nn1.hadoop
ssh hadoop@nn2.hadoop
ssh hadoop@dn1.hadoop
ssh hadoop@dn2.hadoop
ssh hadoop@dn3.hadoop