增加ssh无密码信任连接的安全性
为了方便系统管理或者服务器运维自动化,我们通常要在服务器间做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:
::a5:ef::a0::0b:f6:9c::b1:::0b: root@centos7-cn
The key's randomart image is:
+--[ RSA ]----+
| 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 :e2:ca::ac::fc:df:e9:::ec:0e::bb:.
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: 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: 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 :: from centos7-cn
[root@centos7 ~]#
[root@centos7 ~]# ip a show enp0s3
: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :::::d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.150.110/ brd 192.168.150.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe15:35d2/ 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 % .6KB/s :
[opuser@centos7-en ~]$
[opuser@centos7-en ~]$ ssh root@192.168.150.110
Last login: Tue Oct :: 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 :: 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 % .7KB/s :
[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 :: from 192.168.150.76
[root@centos7 ~]#
依然是只有输入正确的口令短语才能连接目标主机。
需要注意一点,第三主机仅仅复制了id_rsa,是不能做ssh-copy-id的,因为还没复制id_rsa.pub。
比较两个id_rsa文件,除了密钥本身不同了,加口令后的文件还多了三行(含一个空行):
Proc-Type: ,ENCRYPTED
DEK-Info: AES--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
[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 :: from 192.168.150.100
[root@centos7 ~]#
OK了!
其他需要知道的:
列出代理守护进程保存的私钥:
[root@centos7-cn ~]# ssh-add -l
5f:::3a:ac:::::ac:8c:d7:f0::c3: /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.; export SSH_AUTH_SOCK;
SSH_AGENT_PID=; export SSH_AGENT_PID;
echo Agent pid ;
[root@centos7-cn ~]#
eval `ssh-agent` 就是将ssh-agent的输出结果再执行一次,相当于:
[root@centos7-cn ~]# SSH_AUTH_SOCK=/tmp/ssh-CDZB3GtAT0MT/agent.; export SSH_AUTH_SOCK;
[root@centos7-cn ~]# SSH_AGENT_PID=; export SSH_AGENT_PID;
[root@centos7-cn ~]# echo Agent pid ;
所以 eval `ssh-agent` 的执行结果就是:
后台运行ssh-agent,并且在当前会话输出两个环境变量SSH_AUTH_SOCK、SSH_AGENT_PID,然后再显示 Agent pid 11759 。
我们试一下:
[root@centos7-cn ~]# eval `ssh-agent`
Agent pid
[root@centos7-cn ~]# echo $SSH_AUTH_SOCK
/tmp/ssh-2Aq37RrIkeOH/agent.
[root@centos7-cn ~]# echo $SSH_AGENT_PID
[root@centos7-cn ~]#
注意,这里得到的Pid跟单独执行的ssh-agent不同了,pgrep ssh-agent 会看到两个进程号:
[root@centos7-cn ~]# pgrep ssh-agent [root@centos7-cn ~]#
还要注意,退出当前会话并不会杀死ssh-agent进程。手工杀死进程除了上述的 pgrep 指令,还有ssh-agent -k 可以。试试:
[root@centos7-cn ~]# ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-gm8UdqqlTXeb/agent.; export SSH_AUTH_SOCK;
SSH_AGENT_PID=; export SSH_AGENT_PID;
echo Agent pid ;
[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=; export SSH_AGENT_PID;
[root@centos7-cn ~]#
[root@centos7-cn ~]# ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid killed;
[root@centos7-cn ~]#
这回可以了。所以 ssh-agent 命令最好还是用 eval `ssh-agent` 执行更方便,但是要记住不能重复执行,ssh-agent -k 只负责最后一个进程,道理呢?参考ssh-agent -k指令输出,自己琢磨一下吧。
增加ssh无密码信任连接的安全性的更多相关文章
- 第2次增加ssh 主机信任脚本
dr-mysql01:/root# cat a1.sh #用户名 uname="$1" #密码 passwd="$2" #执行检测并安装expect模块 ep= ...
- 批量实现多台服务器之间ssh无密码登录的相互信任关系
最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...
- Windows下通过SSH无密码连接Linux服务器
一.配置环境 1.本机系统:Windows 10 Pro(64位) 2.服务器:CentOS 6.10(64位) 3.SSH连接软件:SecureCRT 二.配置SSH无密码登录步骤 1.在个人PC机 ...
- CenOS SSH无密码登录
系统环境:CentOS6.8 软件环境:SSH(yum -y install openssh-clients) IP 地址:192.168.0.188 用户环境:root.xiaoming 系统 ...
- 使用ssh无密码登录
使用ssh无密码登录 ssh 是一个专为远程登录会话和其他网络服务提供安全性的协议.默认状态下ssh链接是需要密码认证的,可以通过添加系统认证(即公钥-私钥)的修改,修改后系统间切换可以避免密码输入和 ...
- SSH无密码登录的原理及配置
一.SSH概念(百度) SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目 ...
- 普通用户ssh无密码登录设置
这段时间在做Hadoop的环境配置,用root用户只需要按照一定的步骤进行操作就可以直接实现无密码登录,但如果使用新建用户,怎么尝试都不行. 本帖大部分都是其他人帖子的内容.如果按照下面的步骤还是不能 ...
- 烂泥:学习ssh之ssh无密码登陆
本文由秀依林枫提供友情赞助,首发于烂泥行天下 最近一个月没有写过文章,主要是刚刚换的新工作.新公司服务器OS使用的是ubuntu server版,和以前熟悉的centos还是有很多不同的. 刚好这几天 ...
- CentOS配置ssh无密码登录
CentOS配置ssh无密码登录的注意点 前提配置:使用root登录修改配置文件:/etc/ssh/sshd_config,将其中三行的注释去掉,如下: 然后重启ssh服务:service s ...
随机推荐
- Python 模拟简单区块链
首先这是说明一下这是Tiny熊老师的教程https://www.cnblogs.com/tinyxiong 另外还要说明一下,暑假指导老师让我们做一些关于区块链的应用.这里只是涉及极其简单的模拟,主要 ...
- HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.e ...
- linux防火墙添加端口
防火墙配置文件: /etc/sysconfig/iptables 1.使用命令查看端口开启情况(下图为安装时未选择开启防火墙) [root@fullstack ~]# iptables -L -n ...
- C#是类型安全语言
C#是一种类型安全语言:所有的表达式都解析成某个类型的一个实例,在编译器生成的代码中,只会执行对这个类型来说有效的操作. [优势] 许多错误能在编译时就检测到,确保代码在执行这段代码前是正确的: 生成 ...
- IIS Express被局域网访问
在 文件夹 C:\Users\administrator\Documents\IISExpress\config 下面 applicationhost.config 文件里 找到相应的项目 如 < ...
- Install ElasticSearch plugin for head
git clone git://github.com/mobz/elasticsearch-head.git yum install git npm cd elasticsearch-head npm ...
- logback+spring实践
配置文件名称使用: logback-spring.xml 配置user.home是jvm传过来的系统参数,可以直接使用 <property name="LOG_PATH&quo ...
- github访问慢解决
参考:https://github.com/chenxuhua/issues-blog/issues/3 hosts文件: # GitHub Start 192.30.253.112 github.c ...
- vue的watch详细用法
https://www.cnblogs.com/shiningly/p/9471067.html https://www.jb51.net/article/139282.htm
- requirej入门(二)
requirejs中的一些基本知识,包括API使用方式等. 基本API require会定义三个变量:define,require,requirejs,其中require === requirejs, ...