如何在主機之間建立SSH信任關係?
伺服器之間建立信任關係,對於監控伺服器管理很必要。這裡介紹ssh的無密碼登陸。
本機ip:192.168.1.6
[[email protected] ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y (因為我是第2次建立關係所以此處覆蓋原來的檔案)
Enter passphrase (empty for no passphrase):(直接回車無須輸入金鑰)
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
04:37:13:2a:4b:10:af:c1:2b:03:3f:6b:27:ce:b9:62 [email protected]
[[email protected] ~]# cd .ssh/
[[email protected] .ssh]# ll
-rw——- 1 root root 883 Apr 25 17:51 id_rsa
-rw-r–r– 1 root root 221 Apr 25 17:51 id_rsa.pub
-rw-r–r– 1 root root 442 Apr 25 17:37 known_hosts
id_rsa是金鑰檔案,id_rsa.pub是公鑰檔案。
[[email protected] .ssh]# scp id_rsa.pub192.168.1.4:/root/.ssh/192.168.1.6
[email protected]’s password:
id_rsa.pub 100% 221 0.2KB/s 00:00
這裡把公鑰檔案取名為本機的ip地址就是為了以後和更多的機器建立信任關係不發生混淆。
現在登陸到192.168.1.4機器
[[email protected] ~]# cd .ssh/
[[email protected] .ssh]# cat 192.168.1.6 >> authorized_keys
然後回到192.168.1.6機器直接
[[email protected] .ssh]# ssh 192.168.1.4
Last login: Wed Aug 8 12:14:42 2007 from 192.168.1.6
這樣就可以了,裡面偶爾涉及到許可權問題。一般./ssh資料夾是755 authorized_keys為600或者644