一、准备工作
- 用客户端工具(ssh client或者putty)连接到linux服务器。在root用户下输入命令
vi /etc/hosts,用vi编辑hosts文件,如下:
#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.160 cdh1
192.168.1.161 cdh2
192.168.1.162 cdh3
192.168.1.163 cdh4
192.168.1.118 cdh5(新增)
192.168.1.117 cdh6(新增)
<放入你所需要得所有机器>
- 对新增机器输入命令:cd .ssh进入rsa公钥私钥文件存放的目录,删除目录下的id_rsa,id_rsa.pub文件。
二、在每台机上产生新的rsa公钥私钥文件,并统一拷贝到一个authorized_keys文件中
- 登录cdh5,在.ssh目录下输入命令:
ssh-keygen -t rsa
,三次回车后,该目录下将会产生id_rsa,id_rsa.pub文件。其他主机也使用该方式产生密钥文件。
新增cdh6也同样操作。
- 登录cdh5,输入命令:
cat id_rsa.pub >> authorized_keys
,将id_rsa.pub公钥内容拷贝到authorized_keys文件中。
- 将新增主机的公钥文件内容都拷贝到cdh1主机上的authorized_keys文件中,命令如下:
将公钥拷贝到目标主机的authorized_keys中,例如:(在所需配置得机器上,目标先把所有机器密钥全部复制到某一台)
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@192.168.76.141(在cdh5上执行操作)
ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop@192.168.76.141(在cdh6上执行操作)
此时cdh1配置了所有需要免密得机器(包括之前已有机器),
最终authorized_keys文件内容如下:
[root@cdh1 .ssh]# more authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp7jp4a1/deK4B/eZW8kdiCQvzpKe4Id0uP0DdONFW85r
20rpH4ZpNSB7JNYFiqjMUPQ/2EWLXh7xoa9UntKsMVVcZr3BTiXRZ0Gx7IDacwz8Cq0qYrwUCMTKt7QF
xK5RwXyuZqDFjc2yC3/ZoqeyeLERFTll3ZLGeFsXhJjlkPTvtnvx0zX6lacPZL4VguoM5YHw3KaJg8bd
qehJzbQoPS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oVxzexTFMOSwNFpTMsLc7Oza
Q3XG6v6PgtBEJqdUdl4zIFAZnkZjKmZp8SWG1jGo/vl3jsbrnmFQFeH04MdVcm9r1wvl80bdF9YvYEtg
ymHO4/tANKWSNKGw2EcthNWyQiN4hIYOoBQaUA3FlAXyqcD2+Q+2frf0gNF5KfKKLXmmlAuS9Oi6JqFJ
f1BLmMquI4n02owzU0zlkj3Px5IcCfQbDQ/JedHcjxEKP2S+9m3yxG4h2Q== hadoop@cdh4
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyS1PbXTgFqHcGV/k0QUZni5xB5+q71qx4r5xto+WLkVQ
MiS3gR/VYZoyDLrQRVf5t0JiliCgHeU0MG/ASd1LPbsVYCX5V9cmcm1hMByDKJ6FwIxz5cY4y1dBpwAZ
2E24CQWfZuevaeIFnL80RGGLhRgCxbLb3aPvRZuV34utD+5kgAFJwYCQYN4KnFG1S5e4X6U5J3FsCDby
GD7dhtWWktMV2SAnWjrmULguaVRqO0sDIEiVSc9+KKqAmlOab+5SbZAI5+3hzJog5c3i4f5fHXWnxYqy
LNo3yYSWraV5KcYIlNPqX6iJKvjLWbNDwD3+70u4nWjBSXXufhff71LdHw== hadoop@cdh5
三、授权authorized_keys文件
- 登录cdh1,在.ssh目录下输入命令:
chmod 600 authorized_keys
,效果如下:
[root@cdh1 .ssh]# ls -al
total 28
drwx------. 2 root root 4096 Oct 24 12:27 .
dr-xr-x---. 22 root root 4096 Oct 24 19:20 ..
-rw-------. 1 root root 1975 Oct 24 12:27 authorized_keys
-rw-------. 1 root root 1975 Oct 24 12:09 authorized_keys_20141024
-rw-------. 1 root root 1675 Oct 24 12:23 id_rsa
-rw-r--r--. 1 root root 395 Oct 24 12:23 id_rsa.pub
-rw-r--r--. 1 root root 2020 Oct 24 12:10 known_hosts
四、将授权文件分配到其他主机上
1)此时cdh1上配置了所有机器得密钥, 登录cdh1,将授权文件拷贝到cdh2、cdh3…,(所有机器)命令如下:
进入当前authorized_keys目录:cd .ssh
scp authorized_keys hadoop@cdh2:/home/hadoop(用户名)/.ssh/ #拷贝到cdh2上
scp authorized_keys hadoop@cdh3:/home/hadoop(用户名)/.ssh/ #拷贝到cdh3上
scp authorized_keys hadoop@cdh4:/home/hadoop(用户名)/.ssh/ #拷贝到cdh4上
scp authorized_keys hadoop@cdh5:/home/hadoop(用户名)/.ssh/ #拷贝到cdh5上
scp authorized_keys hadoop@cdh6:/home/hadoop(用户名)/.ssh/ #拷贝到cdh6上
2)所有机器得/etc/hosts下得主机名配置也得复制好
至此,免密码登录已经设定完成,注意第一次ssh登录时需要输入密码,再次访问时即可免密码登录