最新消息:阿啰哈,本人90后,目前单身,欢迎妹子们来撩!.(。→‿←。) 微信:frank01991

增加ssh无密码信任连接的安全性

Security 林志斌 656浏览

为了方便系统管理或者服务器运维自动化,我们通常要在服务器间做ssh无密码信任连接。

环境:
目标主机    centos7    192.168.150.110
操作主机    centos7-cn 192.168.150.76    
第三主机    centos7-en 192.168.150.81

一、我们经常是这么做的
网上的教程大多数是这样的。
在操作主机上,创建密钥:

[[email protected] ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is: 27:85:a5:ef:51:a0:61:0b:f6:9c:64:b1:76:66:0b:24
[email protected]
The key's randomart image is: +--[ RSA 2048]----+
|      E B.o      |
|     . X X .     |
|        % = .    |
|       . B o     |
|        S =      |
|         + .     |
|          .      |
|                 |
|                 |
+-----------------+
[[email protected] ~]# ls .ssh
id_rsa  id_rsa.pub  known_hosts
[[email protected] ~]#

id_rsa是私钥,id_rsa.pub是公钥。复制公钥到目标服务器,然后就可以无密码登录了:

[[email protected] ~]# ssh-copy-id [email protected] The authenticity of host '192.168.150.110 (192.168.150.110)' can't be established. ECDSA key fingerprint is 99:e2:ca:93:ac:01:fc:df:e9:87:73:ec:0e:98:bb:77.
Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:  Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]# ssh [email protected] Last login: Tue Oct 28 11:41:12 2014 from centos7-cn
[[email protected] ~]# ip a show enp0s3 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe15:35d2/64 scope link
valid_lft forever preferred_lft forever

这固然方便,但是网上教程中在ssh-keygen创建密钥的时候,
“Enter passphrase (empty for no passphrase):”
和下一行
“Enter same passphrase again:”
两处都是直接回车,就是说没有创建口令短语(passphrase)。

那么问题来了,学挖掘机吗?😉,如果你把私钥文件 ~/.ssh/id_rsa 复制到其他Linux主机上,会怎么样呢?做一下试试。

在第三主机上,注意用户并不是root:

[[email protected] ~]$ mkdir .ssh
[[email protected] ~]$ scp [email protected]:~/.ssh/id_rsa ~/.ssh
[email protected]'s password:
id_rsa                                                                                100% 1679     1.6KB/s   00:00
[[email protected] ~]$ ssh [email protected]
Last login: Tue Oct 28 11:43:04 2014 from centos7-cn

已经顺利的进入了。
就是说呢,如果操作主机上没有口令短语的id_rsa文件被别人获得,你的服务器基本就是人家的了。

二、使用口令短语
我们把目标主机的 /root/.ssh/authorized_keys 移走,在操作主机上重新生成一对儿密钥,这回加上口令短语(至少5个字符),再ssh-copy-id 到目标主机,试试连接:

[[email protected] ~]# ssh [email protected]
Enter passphrase for key '/root/.ssh/id_rsa': <输入正确的口令短语>
Last login: Tue Oct 28 11:46:56 2014 from 192.168.150.76

必须输入正确的口令短语才能登录目标主机。

在第三主机上,把原来的id_rsa文件备份一下,把带有口令短语的id_rsa文件复制过来:

[[email protected] ~]$ cp .ssh/id_rsa /tmp
[[email protected] ~]$ scp [email protected]:~/.ssh/id_rsa ~/.ssh
[email protected]'s password:
id_rsa                                                                                100% 1766     1.7KB/s   00:00
[[email protected] ~]$ ssh [email protected]
Enter passphrase for key '/home/opuser/.ssh/id_rsa': <输入错误的口令短语>
Enter passphrase for key '/home/opuser/.ssh/id_rsa': <输入错误的口令短语>
Enter passphrase for key '/home/opuser/.ssh/id_rsa': <输入正确的口令短语>
Last login: Tue Oct 28 11:48:18 2014 from 192.168.150.76

依然是只有输入正确的口令短语才能连接目标主机。
需要注意一点,第三主机仅仅复制了id_rsa,是不能做ssh-copy-id的,因为还没复制id_rsa.pub。
比较两个id_rsa文件,除了密钥本身不同了,加口令后的文件还多了三行(含一个空行):

Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,395D869B2463C1038A189E373CEB0C43

删除这三行并不能去除口令,呵呵;同时正确的口令也失效了,呵呵呵呵;而且呢,ssh-keygen的man page说了,如果口令短语忘了是没有办法找回的,只能重新生成密钥。


三、增加口令短语
那么在生产环境里,已经部署了不带口令短语的密钥,怎么增加口令短语呢?这样:

[[email protected] ~]# ssh-keygen -p Enter file in which the key is (/root/.ssh/id_rsa):
Key has comment '/root/.ssh/id_rsa' Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

这时候再试试连接目标主机,除了需要输入口令短语,登录服务器依旧不需要密码。
但是仅仅增加口令短语并不能解决问题,因为改动的是操作主机上的id_rsa文件(私钥),目标主机上保存的是毫无变化的公钥,原来未加口令的私钥依然有效!!

那么在操作主机上重新ssh-copy-id呢?试试:

[[email protected] ~]# ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
Enter passphrase for key '/root/.ssh/id_rsa': /usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.

首先,需要输入口令短语;其次,提示说的明明白白,你的公钥已经在远程服务器上了,不会再次复制。
所以没有办法了,增加口令没有太多实际意义。必须删除目标主机原有的公钥(保存在目标主机的 /root/.ssh/authorized_keys),删除操作主机旧的密钥并重新生成一套带口令的,再ssh-copy-id到目标主机。这可能是个浩大的工程。。。

ssh-keygen -p 选项也可以修改口令短语,只是在输入新口令之前需要先输入旧口令。

四、让系统记住口令短语
那么现在又一个问题来了,加了口令短语,私钥安全了,但是登录麻烦了,自动化运维也不可能了。怎么办?
我们可以用ssh-agent(ssh代理守护进程)。

启动代理守护进程:

[[email protected] ~]# eval `ssh-agent` Agent pid 11844

将私钥添加到代理守护进程:

[[email protected] ~]# ssh-add Enter passphrase for /root/.ssh/id_rsa:         【输入口令短语】
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

试试连接目标主机:

[[email protected] ~]# ssh [email protected] Last login: Wed Oct 29 08:30:41 2014 from 192.168.150.100

OK了!

其他需要知道的:
列出代理守护进程保存的私钥:

[[email protected] ~]# ssh-add -l 2048 5f:10:15:3a:ac:61:01:79:29:ac:8c:d7:f0:84:c3:89 /root/.ssh/id_rsa (RSA)
[[email protected]-cn ~]# 

删除代理守护进程保存的私钥:

[[email protected] ~]# ssh-add -D All identities removed.
[[email protected]-cn ~]#

再试试连接目标主机:

[[email protected] ~]# ssh [email protected]192.168.150.110 Enter passphrase for key '/root/.ssh/id_rsa':

这时候又需要口令短语了。
 
参考:http://docs.oracle.com/cd/E26926_01/html/E25889/sshuser-15.html

五、顺便说说eval
这个bash内部指令非常有意思,它是将后面的 `` 符号(键盘左上角跟~符一起的那个,不是单引号哈!)内的指令执行之后,把输出结果再执行一遍,比如上文的 eval `ssh-agent`。

先看看 ssh-agent 单独执行结果:

[[email protected] ~]# ssh-agent SSH_AUTH_SOCK=/tmp/ssh-CDZB3GtAT0MT/agent.11758; export SSH_AUTH_SOCK;
SSH_AGENT_PID=11759; export SSH_AGENT_PID; echo Agent pid 11759;
[[email protected]-cn ~]#

eval `ssh-agent` 就是将ssh-agent的输出结果再执行一次,相当于:

[[email protected] ~]# SSH_AUTH_SOCK=/tmp/ssh-CDZB3GtAT0MT/agent.11758; export SSH_AUTH_SOCK;
[[email protected]-cn ~]# SSH_AGENT_PID=11759; export SSH_AGENT_PID;
[[email protected]-cn ~]# echo Agent pid 11759;

所以 eval `ssh-agent` 的执行结果就是:
后台运行ssh-agent,并且在当前会话输出两个环境变量SSH_AUTH_SOCK、SSH_AGENT_PID,然后再显示 Agent pid 11759 。

我们试一下:

[[email protected] ~]# eval `ssh-agent` Agent pid 11877
[[email protected] ~]# echo $SSH_AUTH_SOCK /tmp/ssh-2Aq37RrIkeOH/agent.11876
[[email protected] ~]# echo $SSH_AGENT_PID 11877

注意,这里得到的Pid跟单独执行的ssh-agent不同了,pgrep ssh-agent 会看到两个进程号:

[[email protected] ~]# pgrep ssh-agent 11759 11877

还要注意,退出当前会话并不会杀死ssh-agent进程。手工杀死进程除了上述的 pgrep 指令,还有ssh-agent -k 可以。试试:

[[email protected] ~]# ssh-agent SSH_AUTH_SOCK=/tmp/ssh-gm8UdqqlTXeb/agent.14140; export SSH_AUTH_SOCK;
SSH_AGENT_PID=14141; export SSH_AGENT_PID; echo Agent pid 14141;
[[email protected] ~]# ssh-agent -k SSH_AGENT_PID not set, cannot kill agent

找不到SSH_AGENT_PID环境变量,这个指令选项无效。那么手工输出一下吧:

[[email protected] ~]# SSH_AGENT_PID=14141; export SSH_AGENT_PID; [[email protected] ~]#
[[email protected] ~]# ssh-agent -k unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID; echo Agent pid 14141 killed;

这回可以了。所以 ssh-agent 命令最好还是用 eval `ssh-agent` 执行更方便,但是要记住不能重复执行,ssh-agent -k 只负责最后一个进程,道理呢?参考ssh-agent -k指令输出,自己琢磨一下吧。

via: http://www.cnblogs.com/panblack/p/Secure_ssh_trust_connection.html

转载请注明:林志斌 » 增加ssh无密码信任连接的安全性

发表评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址