集羣環境ssh免密碼登陸設置

1、準備工做

1) 用客戶端工具(ssh client或者putty)鏈接到linux服務器。在root用戶下輸入命令linux

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 hadoop01

192.168.1.161 hadoop02

192.168.1.162 hadoop03

192.168.1.163 hadoop04

192.168.1.164 hadoop05
複製代碼

2) 輸入命令:cd .ssh進入rsa公鑰私鑰文件存放的目錄,刪除目錄下的id_rsa,id_rsa.pub文件。dom

2、在每臺機上產生新的rsa公鑰私鑰文件,並統一拷貝到一個authorized_keys文件中

1) 登陸hadoop01,在.ssh目錄下輸入命令:ssh

ssh-keygen -t rsa

,三次回車後,該目錄下將會產生id_rsa,id_rsa.pub文件。其餘主機也使用該方式產生密鑰文件。工具

2) 登陸hadoop01,輸入命令:oop

cat id_rsa.pub >> authorized_keys

,將id_rsa.pub公鑰內容拷貝到authorized_keys文件中。ui

3) 登陸其餘主機,將其餘主機的公鑰文件內容都拷貝到hadoop01主機上的authorized_keys文件中,命令以下:spa

複製代碼
ssh-copy-id -i hadoop01 #登陸hadoop02,將公鑰拷貝到hadoop01的authorized_keys中

ssh-copy-id -i hadoop01#登陸hadoop03,將公鑰拷貝到hadoop01的authorized_keys中

ssh-copy-id -i hadoop01#登陸hadoop04,將公鑰拷貝到hadoop01的authorized_keys中

ssh-copy-id -i hadoop01 #登陸hadoop05,將公鑰拷貝到hadoop01的authorized_keys中,最終authorized_keys文件內容以下:
複製代碼
複製代碼
[root@hadoop01 .ssh]# more authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAp7jp4a1/deK4B/eZW8kdiCQvzpKe4Id0uP0DdONFW85r

20rpH4ZpNSB7JNYFiqjMUPQ/2EWLXh7xoa9UntKsMVVcZr3BTiXRZ0Gx7IDacwz8Cq0qYrwUCMTKt7QF

xK5RwXyuZqDFjc2yC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Gf8qLPsFY+

/KluWl0TrRLwBVHGc94gpN2JOqtvKYYDSHw4Fj6KPSk/6Nt8CutZAwASoVxzexTFMOSwNFpTMsLc7Oza

Q3XG6v6PgtBEJqdUdl4zIFAZnkZjKmZp8SWG1jGo/vl3jsbrnmFQFeH04MdVcm9r1wvl80bdF9YvYEtg

ymHO4/tANKWSNKGw2EcthNWyQiN4hIYOoBQaUA3FlAXyqcD2+Q+2frf0gNF5KfKKLXmmlAuS9Oi6JqFJ

f1BLmMquI4n02owzU0zlkj3Px5IcCfQbDQ/JedHcjxEKP2S+9m3yxG4h2Q== root@hadoop05

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyS1PbXTgFqHcGV/k0QUZni5xB5+q71qx4r5xto+WLkVQ

MiS3gR/VYZoyDLrQRVf5t0JiliCgHeU0MG/ASd1LPbsVYCX5V9cmcm1hMByDKJ6FwIxz5cY4y1dBpwAZ

2E24CQWfZuevaeIFnL80RGGLhRgCxbLb3aPvRZuV34utD+5kgAFJwYCQYN4KnFG1S5e4X6U5J3FsCDby

GD7dhtWWktMV2SAnWjrmULguaVRqO0sDIEiVSc9+KKqAmlOab+5SbZAI5+3hzJog5c3i4f5fHXWnxYqy

LNo3yYSWraV5KcYIlNPqX6iJKvjLWbNDwD3+70u4nWjBSXXufhff71LdHw== root@hadoop01
複製代碼
3、受權authorized_keys文件

1) 登陸hadoop01,在.ssh目錄下輸入命令:3d

chmod 600 authorized_keys

,效果以下:code

複製代碼
[root@hadoop01 .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
複製代碼
4、將受權文件分配到其餘主機上

1) 登陸hadoop01,將受權文件拷貝到hadoop0二、hadoop03...,命令以下:

複製代碼
scp /root/.ssh/authorized_keys hadoop02:/root/.ssh/ #拷貝到hadoop02上

scp /root/.ssh/authorized_keys hadoop03:/root/.ssh/ #拷貝到hadoop03上

scp /root/.ssh/authorized_keys hadoop04:/root/.ssh/ #拷貝到hadoop04上

scp /root/.ssh/authorized_keys hadoop05:/root/.ssh/ #拷貝到hadoop05上
複製代碼

2) 至此,免密碼登陸已經設定完成,注意第一次ssh登陸時須要輸入密碼,再次訪問時便可免密碼登陸。

相關文章
相關標籤/搜索