关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » HTML5 »增加SSH无密码信任连接的安全性

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

    2014-10-29 00:00:00 出处:好大一片云
    分享

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

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

    一、我们经常是这么做的

    网上的教程大多数是这样的。
    在操作主机上,创建密钥:

    [root@centos7-cn ~]# 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 root@centos7-cn
    The key's randomart image is:
    +--[ RSA 2048]----+
    |      E B.o      |
    |     . X X .     |
    |        % = .    |
    |       . B o     |
    |        S =      |
    |         + .     |
    |          .      |
    |                 |
    |                 |
    +-----------------+
    [root@centos7-cn ~]# 
    [root@centos7-cn ~]# ls .ssh
    id_rsa  id_rsa.pub  known_hosts
    [root@centos7-cn ~]#

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

    [root@centos7-cn ~]# ssh-copy-id root@192.168.150.110
    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
    root@192.168.150.110's password:
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'root@192.168.150.110'"
    and check to make sure that only the key(s) you wanted were added.
    
    [root@centos7-cn ~]#
    [root@centos7-cn ~]# ssh root@192.168.150.110
    Last login: Tue Oct 28 11:41:12 2014 from centos7-cn
    [root@centos7 ~]#
    [root@centos7 ~]# 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
    [root@centos7 ~]#

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

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

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

    [opuser@centos7-en ~]$ mkdir .ssh
    [opuser@centos7-en ~]$ scp root@192.168.150.76:~/.ssh/id_rsa ~/.ssh
    root@192.168.150.76's password:
    id_rsa                                                                                100% 1679     1.6KB/s   00:00
    [opuser@centos7-en ~]$
    [opuser@centos7-en ~]$ ssh root@192.168.150.110
    Last login: Tue Oct 28 11:43:04 2014 from centos7-cn
    [root@centos7 ~]#

    已经顺利的进入了。

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

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

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

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

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

    [opuser@centos7-en ~]$ cp .ssh/id_rsa /tmp
    [opuser@centos7-en ~]$ scp root@192.168.150.76:~/.ssh/id_rsa ~/.ssh
    root@192.168.150.76's password:
    id_rsa                                                                                100% 1766     1.7KB/s   00:00
    [opuser@centos7-en ~]$
    [opuser@centos7-en ~]$ ssh root@192.168.150.110
    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
    [root@centos7 ~]#

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

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

    删除这三行并不能去除口令,呵呵;同时正确的口令也失效了,呵呵呵呵;而且呢,ssh-keygen的man page说了,假如口令短语忘了是没有办法找回的,只能重新生成密钥。
    三、增加口令短语
    那么在生产环境里,已经部署了不带口令短语的密钥,怎么增加口令短语呢?这样:

    [root@centos7-cn ~]# 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呢?试试:

    [root@centos7-cn ~]# ssh-copy-id root@192.168.150.110
    /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代理守护进程)。

    启动代理守护进程:

    [root@centos7-cn ~]# eval `ssh-agent`
    Agent pid 11844
    [root@centos7-cn ~]#

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

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

    试试连接目标主机:

    [root@centos7-cn ~]# ssh root@192.168.150.110
    Last login: Wed Oct 29 08:30:41 2014 from 192.168.150.100
    [root@centos7 ~]#

    OK了!

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

    [root@centos7-cn ~]# 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)
    [root@centos7-cn ~]#

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

    [root@centos7-cn ~]# ssh-add -D
    All identities removed.
    [root@centos7-cn ~]#

    再试试连接目标主机:

    [root@centos7-cn ~]# ssh root@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 单独执行结果:

    [root@centos7-cn ~]# 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;
    [root@centos7-cn ~]#

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

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

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

    我们试一下:

    [root@centos7-cn ~]# eval `ssh-agent`
    Agent pid 11877
    [root@centos7-cn ~]# echo $SSH_AUTH_SOCK /tmp/ssh-2Aq37RrIkeOH/agent.11876
    [root@centos7-cn ~]# echo $SSH_AGENT_PID 11877
    [root@centos7-cn ~]#

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

    [root@centos7-cn ~]# pgrep ssh-agent
    11759
    11877
    [root@centos7-cn ~]#

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

    [root@centos7-cn ~]# 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;
    [root@centos7-cn ~]#
    [root@centos7-cn ~]# ssh-agent -k
    SSH_AGENT_PID not set, cannot kill agent

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

    [root@centos7-cn ~]#
    [root@centos7-cn ~]# SSH_AGENT_PID=14141; export SSH_AGENT_PID;
    [root@centos7-cn ~]#
    [root@centos7-cn ~]# ssh-agent -k
    unset SSH_AUTH_SOCK;
    unset SSH_AGENT_PID;
    echo Agent pid 14141 killed;
    [root@centos7-cn ~]#

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

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    正版 Windows 11产品密钥怎么查找/查看?

    还有3个月,微软将停止 Windows 10 的更新

    Windows 10 终止支持后,企业为何要立即升级?

    Windows 10 将于 2025年10 月终止技术支持,建议迁移到 Windows 11

    Windows 12 发布推迟,微软正全力筹备Windows 11 25H2更新

    Linux 退出 mail的命令是什么

    Linux 提醒 No space left on device,但我的空间看起来还有不少空余呢

    hiberfil.sys文件可以删除吗?了解该文件并手把手教你删除C盘的hiberfil.sys文件

    Window 10和 Windows 11哪个好?答案是:看你自己的需求

    盗版软件成公司里的“隐形炸弹”?老板们的“法务噩梦” 有救了!

    IT头条

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    液冷服务器概念股走强,博汇、润泽等液冷概念股票大涨

    01:17

    亚太地区的 AI 驱动型医疗保健:2025 年及以后的下一步是什么?

    16:30

    智能手机市场风云:iPhone领跑销量榜,华为缺席引争议

    15:43

    大数据算法和“老师傅”经验叠加 智慧化收储粮食尽显“科技范”

    15:17

    技术热点

    商业智能成CIO优先关注点 技术落地方显成效(1)

    用linux安装MySQL时产生问题破解

    JAVA中关于Map的九大问题

    windows 7旗舰版无法使用远程登录如何开启telnet服务

    Android View 事件分发机制详解

    MySQL用户变量的用法

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。