现在的位置: 首页 > 综合 > 正文

ssh免输入密码登录

2013年10月12日 ⁄ 综合 ⁄ 共 1560字 ⁄ 字号 评论关闭

场景:服务器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

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

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

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的公钥不一致,就会提醒你是否连接上去。一般输入一次,以后就不会再提示了。

接着会出现以下信息:

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/

抱歉!评论已关闭.