1.配置sshd服务
SSH(Secure shell)是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux 系统的首选方式。在此之前,一般使用FTP或 Telnet 来进行远程登录。但是因为它们以明文的形式在网络中传输账户密码和数据信息,所以很不安全,很容易受到黑客发起的中间人攻击。轻则篡改传输的数据信息,重则直接抓取服务器的账户密码。
想要使用SSH协议来远程管理Linux系统,则需要部署配置 sshd服务程序。sshd是基于SSH协议开发的一款远程管理服务程序,不仅使用起来方便快捷,而且提供了以下两种安全验证的方法。
•基于口令的验证——用账户和密码来验证登录。
•基于密钥的验证——需要在本地生成密钥对,然后把密钥对中的公钥上传至服务器,并与服务器中的公钥进行比较;该方式相较来说更安全。
前文曾多次强调“Linux 系统中的一切都是文件”,因此在 Linux 系统中修改服务程序的运行参数,实际上就是在修改程序配置文件。sshd 服务的配置信息保存在/etc/ssh/sshd_config文件中。运维人员一般会把保存着最主要配置信息的文件称为主配置文件,而配置文件中有许多以井号(#)开头的注释行,要想让这些配置参数生效,需要在修改参数后再去掉前面的井号(#)。
sshd服务配置文件中包含的重要参数如表所示。
| 参数 | 作用 |
| Port 22 | 默认的sshd服务端口 |
| ListenAddress 0.0.0.0 | |
| Protocol 2 | SSH协议的版本号 |
| HostKey /etc/ssh/ssh_host_key | SSH协议版本为1时,DES私钥存放的位置 |
| HostKey /etc/ssh/ssh_host_rsa_key | SSH协议版本为2时,RSA私钥存放的位置 |
| HostKey /etc/ssh/ssh_host_dsa_key | SSH协议版本为2时,DSA私钥存放的位置 |
| PermitRootLogin yes | 设定是否允许root管理员直接登录 |
| StrictModes yes | 当远程用户的私钥改变时直接拒绝连接 |
| MaxAuthTries 6 | 最大密码尝试次数 |
| MaxSessions 10 | 最大终端数 |
| PasswordAuthentication yes | 是否允许密码验证 |
| PermitEmptyPasswords no | 是否允许空密码登录(很不安全) |
38 PermitRootLogin no
39 #StrictModes yes
(2)一般的服务程序并不会在配置文件修改之后立即获得最新的参数。如果想让新配置文件生效,则需要手动重启相应的服务程序。最好也将这个服务程序加入到开机启动项中,这样系统在下一次启动时,该服务程序便会自动运行,继续为用户提供服务
[root@CentOS7-1 ~]# systemctl restart sshd
[root@CentOS7-1 ~]# systemctl enable sshd
(3)当root管理员再尝试访问 sshd服务程序时,系统会提示不可访问的错误信息。仍然在CentOS7-2上测试。
[root@CentOS7-2 ~]# ssh 192.168.44.1
root@192.168.44.10's password: //此处输人远程主机root管理员的密码
Permission denied, please
try again.
注意:为了不影响下面的实训,请将/etc/ssh/sshd config配置文件恢复到初始状态。
2.安全密钥验证
加密是对信息进行编码和解码的技术,在传输数据时,如果担心被他人监听或截获,就可以在传输前先使用公钥对数据加密处理,然后再行传送。这样,只有掌握私钥的用户才能解密这段数据,除此之外的其他人即便截获了数据,一般也很难将其破译为明文信息。
在生产环境中使用密码进行口令验证存在着被暴力破解或嗅探截获的风险。如果正确配置了密钥验证方式,那么sshd服务程序将更加安全。
下面使用密钥验证方式,以用户student身份登录SSH服务器,具体配置如下。
(1)在服务器CentOS7-1上建立用户student,并设置密码。
[root@CentOS7-1 ~]# useradd student
[root@CentOS7-1 ~]# passwd student
(2)在客户端主机 CentOS7-2中生成“密钥对”。查看公钥id_rsa.pub和私钥
[root@CentOS7-2 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /按“Enter”键或设置密钥的存储路径
Enter passphrase (empty for no passphrase): //直接按“Enter”键或设置密钥的密码
Enter same passphrase again: //再次按“Enter”键或设置密钥的密码
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:
SHA256:fTBIoAkZL07L7UkzayBz8MvVDkIX8fJj36vvNaqgs1s root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ooo.... |
| .o = . . |
|. + * . . o |
| B = + . o |
|o O B = S . . |
| = B O o . . |
| o = E . . o |
| ..o . + . |
| ++ o== |
+----[SHA256]-----+
[root@CentOS7-2 ~]# cat /root/.ssh/id_rsa.pub
ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQCipS4PcoYt1g4KkSp3EySksUk6PrIaoTjGUmglHTvbh22J05WxnSLnOvY2MTffRaXXaf6Y6e32xdI6iB1VLedHU7AG/7TrWyhCk0ft0qAuPPtwWhYgn8AZEXVvb2yK29BuvRsN3Uw8bJsV4DX3VRLiW6wPAmZkihpgnZVgumcFV6VPLHNK7XCH0bhCu7LsXaDPj8keRlktQMNdfJm9V3wcPn4wtmPXP1K66s08IKWAA6NHYScCGoLW9N1ePwckCe7xxDvOY/DyhV1pmHkMhwlitXxr0vHInDatjCKE5Li6r/YwmPKGIAI6wX9g+zM86BdXSI1Baic7b4mPSPVRGJ root@CentOS7-2
[root@CentOS7-2 ~]# cat /root/.ssh/id_rsa
[root@CentOS7-2 ~]# ssh-copy-id student@192.168.44.1
/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
student@192.168.44.1's password:
Permission denied, please try again.
student@192.168.44.1's password: //此处输入远程服务器密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'student@192.168.44.1'"
and check to make sure that only the key(s) you wanted were added.
- 对服务器 CentOS 7-1进行设置(65行左右),使其只允许密钥验证,拒绝传统的口令壁证方式。将“PasswordAuthentication yes”改为“PasswordAuthentication no”。配置文件后保存并重启 sshd服务程序。
[root@CentOS7-1 ~]# vim /etc/ssh/sshd_config
......
61 #IgnoreRhosts yes
62 # To disable tunneled clear text passwords, change to no here!
63 #PasswordAuthentication yes
#PermitEmptyPasswords no
65 PasswordAuthentication no
......
Last login: Thu Oct 13 01:41:01 2022 from 192.168.44.10
[root@CentOS7-2 ~]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.44.1 netmask 255.255.255.0 broadcast 192.168.44.255
inet6 fe80::22a9:8ff5:fdec:2cf7 prefixlen scopeid 0x20<link>
ether 00:0c:29:30:a3:4c txqueuelen 1000 (Ethernet)
RX packets 581 bytes 81467 (79.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 744 bytes 111678 (109.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(6)在CentOS7-1上查看 CentOS7-2客户机的公钥是否传送成功。本例成功传送。
[root@CentOS7-1 ~]# cat /home/student/.ssh/authorized_keys