模拟情况,A机器想要免密码用ssh登录B机器:
A机器ip:192.168.0.100 A机器ip:192.168.0.223
1、登陆192.168.0.100,生成A机器的公私钥匙对:
用这条命令生成:
ssh-keygen -b 1024 -t rsa
结果如下
Generating public/private rsa key pair.[root@localhost ~]# ssh-keygen -b 1024 -t rsa
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
40:44:8d:2c:20:bd:c0:1b:b2:01:9f:e8:f9:bb:db:67 root@localhost.localdomain
可以看到公钥和私钥都生成在了.shh目录下,公钥为id_rsa.pub,私钥为id_rsa,路径在/root/.ssh/下面。
2、进入B机器,拷贝A机器的公钥到B机器的.shh目录,并加入到authorized_keys文件中
用这个命令:
输入密码,完成拷贝。scp root@192.168.0.100:/root/.ssh/id_rsa.pub /root/.ssh
追加A机器的公钥到B机器的认证文件中:
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
OK,这个A机器到B机器的信任关系就建立好了。
你可以在A机器用SSH命令直接登录到B机器了。