场景:服务器A 采用ssh 登录服务器B,没有任何特殊设置情况下,采用ssh host.b 会出现提示Password: 让输入密码。如何可以不手工输入密码?
解决方案:
- 生成ssh公钥和私钥
- [qingxu@login1.cm3 .ssh]$ ssh-keygen -t dsa
- Generating public/private dsa key pair.
- Enter file in which to save the key (/home/qingxu/.ssh/id_dsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /home/qingxu/.ssh/id_dsa.
- Your public key has been saved in /home/qingxu/.ssh/id_dsa.pub.
- The key fingerprint is:
- ba:43:5b:8e:80:50:5b:88:f1:01:99:1a:c8:73:68:ab qingxu@login1.cm3
将公钥copy到要登录的机器B上去,并在B的.ssh目录下,然后将id_dsa.pub的内容追加到authorized_keys文件中。
- cat id_dsa.pub >> authorized_keys
此时我们可以通过ssh host.b登录B服务器了,不会提醒Password了,不过。这个时候你仍然会看到这样的信息。
- [qingxu@login1.cm3 ~]$ ssh host.b
- The authenticity of host 'host.b (xxxxxx)' can't be established.
- DSA key fingerprint is b9:d9:d6:69:c0:e5:bd:6d:c8:89:43:8a:a5:d6:ef:a4.
- Are you sure you want to continue connecting (yes/no)?
接着会出现以下信息:
- Enter passphrase for key '/home/qingxu/.ssh/id_dsa':
- 使用ssh-agent和ssh-add管理密钥
ssh-agent是用于管理密钥,ssh-add用于将密钥加入到ssh-agent中,SSH可以和ssh-agent通信获取密钥,这样就不需要用户手工输入密码了。
eval `ssh-agent` ssh-add Enter passphrase for /home/qingxu/.ssh/id_dsa:
不过由于每次登录都需要设置一次,所以最好将命令放到~/.bash_profile中。
另外,可以采用keychain来处理这一步,参考:
http://www.ibm.com/developerworks/cn/linux/security/openssh/part1/index.html
http://www.ibm.com/developerworks/cn/linux/security/openssh/part2/