ssh密钥管理和认证代理

转贴地址:http://blog.chinaunix.net/u/19895/showart_129077.html

ssh密钥管理和认证代理
ssh允许用户把密钥存储在内存中,这就是ssh认证代理。认证代理为用户提供了使用RSA密钥而不必随时键入口令字的能力。这对于不必在所有登录、X会话或运行脚本时都要键入口令字提供便利是很有效的。

(一)ssh认证登录是ssh-agent。执行这个命令可以使认证代理运行,但是它没有在内存中增加任何密钥。密钥是由ssh-add命令增加的。
[root@localhost ~]# ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-CmeOr30022/agent.30022; export SSH_AUTH_SOCK;
SSH_AGENT_PID=30023; export SSH_AGENT_PID;
echo Agent pid 30023;
[root@localhost ~]# ps aux|grep ssh
root      2019  0.0  0.3   4388  1728 ?        Ss   Jun01   0:01 /usr/sbin/sshd
root     15483  0.0  0.4   7412  2324 ?        Ss   Jun16   0:20 sshd:
root@notty
root     25387  0.0  0.5   7380  2628 ?        Ss   Jun16   0:00 sshd:
root@pts/0
root     30023  0.0  0.2   3868  1128 ?        Ss   01:05   0:00 ssh-agent
root     30053  0.0  0.1   3756   700 pts/0    R+   01:06   0:00 grep ssh
这样就启动了ssh认证代理。
认证代理产生UNIX套接字,该套接字被存放在/tmp/ssh-username/agent-socket-processID
中。套接字名定位在环境变量SSH_AUTH_SOCK中。Secure Shell为维护认证代理安全性所做
的一件事是使它只能被用户自身访问。然而,超级用户可以访问它,并且如果同一个用户启
动另外的ssh-agent进程,这可能产生问题。
注意记住,运行ssh-agent将不会把你的密钥载入内存。你必须用ssh-add命令自己把密
钥载入内存。

(二)接下来我们先生成登录到远程机器的密钥对
[root@localhost ~]# ssh-keygen -t dsa -f ~/.ssh/id_dsa10.4.5.29
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa10.4.5.29.
Your public key has been saved in /root/.ssh/id_dsa10.4.5.29.pub.
The key fingerprint is:
ed:b1:b1:5b:d9:6e:8a:01:71:5c:0b:ec:b9:89:51:f1
root@localhost.localdomain
[root@localhost ~]#
-t   指定生成的密钥对类型
-f   指定生成的密钥文件的位置和名称,本例中我想把这个密钥对只用于10.4.5.29这台计算机,所以取了id_dsa10.4.5.29这个名字。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
这两行要求我们输入密钥口令字,要输入两次。
这个命令执行成功后,会在~/.ssh/目录下生成两个文件id_dsa10.4.5.29和id_dsa10.4.5.29.pub两个文件。
然后,我们把id_dsa10.4.5.29.pub这个文件先传输到10.4.5.29这台计算机上,当然这时是要我们输入用户密码的。之后登录10.4.5.29计算机,
[root@server root]# cat id_dsa10.4.5.29.pub >> ~/.ssh/authorized_keys
现在我们重新登录10.4.5.29
[root@localhost ~]# ssh -i ~/.ssh/id_dsa10.4.5.29 10.4.5.29
Enter passphrase for key '/root/.ssh/id_dsa10.4.5.29':
Last login: Sat Jun 17 01:06:08 2006 from 10.4.5.161
[root@server root]#
-i   指定我们要使用的私钥文件。
然后我们要输入密钥口令字,才可登录到10.4.5.29这台远程计算机。这样是很不方便的。

(三)把密钥加入ssh-agent
当你已经用代理运行时,需要把R S A密钥加到代理中。要把密钥加到ssh-agent,你需要
运行ssh-add。它把私有RSA密钥加到ssh-agent中,并且允许通过Secure Shell连接转发密钥。
这意味着在加入密钥和ssh-agent运行后,你只需要键入一次口令字来使它运行。
要加入你的身份密钥,你需要做的只是运行ssh-add :
[root@localhost ~]# ssh-add ~/.ssh/id_dsa10.4.5.29
Enter passphrase for /root/.ssh/id_dsa10.4.5.29:
Identity added: /root/.ssh/id_dsa10.4.5.29 (/root/.ssh/id_dsa10.4.5.29)
[root@localhost ~]#
把id_dsa10.4.5.29这个密钥加入到ssh-agent代理中,我们在这里还需要输入一次密钥口令字。
这时我们再次登录10.4.5.29远程计算机:
[root@localhost ~]# ssh -i ~/.ssh/id_dsa10.4.5.29 10.4.5.29
Last login: Sat Jun 17 01:35:00 2006 from 10.4.5.161
[root@server root]#
这次我们就不需要再输入密钥口令字了,可以自由的登录远程计算机。

(四)相关命令
>>如果你要列出所有存储在认证代理中的当前身份,则使用- I选项:
[root@localhost ~]# ssh-add -l
1024 81:71:25:91:30:5b:f3:7a:ba:f0:a9:56:2d:51:fd:e4 /root/.ssh/id_dsa10.4.5.29 (DSA)
[root@localhost ~]#
>>你可以把多重身份存储在s s h - a g e n t中。你也可以使用s s h - a d d把身份从Secure Shell代理中移去。要移去一个身份,使用后面跟着身份文件名的- d选项:
[root@localhost ~]# ssh-add -d /root/.ssh/id_dsa10.4.5.29
Identity removed: /root/.ssh/id_dsa10.4.5.29 (/root/.ssh/id_dsa10.4.5.29.pub)
[root@localhost ~]# ssh-add -l
The agent has no identities.
>>要移去所有的身份,使用- D选项:
[root@localhost ~]# ssh-add -D
这意味着你不必逐一登录到每台远程主机上以移去身份—你可以在本地完成该工作。
>>你可以很容易地移去ssh-agent。你也可以在从X会话退出时杀死ssh-agent进程或通过杀死SSH_AGENT_PID来终止ssh-agent。
[root@localhost ~]#kill -9 $SSH_AGENT_PID

[root@localhost ~]#ssh-agent -k

posted @ 2008-10-30 17:28  beta2013  阅读(261)  评论(0编辑  收藏  举报