关闭 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指令输出,自己琢磨一下吧。

    上一篇返回首页 下一篇

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

    别人在看

    帝国CMS7.5编辑器上传图片取消宽高的三种方法

    帝国cms如何自动生成缩略图的实现方法

    Windows 12即将到来,将彻底改变人机交互

    帝国CMS 7.5忘记登陆账号密码怎么办?可以phpmyadmin中重置管理员密码

    帝国CMS 7.5 后台编辑器换行,修改回车键br换行为p标签

    Windows 11 版本与 Windows 10比较,新功能一览

    Windows 11激活产品密钥收集及专业版激活方法

    如何从 Windows 11 中完全删除/卸载 OneNote?无解!

    抖音安全与信任开放日:揭秘推荐算法,告别单一标签依赖

    ultraedit编辑器打开文件时,总是提示是否转换为DOS格式,如何关闭?

    IT头条

    华为Pura80系列新机预热,余承东力赞其复杂光线下的视频拍摄实力

    01:28

    阿里千问3开源首战告捷:全球下载破千万,国产AI模型崛起新高度!

    01:22

    DeepSeek R1小版本试升级:网友实测编程能力已达到国际一线水平

    23:15

    NVIDIA 与 Dell 合作,大规模交付 Blackwell AI 系统

    20:52

    Cerebras 以最快的 Llama 4 Maverick 性能引领 LLM 推理竞赛

    20:51

    技术热点

    PHP中的随机性——你觉得自己幸运吗?

    搞定Ubuntu Linux下WPA无线上网

    Java使用内存映射实现大文件的上传

    MySQL安全性指南

    MySQL两项性能的基本测试浅谈

    教您使用UniqueIdentifier选取SQL Server主键

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

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