Linux限制可通过SSH登录到服务器的IP
说明:这里的IP是指客户端IP,不是服务器IP,下面的例子使用了hosts.allow文件的配置方式,目的是快,但也有不灵活。
1、查看服务器最近登录ip:
[root@VM_0_13_centos ~]# last -10
zhangsan pts/0 101.86.1.1 Sun May 10 23:50 still logged in
root tty1 Sun May 10 23:47 still logged in
zhangsan pts/1 183.192.1.2 Sat Apr 25 15:29 - 18:59 (03:30)
zhangsan pts/0 183.192.1.2 Sat Apr 25 12:15 - 18:59 (06:44)
zhangsan pts/0 183.192.1.2 Thu Apr 23 22:35 - 23:59 (01:24)
zhangsan pts/0 183.192.1.2 Wed Apr 22 22:38 - 23:36 (00:57)
zhangsan pts/0 183.192.1.2 Tue Apr 21 22:36 - 23:03 (00:27)
zhangsan pts/0 183.192.1.2 Tue Apr 21 22:05 - 22:35 (00:29)
zhangsan pts/0 183.192.1.2 Sat Apr 18 21:40 - 21:40 (00:00)
zhangsan pts/0 183.192.1.2 Tue Apr 14 21:53 - 00:16 (02:23)
2、除了可以禁止某个用户登录,我们还可以针对固定的IP进行禁止登录,linux 服务器通过设置/etc/hosts.allow和/etc/hosts.deny这个两个文件,hosts.allow许可大于hosts.deny可以限制或者允许某个或者某段IP地址远程 SSH 登录服务器,方法比较简单,且设置后立即生效,不需要重启SSHD服务,具体如下:
[root@VM_0_13_centos ~]# vim /etc/hosts.allow 添加以下内容
sshd:192.168.0.1:allow # 允许 192.168.0.1 这个IP地址SSH登录
# sshd:192.168.0.:allow # 允许192.168.0.1/24这段IP地址的用户登录,多个网段或ip可以以逗号隔开,比如sshd:192.168.0.*,192.168.1.*:allow
# sshd:192.168.*.*:allow # 允许192.168下的全部ip登录
# sshd:ALL # 允许全部的ssh登录 (或删除前面的规则(没设置第3个步骤才行,否则禁用所有))
注:只能有一行sshd:条目,多行也没效果,如果多个ip可使用上面的第二种方式使用逗号隔开。
3、hosts.allow和hosts.deny两个文件同时设置规则的时候,hosts.allow文件中的规则优先级高,按照此方法设置后服务器只允许192.168.0.1这个IP地址的SSH登录,其它的IP都会拒绝。
[root@VM_0_13_centos ~]# vim /etc/hosts.deny添加
sshd:ALL # 表示除了上面允许的,其他的ip 都拒绝登陆ssh)
4、重启ssh服务
[root@VM_0_13_centos ~]# /bin/systemctl restart sshd.service

浙公网安备 33010602011771号