SSH安全,配置文件选项以及详细案例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ck784101777/article/details/102554834

提高SSH服务安全

1.SSH配置文件

文件路径:/etc/ssh/sshd_config

参考选项:

1_常见的SSH服务器监听的选项如下:

1 Port 22                  //监听的端口号为22
2 Protocol 2               //使用SSH V2协议
3 ListenAdderss 0.0.0.0     //监听的地址为所有的地址
4 UserDNS no              //禁止DNS反向解析

2_常见用户登录控制选项如下:

1 PermitRootLogin no              // 禁止root用户登录
2 PermitEmptyPasswords no      // 禁止空密码用户登录
3 LoginGraceTime 2m             // 登录验证时间为2分钟
4 MaxAuthTries 6                   //  最大重试次数6次
5 AllowUsers steven               // 只允许steven用户登录
6 DenyUsers steven               //  不允许登录用户 steven

3_常见登录验证方式如下:

1 PasswordAuthentication  yes             //启用密码验证
2 PubkeyAuthentication    yes              //启用密匙验证
3 AuthorsizedKeysFile .ssh/authorized_keys   //指定公钥数据库文件

 

2.修改ssh端口

将端口修改成999,用ssh登录默认采用22端口,使用-p选项指定端口.被ssh的主机ip为192.168.4.5

  1. [root@proxy ~]# vim /etc/ssh/sshd_config
  2. Port 999
  3. [root@proxy ~]# systemctl restart sshd
  4. [root@room9pc01 ~]#ssh [email protected]
  5. ssh: connect to host 192.168.1.41 port 22: Connection refused
  6. [root@room9pc01 ~]#ssh -p 999 [email protected]

 

3.禁止root用户登录/禁止密码为空的用户登录

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

  1. [root@proxy ~]# vim /etc/ssh/sshd_config
  2. .. ..
  3. PermitRootLogin no                                 //禁止root用户登录
  4. PermitEmptyPasswords no                             //禁止密码为空的用户登录
  5. .. ..
  6. [root@proxy ~]# systemctl restart sshd

2)测试基本安全策略

尝试以root用户SSH登录,失败:

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

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

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

 

4.设置白名单与黑名单,仅允许白名单用户访问

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

1)调整sshd服务配置,添加AllowUsers策略,仅允许用户jjh、ljz,其中ljz只能从网段192.168.4.0/24登录。

注意:如果没有这些用户,需要提前创建用户并设置密码。

如果你添加了白名单,那么只有白名单上的用户可以访问(将白名单注释掉代表允许所有用户访问),如果你只是想禁用某些用户访问,推荐使用黑名单

你还可以定义组白名单,将用户添加到某个组以便管理.

  1. [root@proxy ~]# vim /etc/ssh/sshd_config
  2. .. ..
  3. AllowUsers jjh [email protected]/24            //定义账户白名单
  4. ##DenyUsers USER1 USER2                                //定义账户黑名单
  5. ##DenyGroups GROUP1 GROUP2                            //定义组黑名单
  6. ##AllowGroups GROUP1 GROUP2                            //定义组白名单
  7. [root@proxy ~]# systemctl restart sshd

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

如果你没有将root添加到白名单,那么即使是root也无法ssh上去

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

 

5.实现密钥对验证登录(私钥口令)、免密码登入

1)准备客户机测试环境

为客户机的用户root建立SSH密钥对

使用ssh-keygen创建密钥对,将私钥口令设为空(直接回车):

  1. [root@client ~]$ ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):                    //直接回车将口令设为空
  6. Enter same passphrase again:                                             //再次回车确认
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 63:6e:cf:45:f0:56:e2:89:6f:62:64:5a:5e:fd:68:d2
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | |
  14. | |
  15. | . . . |
  16. | = = |
  17. | S = B . |
  18. | o B = . o |
  19. | + + = E .|
  20. | . + + o |
  21. | o |
  22. +-----------------+
  23. [root@client ~]$ ls -lh ~/.ssh/id_rsa*                 //确认密钥对文件
  24. -rw-------. 1 root root 1.8K 8月 15 10:35 /root/.ssh/id_rsa
  25. -rw-r--r--. 1 root root 403 8月 15 10:35 /root/.ssh/id_rsa.pub

2)将客户机上用户root的公钥部署到SSH服务器

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

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

3)在服务器上确认客户机用户root上传的公钥信息

默认部署位置为目标用户的家目录下 ~/.ssh/authorized_keys文件:

  1. [root@proxy ~]# ls ~/.ssh/authorized_keys
  2. .ssh/authorized_keys

4)在客户机上测试SSH密钥对验证

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

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

5)确认密钥验证使用正常后,禁用口令验证

这个配置项是使用秘钥与否的开关

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

  1. [root@proxy ~]# vim /etc/ssh/sshd_config
  2. .. ..
  3. PasswordAuthentication no                             //将此行yes改成no
  4.  
  5. [root@proxy ~]# systemctl restart sshd

猜你喜欢

转载自blog.csdn.net/ck784101777/article/details/102554834
今日推荐