ssh多臺服務器互相配置免密登錄
假設有如下四臺服務器
主機名 | IP地址 | 免密碼用戶名 |
---|---|---|
node01 | 192.168.8.1 | root |
node02 | 192.168.8.2 | root |
node03 | 192.168.8.3 | root |
node04 | 192.168.8.4 | root |
一伙单、確認sshd配置
查看每一臺服務器的 /etc/ssh/sshd_config文件,確認AuthorizedKeysFile一行配置沒有被注釋掉:
# but this is overridden so installations willcheck .ssh/authorized keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none .....
二、修改hosts配置主機名(可選)
用root賬號登錄服務器兑障,修改/etc/hosts文件循集,hosts文件末尾添加:
192.168.8.1 node01
192.168.8.2 node02
192.168.8.3 node03
192.168.8.4 node04
三荣瑟、在每一臺服務器上生成rsa秘鑰
分別登錄四臺服務器執(zhí)行以下命令世剖。并且連續(xù)按三次回車鍵(生成一個無密碼的rsa秘鑰)杆怕,
ssh-keygen -t rsa
在每一臺服務期下间聊,會生成兩個密鑰文件攒盈,密鑰會生成在~/.ssh目錄下
id_rsa #私鑰
id_rsa.pub #公鑰
四、依次將公鑰寫入認證文件
首先登錄 node01(192.168.8.1),執(zhí)行以下命令,將本臺機器的ras公鑰(追加)寫入~/.ssh/authorized_keys文件中哎榴,然后傳輸?shù)絥ode02服務器型豁,第一次執(zhí)行scp命名都需要輸入密碼
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node02:~/.ssh/authorized_keys
再登錄 node02(192.168.8.2),將node02服務器的ras公鑰(追加)寫入~/.ssh/authorized_keys文件中 然后傳輸?shù)絥ode03服務器.
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node03:~/.ssh/authorized_keys
再登錄 node03(192.168.8.3),執(zhí)行同樣的追加操作
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node04:~/.ssh/authorized_keys
登錄 node04(192.168.8.4),執(zhí)行追加操作僵蛛,然后把~/.ssh/authorized_keys文件分別傳入node01、node02迎变、node03
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node01:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node02:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys root@node03:~/.ssh/authorized_keys
五充尉、驗證
ssh -T node01
#ssh -T 192.168.8.1
ssh node01
#ssh 192.168.8.1