RedHat/CentOS 服务器 SSH 密钥交换实验(实现SSH密钥认证)
常见的文件传输协议,无法在同一条命令中指定远程主机上的用户名和密码,为实现服务器间文件传输的自动任务,需要以交换ssh密钥的方式完成ssh安全密钥验证(登录免密)
通过两台Linux服务器进行ssh密钥交换,实现以下效果:
(1)从主机1以huijiaxi用户免密码验证登录到主机2
(2)从主机2以rhel用户免密码验证登录到主机1
(3)用root用户登录主机1,使用scp命令,将文件推送到主机2的指定目录/tmp/testData/下
用huijiaxi用户登录主机2,使用scp命令,将文件推送到主机1的指定目录/tmp/testData/下
一、实验准备
准备两台Linux系统的服务器,确保两台机器网络互通,可以从主机1以huijiaxi用户通过ssh远程登录到主机2,可以从主机2以rhel用户通过ssh远程登录到主机1
主机1:192.168.109.151 (CentOS Linux release 7.5.1804) 已存在可登录用户rhel
主机2:192.168.109.252 (CentOS Linux release 7.5.1804) 已存在可登录用户huijiaxi
检查/etc/ssh/sshd_config配置文件
[root@mysqldatabase .ssh]# cat /etc/ssh/sshd_config
......
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes -- 是否支持密钥认证,默认yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys -- 认证公钥
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes -- 是否支持密钥验证
......
二、实验过程
1、完成从主机1以huijiaxi用户免密码验证登录到主机2的效果
(1) 以root用户连接到主机1
[root@localhost ~]# whoami
root
[root@localhost ~]#
(2) 在主机1生成RSA算法密钥对
[root@localhost ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa
Generating public/private rsa key pair.
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:
a9:0b:e8:8e:7f:21:47:8e:0f:d2:d4:8d:53:4e:89:32 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| . . |
| E . + |
| + * |
| . = o . |
| o + . S |
|. =.+ . |
| ..=... |
| o o. . |
|.o+. . |
+-----------------+
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ll -a
总用量 16
drwx------. 2 root root 54 11月 30 10:26 .
dr-xr-x---. 14 root root 4096 11月 30 10:13 ..
-rw-------. 1 root root 1675 11月 30 10:26 id_rsa
-rw-r--r--. 1 root root 408 11月 30 10:26 id_rsa.pub -- 生成的公钥
-rw-r--r--. 1 root root 177 5月 24 2021 known_hosts
(3) 将主机1上生成的公钥复制到主机2上huijiaxi用户家目录的.ssh目录下
[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
The authenticity of host '192.168.109.252 (192.168.109.252)' can't be established.
ECDSA key fingerprint is 3e:08:f4:fa:70:22:d4:45:37:35:4f:f5:fa:df:96:eb.
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 ar
e 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 [email protected]'s password: 此处输入主机2的huijiaxi用户的密码
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.
(4) 测试从主机1以huijiaxi用户免密码验证登录到主机2
[root@localhost ~]# ssh [email protected]
Last login: Tue Nov 30 10:35:07 2021 from 192.168.109.151 --远程登录成功
[huijiaxi@mysqldatabase ~]$ whoami
huijiaxi --当前登录用户为huijiaxi
[huijiaxi@mysqldatabase ~]$ ifconfig
ens33: flags=4163
inet 192.168.109.252 netmask 255.255.255.0 broadcast 192.168.109.255 --当前主机为192.168.109.252(主机1),已实现预期效果
inet6 fe80::e94e:4d79:bf87:15e1 prefixlen 64 scopeid 0x20
ether 00:0c:29:7a:7d:62 txqueuelen 1000 (Ethernet)
RX packets 1163 bytes 128243 (125.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 887 bytes 175032 (170.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 72 bytes 8088 (7.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 72 bytes 8088 (7.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[huijiaxi@mysqldatabase ~]$ mkdir -p /tmp/testDate
(5) 用root用户登录主机1,使用scp命令,将文件推送到主机2的指定目录/tmp/testData/下
[root@localhost ~]# mkdir -p /tmp/testData
[root@localhost ~]# echo "This is the test file from Host1(151) to Host2(252)." > /tmp/testData/test_file_1.txt
[root@localhost ~]# chmod -R 777 /tmp/testData/
[root@localhost ~]# scp /tmp/testData/test_file_1.txt [email protected]:/tmp/testData/test_file_1.txt
scp: /tmp/testData/test_file_1.txt: No such file or directory
[root@localhost ~]# scp /tmp/testData/test_file_1.txt [email protected]:/tmp/testData/
test_file_1.txt 100% 53 0.1KB/s 00:00
[root@localhost ~]#
2、完成从主机2以rhel用户免密码验证登录到主机1的效果(过程完全相同)
(1) 以huijiaxi用户连接到主机1
[root@localhost ~]# whoami
root
[root@localhost ~]#
(2) 在主机2生成RSA算法密钥对(在主机上生成RSA密钥对时,并非必需root权限,普通用户也可以,但是生成的密钥对需要保存在普通用户的家目录下)
[huijiaxi@mysqldatabase ~]$ ssh-keygen -t rsa -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/huijiaxi/.ssh/id_rsa.
Your public key has been saved in /home/huijiaxi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:oqdEWR9q4p2qL1hYttt1YLhKgY06ToHeZ9O/Hpnn6mg huijiaxi@mysqldatabase
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
|.+ .. . |
|+.= .ooo . |
|o+oo+o=.S |
|+o++.O.+. o |
|o= +*.=..+ . |
|..+..+ E..+ |
| .++ ..o=o. |
+----[SHA256]-----+
[huijiaxi@mysqldatabase ~]$ cd .ssh/
[huijiaxi@mysqldatabase .ssh]$ ll -a
total 12
drwx------. 2 huijiaxi huijiaxi 61 Nov 30 11:13 .
drwx------. 6 huijiaxi huijiaxi 140 Nov 30 10:48 ..
-rw-------. 1 huijiaxi huijiaxi 408 Nov 30 10:34 authorized_keys
-rw-------. 1 huijiaxi huijiaxi 1679 Nov 30 11:13 id_rsa
-rw-r--r--. 1 huijiaxi huijiaxi 404 Nov 30 11:13 id_rsa.pub
(3) 将主机2上生成的公钥复制到主机1上rhel用户家目录的.ssh目录下
[huijiaxi@mysqldatabase .ssh]$ ssh-copy-id -i /home/huijiaxi/.ssh/id_rsa.pub [email protected]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/huijiaxi/.ssh/id_rsa.pub"
The authenticity of host '192.168.109.151 (192.168.109.151)' can't be established.
ECDSA key fingerprint is SHA256:l5R66dqH0AkY8NuRjMNZIQ6H1AngnGIVtT7+8J50HtU.
ECDSA key fingerprint is MD5:5e:d8:cd:11:d9:03:71:e1:58:a4:e7:a7:70:df:2d:51.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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.
(4) 测试从主机1以huijiaxi用户免密码验证登录到主机2
[huijiaxi@mysqldatabase .ssh]$ ssh [email protected]
Last login: Tue Nov 30 10:17:50 2021 from 192.168.109.252
[rhel@localhost ~]$
[rhel@localhost ~]$
[rhel@localhost ~]$ whoami
rhel
[rhel@localhost ~]$ ifconfig
eno16777736: flags=4163
inet 192.168.109.151 netmask 255.255.255.0 broadcast 192.168.109.255
inet6 fe80::20c:29ff:fe0a:e95c prefixlen 64 scopeid 0x20
ether 00:0c:29:0a:e9:5c txqueuelen 1000 (Ethernet)
RX packets 1959 bytes 211350 (206.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1458 bytes 219750 (214.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 4 bytes 340 (340.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 340 (340.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(5) 用huijiaxi用户登录主机2,使用scp命令,将文件推送到主机1的指定目录/tmp/testData/下
[huijiaxi@mysqldatabase ~]$
[huijiaxi@mysqldatabase ~]$ echo "This is the test file from Host1(252) to Host2(151)." > /tmp/testData/test_file_2.txt
[huijiaxi@mysqldatabase ~]$
[huijiaxi@mysqldatabase ~]$ scp /tmp/testData/test_file_2.txt [email protected]:/tmp/testData/
test_file_2.txt 100% 53 38.7KB/s 00:00
[huijiaxi@mysqldatabase ~]$
三、实验总结
1. 用以下命令完成ssh密钥对交换
生成密钥对:
ssh-keygen -t rsa -f /root/.ssh/id_rsa -- t参数指定加密算法,f参数指定了密钥对生成的位置。在t参数缺省的状态下,默认为RSA2048
将生成的公钥复制到远程机器的指定用户的家目录:
ssh-copy-id -i /home/huijiaxi/.ssh/id_rsa.pub [email protected]
系统/etc/ssh/sshd_config配置文件中说明了,只认证.ssh/authorized_keys,所以在公钥复制过程中只需要指定目的主机的用户、IP,不需要指定路径
此命令的实际操作是,单纯将源公钥文件中的内容追加到目的主机指定用户的~/.ssh/authorized_keys中,如果文件authorized_keys不存在,就创建空文件后再追加公钥文件内容
[rhel@localhost ~]$ sha1sum /home/rhel/.ssh/authorized_keys
350ad83199527ea3746b0fd0dfde5ac850029ecb /home/rhel/.ssh/authorized_keys
[rhel@localhost ~]$ exit
logout
Connection to 192.168.109.151 closed.
[huijiaxi@mysqldatabase ~]$ sha1sum ~/.ssh/id_rsa.pub
350ad83199527ea3746b0fd0dfde5ac850029ecb /home/huijiaxi/.ssh/id_rsa.pub
[huijiaxi@mysqldatabase ~]$
2. 在主机上生成RSA密钥对时,并非必需root权限,普通用户也可以,但是生成的密钥对需要保存在普通用户的家目录下,原因有两个:
(1) 普通用户无权限写/root目录
[huijiaxi@mysqldatabase ~]$ ssh-keygen -t rsa -f /root/.ssh/id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Saving key "/root/.ssh/id_rsa" failed: Permission denied
(2) 只有做过安全密钥认证(生成密钥对)的用户才能实现远程ssh免密连接,在验证过程中,只检查当前用户的家目录。
[huijiaxi@mysqldatabase .ssh]$ ssh [email protected]
Last login: Tue Nov 30 10:17:50 2021 from 192.168.109.252
[rhel@localhost ~]$
[rhel@localhost ~]$
[rhel@localhost ~]$ whoami
rhel
[root@mysqldatabase .ssh]# ssh [email protected]
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@mysqldatabase .ssh]#
(3) 由(2)可得,要完成远程ssh免密连接,需要先在本地用已完成安全认证的用户登录。