教你如何保护Linux系统安全----SSH服务安全

提高SSH服务安全

1.配置基本安全策略(禁止root、禁止空口令)
2.针对SSH访问采用仅允许的策略,未明确列出的用户一概拒绝登录
3.实现密钥验证登录(私钥口令)、免密码登入
4.确认密钥验证使用正常后,禁用口令验证

步骤一:配置基本安全策略

1)调整sshd服务配置,并重载服务

[root@proxy ~]# vim /etc/ssh/sshd_config
.. ..
Protocol 2                                          //SSH协议
PermitRootLogin no                                  //禁止root用户登录
PermitEmptyPasswords no                              //禁止密码为空的用户登录
UseDNS  no                                          //不解析客户机地址
LoginGraceTime  1m                                  //登录限时
MaxAuthTries  3                                      //每连接最多认证次数
.. ..
[root@proxy ~]# systemctl restart sshd

2)测试基本安全策略
尝试以root用户SSH登录,失败:

[root@proxy ~]# ssh [email protected]
[email protected]'s password:
Permission denied, please try again.

将服务器上用户kate(如无该账户则先创建)的密码设为空,尝试SSH登录,也会失败:

[root@proxy ~]# passwd -d kate                          //清空用户口令
清除用户的密码 kate。
passwd: 操作成功
[root@proxy ~]# ssh [email protected]
[email protected]'s password:
Permission denied, please try again.

步骤二:针对SSH访问采用仅允许的策略,未明确列出的用户一概拒绝登录

1)调整sshd服务配置,添加AllowUsers策略,仅允许用户zhangsan、tom、useradm,其中useradm只能从网段192.168.4.0/24登录。
注意:如果没有这些用户,需要提前创建用户并设置密码。

[root@proxy ~]# vim /etc/ssh/sshd_config
.. ..
AllowUsers zhangsan tom [email protected]/24            //定义账户白名单
##DenyUsers  USER1  USER2                                //定义账户黑名单
##DenyGroups  GROUP1 GROUP2                            //定义组黑名单
##AllowGroups  GROUP1 GROUP2                            //定义组白名单
[root@proxy ~]# systemctl restart sshd

2)验证SSH访问控制,未授权的用户将拒绝登录。

[root@proxy ~]# ssh [email protected]              //已授权的用户允许登录
[email protected]'s password:
[useradm@proxy ~]$ exit
[root@proxy ~]# ssh [email protected]                  //未授权的用户被拒绝登录
[email protected]'s password:
Permission denied, please try again.

步骤三:实现密钥对验证登录(私钥口令)、免密码登入

1)准备客户机测试环境
为客户机的用户root建立SSH密钥对
使用ssh-keygen创建密钥对,将私钥口令设为空(直接回车):

[root@client ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
63:6e:cf:45:f0:56:e2:89:6f:62:64:5a:5e:fd:68:d2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|          . . .  |
|           = =   |
|        S = B .  |
|       o B = . o |
|        + + = E .|
|       . + + o   |
|          o      |
+-----------------+
[root@client ~]$ ls -lh ~/.ssh/id_rsa*                  //确认密钥对文件
-rw-------. 1 root root 1.8K 8月  15 10:35 /root/.ssh/id_rsa
-rw-r--r--. 1 root root  403 8月  15 10:35 /root/.ssh/id_rsa.pub

2)将客户机上用户root的公钥部署到SSH服务器
以用户root登入客户机,使用ssh-copy-id命令将自己的公钥部署到服务器:

[root@client ~]$ ssh-copy-id [email protected]
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

3)在服务器上确认客户机用户root上传的公钥信息
默认部署位置为目标用户的家目录下 ~/.ssh/authorized_keys文件:

[root@proxy ~]# tail -2 ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAzz+5AiFMGQ7LfuiV7eBnOcmRO9JRTcqRoynGO2y5
RyFL+LxR1IpEbkNrUyIZDk5uaX1Y8rwsf+pa7UZ2NyqmUEvNSUo0hQyDGsU9SPyAdzRCCvDgwpOFhaHi/OFnT+zqjAqXH2M9fFYEVUU4PIVL8HT19zCQRVZ/q3acQA34UsQUR0PpLJAobsf1BLe2EDM8BsSHckDGsNoDT9vk+u3e83RaehBMuy1cVEN5sLAaIrIeyM8Q0WxQNlqknL908HRkTlTeKrRoHbMnOBFj8StwlnscKHlkrsKkhUf8A9WWz/vL4GDwGND5jdca3I2hdITAySjMdfL1HMHnMYOgMjPM0Q== [email protected]

4)在客户机上测试SSH密钥对验证
在客户机用户root的环境中,以远程用户root登入192.168.4.5主机时,无需验证口令即可登入(因为私钥口令为空):

[root@client ~]$ ssh [email protected]                      //免交互直接登入
Last login: Thu Aug 15 10:48:09 2013 from 192.168.4.100

步骤四:确认密钥验证使用正常后,禁用口令验证

1)调整sshd服务配置,将PasswordAuthentication设为no

[root@proxy ~]# vim /etc/ssh/sshd_config
.. ..
PasswordAuthentication no                              //将此行yes改成no
[root@proxy ~]# systemctl restart sshd
发布了199 篇原创文章 · 获赞 117 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43417559/article/details/101222476
今日推荐