禁止IP或IP段访问SSH:hosts.allow 和 hosts.deny
小何先生 Lv4

Linux 系统/etc/目录下的文件hosts.allow和hosts.deny,可以允许或拒绝某个IP、IP段访问系统中使用了libwrap库的服务,如telnetd、ftpd、sshd等。

hosts.allow 和 hosts.deny

image

当远程请求到达本机时,会先检查/etc/hosts.allow如果有匹配就默认允许访问,并跳过/etc/hosts.deny文件,否则将接着去匹配/etc/hosts.deny,有匹配则拒绝访问。

SSH 访问权限设置

允许单个IP访问

1
2
3
4
#/etc/hosts.allow 文件
sshd:192.168.0.0
#/etc/hosts.deny 文件
sshd:ALL

允许多个IP访问

1
2
3
4
#/etc/hosts.allow 文件
sshd:192.168.0.0 , 192.168.0.1
#IP段#/etc/hosts.deny 文件
sshd:ALL

允许单个IP段访问

1
2
3
4
#/etc/hosts.allow 文件
sshd:192.168.0.
#/etc/hosts.deny 文件
sshd:ALL

允许多个IP段访问

1
2
3
4
#/etc/hosts.allow 文件
sshd:192.168.0. , 192.168.1.
#/etc/hosts.deny 文件
sshd:ALL

允许多个服务多个IP段

1
2
3
4
#/etc/hosts.allow 文件
telnetd,sshd:192.168.1. , 192.168.2.
#/etc/hosts.deny 文件
sshd:ALL

禁止所有服务与所有IP

1
2
#/etc/hosts.deny 文件
all:all:deny
总字数 33.1k 访客数 访问量