SSH登陆时,怎样才能无需使用密码

由于尝试调整VeryCD的服务器集群架构,需要让各个服务器间通过ssh来自动同步数据。因为所有的操作都要在脚本中自动执行,所以必须要让脚本执行时不需要输入密码就能登陆SSH。

请教了“曾浩峰”后,掌握了建立ssh的公私密钥来实现此功能的方法:

首先建立本机的公私密钥

CODE

[root@host1 root]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
9b:40:4c:a1:9c:d0:10:d6:bf:1b:5f:0d:8c:6b:0c:0a root@host1
[root@host1 root]#


注意不要输入passphrase

然后将公钥文件传送到需要登陆的服务器

CODE

[root@host1 root]# scp .ssh/id_dsa.pub root@host2:id_dsa.pub
root@host2.verycd.com’s password:
id_dsa.pub ? ? ? ? ? 100% |*****************************| ? 600 ? ? ? 00:00

登陆那台服务器

CODE
[root@host1 root]# ssh -v root@host2

将刚才的公钥文件内容添加到用户的.ssh目录下的authorized_keys文件中

CODE

[root@host2 root]# cat id_dsa.pub >> /root/.ssh/authorized_keys

然后更改该文件权限

CODE

[root@host2 root]# chmod 600 /root/.ssh/authorized_keys

退出

CODE

[root@host2 root]# exit
Connection to host2 closed.
[root@host1 root]#

再次登陆即不再需要密码了

CODE
[root@host1 root]# ssh host2
Last login: Wed Nov 17 17:55:34 2004 from 61.129.xxx.xxx
[root@host2 root]#

一条评论 发表在“SSH登陆时,怎样才能无需使用密码”上