`

ssh-agent和keychain

 
阅读更多

一、问题的由来:


在上一章中的ssh密钥登陆的状况下:
user1---------|                                      |-----------server1

                    |                                      |
user2---------|                                      |-----------server2
                     |------->B(跳板机)------>|
user3---------|                                      |-----------server3
                    |                                      |
...    -----------|                                      | ----------...


这种情况下,B(跳板机)去登陆每一个server都会提示输入密码才能获取解密的专用密钥。那么可不可以只输入一次,而不必每次都输入呢?


二、知识简介:

下面我介绍ssh-agent(专用密钥高速缓存)以及keychain。
ssh-agent是专为既令人愉快又安全的处理RSA和DSA密钥而设计的特殊程序。它包含在OpenSSH分发内,不同于ssh,ssh-agent是个长时间持续运行的守护进程(daemon),设计它的唯一目的就是对解密的汉族昂用密钥进行高速缓存。
ssh 包含的内建支持允许它同ssh-agent通信,允许ssh不必每次新连接时都提示您要密码才能获取解密的专用密钥。对于ssh-agent,您只要使用 ssh-add把专用密钥添加到ssh-agent的高速缓存中。这是个一次性的过程;用ssh-add之后,ssh将从ssh-agent获取您的专用 密钥,而不会提示要密码短语来麻烦您了。

我们看一下ssh-agent密钥缓存系统的工作过程。ssh-agent启动时,在脱离shell(外壳程序)并继续在后台运行之前它会输出一些重要的环境变量。

启 动 ssh-agent 的最佳方式就是把上面这行添加到您的 ~/.bash_profile 中;这样,在您的登录 shell 中启动的所有程序都将看到环境变量,而且能够定位 ssh-agent ,并在需要的时候向其查询密钥。尤其重要的环境变量是 SSH_AUTH_SOCK;SSH_AUTH_SOCK 包含有 ssh 和 scp 可以用来同 ssh-agent 建立对话的 UNIX 域套接字的路径。

使用ssh-add:
ssh-agent 启动时高速缓存当然是空的,里面不会有解密的专用密钥。在我们真能使用 ssh-agent 之前,首先还需要使用 ssh-add 命令把我们的专用密钥添加到 ssh-agent 的高速缓存中。下面的示例中,我使用 ssh-add 把我的 ~/.ssh/identity 专用 RSA 密钥添加到 ssh-agent 的高速缓存中:

使用keychain:
keychain 是一个有用的 ssh-agent 前端,它基于 bash,叫做 keychain 。 keychain 的特别之处在于它允许 每个系统使用一个 ssh-agent 进程,而非每次登录会话。这意味着您只需对每个专用密钥执行一次 ssh-add ,就一次。正如我们稍后将要看到的一样, keychain 甚至有助于优化 ssh-add ,而这只要它试图向那些正在运行的 ssh-agent 添加其高速缓存中没有的专用密钥。

keychain 如何工作从头到尾浏览一遍。从 ~/.bash_profile 中启动时, keychain 将首先查看 ssh-agent 是否已经在运行了。如果没有,它就启动 ssh-agent 并把重要的 SSH_AUTH_SOCK 和 SSH_AGENT_PID 变量记录在 ~/.ssh-agent 文件中,一方面为了安全而保存,另一方面也是为了以后的使用。这是启动 keychain 的最佳途径;同使用平淡无奇的老式 ssh-agent 一样,我们在 ~/.bash_profile 内部执行必要的配置


三、实施解决方案:

所用的测试机:
10.1.15.155   B
10.1.15.125   A
10.1.15.156   C

1.安装keychain

在B(跳板机)上面安装keychain

tar xjvf keychain-2.6.1.tar.bz2
cd /software/src/keychain-2.6.1
install -m0755 keychain /usr/bin/keychain


2.在B上面生成密钥对:
[root@test126 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/test_rsa #(名字我改变了一下,没有用默认的,为了方便识别)
Enter passphrase (empty for no passphrase): #(这里的密码我用了123456)
Enter same passphrase again:
Your identification has been saved in /root/.ssh/test_rsa.
Your public key has been saved in /root/.ssh/test_rsa.pub.
The key fingerprint is:
e1:45:9b:44:d0:2c:a7:49:a2:bc:38:11:39:a0:09:c1
[root@test126 ~]#

3.在B上面的.bash_profile文件追加内容:
vi /root/.bash_profile(追加下面的内容)
/usr/bin/keychain ~/.ssh/test_rsa
source  ~/.keychain/test126-sh > /dev/null

[root@test126 ~]# source /root/.bash_profile

KeyChain 2.6.1;http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

 * Found existing ssh-agent (28593)
 * Adding 1 ssh key(s)...
Enter passphrase for /root/.ssh/test_rsa:
Identity added: /root/.ssh/test_rsa (/root/.ssh/test_rsa)

4.在A、C上面的处理:
把B上面的test_rsa.pub拷贝到A和C上面
(/root/.ssh/test_rsa.pub)
cat /root/.ssh/test_rsa.pub > authorized_keys

修改/etc/ssh/sshd_config文件:
---------------------------------------------------------
PasswordAuthentication no 修改为no,使用public key登陆。
---------------------------------------------------------
重启sshd 服务 service sshd restart

 

四、测试:

在B上登陆A
[root@test126 ~]# ssh 10.1.15.125
Address 10.1.15.125 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Last login: Tue Sep 13 15:26:03 2011 from 10.1.15.155
[root@test125 ~]#


在B上登陆C
[root@test126 ~]# ssh 10.1.15.156
The authenticity of host '10.1.15.156 (10.1.15.156)' can't be established.
RSA key fingerprint is fc:29:7f:5f:23:1e:78:e3:4c:e9:db:2d:3a:57:2c:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.15.156' (RSA) to the list of known hosts.
Address 10.1.15.156 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Last login: Tue Sep 13 15:54:16 2011 from 10.1.15.129
[root@test156 ~]# logout
Connection to 10.1.15.156 closed.
[root@test126 ~]# ssh 10.1.15.156
Address 10.1.15.156 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Last login: Tue Sep 13 16:04:33 2011 from 10.1.15.155
[root@test156 ~]#

在B中
cat ~/.keychain/test126-sh
--------------------------------------------------------------------------
SSH_AUTH_SOCK=/tmp/ssh-uGitf28592/agent.28592; export SSH_AUTH_SOCK;
SSH_AGENT_PID=28593; export SSH_AGENT_PID;
--------------------------------------------------------------------------

其 中它的内容是自动生成的,并且其内容也一直没有变化(PID)实现了高速缓存,多个session也不用多次载入,只是一个pid负责,并我们也只是在B 中source /root/.bash_profile 的时候输入了一次密码,避免了ssh每次新连接时都提示您要密码才能获取解密的专用密钥的问题。


再次打开一个新的session会有下面的内容(新变化,也可以当做成功的参考):
------------------------------------------------------------------
Last login: Tue Sep 13 15:58:20 2011 from 10.1.15.129

KeyChain 2.6.1;http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

 * Found existing ssh-agent (28593)
 * Known ssh key: /root/.ssh/test_rsa

[root@test126 ~]#
-------------------------------------------------------------------

 

 

可供参考的不错的网址:

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

 

 

.bash_profile:

 

usr/bin/keychain ~/.ssh/id_rsa
source  ~/.keychain/主机名-sh > /dev/null

 

 

之前已经在freebsd主机登陆到dreamhost.com的ssh密钥实验是成功的,参考网址

http://wiki.dreamhost.com/SSH#Passwordless_Login

但今天尝试把密钥放到debian系统的/root/.ssh/id_rsa中,然后在debian的终端界面输入
ssh xxxx@xxxx.dreamhost.com
这样就出现 Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open. 等错误显示了,经google查询下,原来只要把权限降到0600就ok了
输入命令
chmod 0600 /root/.ssh/id_rsa
然后再
ssh xxxx@xxxx.dreamhost.com

这样就可以密钥登陆了.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics