当前位置:网站首页>ssh限制登录的四种手段

ssh限制登录的四种手段

2022-04-23 14:10:00 小灰墩墩

ssh限制登录的四种手段

1、xinetd服务

配置/etc/hosts.allow和hosts.deny

[root@centos7 ~]# hostname -I
192.168.75.171 
[root@centos7 ~]# vim /etc/hosts.allow 
all:192.168.75.160:allow
all:192.168.75.1:allow   #在虚拟机中操作,记得添加宿主机,要不ssh会中断,自杀
[root@centos7 ~]# cat /etc/hosts.deny
all:all:deny

[root@centos7 ~]# systemctl restart xinetd
[root@centos7 ~]# systemctl restart sshd



[root@CentOS6 ~]# hostname -I
192.168.75.160 
[root@CentOS6 ~]# ssh [email protected]
[email protected]'s password: 
Last login: Wed Nov  3 20:35:38 2021 from 192.168.75.160  #正常登录
[root@centos7 ~]# 

[root@centos7 ~]# hostname -I
192.168.75.170 
[root@centos7 ~]# ssh [email protected]
ssh_exchange_identification: read: Connection reset by peer #无法登录

2、iptables

# iptables -t filter -A INPUT -s 1.1.1.1 -d `hostname -I` -p tcp --dport 22 -j ACCEPT
# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP

3、通过修改sshd_config配置文件AllowUsers实现

AllowUsers
这个指令后面跟着一串用空格分隔的用户名列表(其中可以使用"*"和"?"通配符)。默认允许所有用户登录。
如果使用了这个指令,那么将仅允许这些用户登录,而拒绝其它所有用户。
如果指定了 USER@HOST 模式的用户,那么 USER 和 HOST 将同时被检查。
这里只允许使用用户的名字而不允许使用UID。相关的 allow/deny 指令按照下列顺序处理:
DenyUsers, AllowUsers, DenyGroups, AllowGroups
[root@centos7 ~]# vim /etc/ssh/sshd_config
#末尾追加如下地址,每个地址用空格隔开
AllowUsers *@10.213.53.40 *@10.213.53.41 *@10.213.53.42 *@10.213.53.43 *@10.213.53.44 *@10.213.53.45 *@11.234.* *@11.2.2.*
[root@centos7 ~]# systemctl restart sshd

4、设定登录黑名单

[root@centos7 ~]# vi /etc/pam.d/sshd
#追加
auth required /lib/security/pam_listfile.so item=user sense=deny file=/etc/sshd_user_deny_list onerr=succeed
所有/etc/sshd_user_deny_list里面的用户被拒绝ssh登录

版权声明
本文为[小灰墩墩]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swyer_66/article/details/121131690