ssh免输入密码登陆

场景:服务器A 采用ssh 登陆服务器B,没有任何特殊设置状况下,采用ssh host.b 会出现提示Password:  让输入密码。如何能够不手工输入密码?html

解决方案:linux

  •   生成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
这里-t dsa表示采用dsa加密方式,回车后会让你输入私钥,最后在.ssh目录下生成两个文件id_dsa和id_dsa.pub,分别表示私钥和公钥。

将公钥copy到要登陆的机器B上去,并在B的.ssh目录下,而后将id_dsa.pub的内容追加到authorized_keys文件中。bash

cat id_dsa.pub >> authorized_keys
此时,B服务器下有两个文件,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)?
会让你输入是否链接b服务器,输入yes,则会在A服务器本地生成一个known_hosts文件,内部是A访问过的服务器,这个文件的做用是:发现B服务器的公钥和本地known_hosts的公钥不一致,就会提醒你是否链接上去。通常输入一次,之后就不会再提示了。

接着会出现如下信息:
ssh

Enter passphrase for key '/home/qingxu/.ssh/id_dsa':
仍然须要你输入私钥。并且之后无论你登录几回,都会提醒你输入私钥,没有达到咱们的要求,怎么办呢?
  • 使用ssh-agent和ssh-add管理密钥

ssh-agent是用于管理密钥,ssh-add用于将密钥加入到ssh-agent中,SSH能够和ssh-agent通讯获取密钥,这样就不须要用户手工输入密码了。ide

eval `ssh-agent`
ssh-add
Enter passphrase for /home/qingxu/.ssh/id_dsa: 

不过因为每次登陆都须要设置一次,因此最好将命令放到~/.bash_profile中。加密

另外,能够采用keychain来处理这一步,参考:spa

http://www.ibm.com/developerworks/cn/linux/security/openssh/part1/index.htmlcode

http://www.ibm.com/developerworks/cn/linux/security/openssh/part2/
htm