提高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
- [root@proxy ~]# vim /etc/ssh/sshd_config
- Port 999
- [root@proxy ~]# systemctl restart sshd
- [root@room9pc01 ~]#ssh [email protected]
- ssh: connect to host 192.168.1.41 port 22: Connection refused
- [root@room9pc01 ~]#ssh -p 999 [email protected]
3.禁止root用户登录/禁止密码为空的用户登录
1)调整sshd服务配置,并重载服务
- [root@proxy ~]# vim /etc/ssh/sshd_config
- .. ..
- PermitRootLogin no //禁止root用户登录
- PermitEmptyPasswords no //禁止密码为空的用户登录
- .. ..
- [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.
4.设置白名单与黑名单,仅允许白名单用户访问
针对SSH访问采用仅允许的策略,未明确列出的用户一概拒绝登录
1)调整sshd服务配置,添加AllowUsers策略,仅允许用户jjh、ljz,其中ljz只能从网段192.168.4.0/24登录。
注意:如果没有这些用户,需要提前创建用户并设置密码。
如果你添加了白名单,那么只有白名单上的用户可以访问(将白名单注释掉代表允许所有用户访问),如果你只是想禁用某些用户访问,推荐使用黑名单
你还可以定义组白名单,将用户添加到某个组以便管理.
- [root@proxy ~]# vim /etc/ssh/sshd_config
- .. ..
- AllowUsers jjh [email protected]/24 //定义账户白名单
- ##DenyUsers USER1 USER2 //定义账户黑名单
- ##DenyGroups GROUP1 GROUP2 //定义组黑名单
- ##AllowGroups GROUP1 GROUP2 //定义组白名单
- [root@proxy ~]# systemctl restart sshd
2)验证SSH访问控制,未授权的用户将拒绝登录。
如果你没有将root添加到白名单,那么即使是root也无法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.
5.实现密钥对验证登录(私钥口令)、免密码登入
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 ~]# ls ~/.ssh/authorized_keys
- .ssh/authorized_keys
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
5)确认密钥验证使用正常后,禁用口令验证
这个配置项是使用秘钥与否的开关
调整sshd服务配置,将PasswordAuthentication设为no
- [root@proxy ~]# vim /etc/ssh/sshd_config
- .. ..
- PasswordAuthentication no //将此行yes改成no
- [root@proxy ~]# systemctl restart sshd