/etc/hosts.deny不会每次都生效

首先,我的sshd已经libwrap.so了,这里是检查结果:

root@FS9000:~# ldd sshd | grep libwrap libwrap.so.0 => /usr/lib/libwrap.so.0 (0x000000555e3fa000) 
  1. 当我像这样configuration/etc/hosts.deny时(没有/etc/hosts.allow):

     sshd: ALL 

    然后任何IP在访问服务器时都会被阻塞,这个效果明显。

  2. 当我像这样configuration/etc/hosts.deny时:

     sshd: 192.168.1.30 

    我试图从192.168.1.30访问服务器,login成功没有任何阻止!

看来它不能阻止指定的IP,这很奇怪。 问题是什么?

PS:我试过在embedded式系统中,使用busybox(v1.22.1),sshd是从openssh-6.3p1。


  1. 所有的testing都是基于本地networking,这里是sshd连接的细节。 192.168.1.30是一个linux客户端,192.168.1.212是embedded式系统。

    root @ FS9000:〜#cat /etc/hosts.deny ALL:192.168.1.30

    root @ FS9000:〜#env | grep SSH SSH_CLIENT = 192.168.1.30 36425 22 SSH_TTY = / dev / pts / 0 SSH_CONNECTION = 192.168.1.30 36425 192.168.1.212 22

    root @ FS9000:〜#netstat -antp主动Internet连接(服务器和已build立)原始Recv-Q发送-Q本地地址外部地址状态PID /程序名称
    tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1726 / portmap tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1770 / vsftpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2359 / sshd tcp 0 0 192.168.1.212:22 192.168.1.30:36425 ESTABLISHED 3256/0 tcp 0 0 ::: 22 ::: * LISTEN 2359 / sshd tcp 0 0 ::: 23 ::: * LISTEN 2148 / telnetd

4.当configuration/etc/hosts.deny像这样:

 root@FS9000:~# cat /etc/hosts.deny ALL: ALL :\ spawn (/bin/echo "%d connection to %N from %n[%a] denied" > /dev/pts/1) pts/1 is one telnetd connection,telnetd doesn't support libwrap,so all telnetd connections won't be blocked. Then I tried access 1.212 from 1.30, it prints this(and of course it blocked successfully): sshd connection to unknown from unknown[0.0.0.0] denied 

当像这样configuration/etc/hosts.deny时:

 sshd: 192.168.0.0/24 127.0.0.1 [ffe0::]/10 [::1] 

它仍然不能阻止192.168.0.0/24中的任何IP,192.168.1.30也可以访问它。

也许你没有使用IP地址192.168.1.30连接到sshd。

你可以使(某些)ssh程序使用-b使用特定的接口

-b bind_address在本地机器上使用bind_address作为连接的源地址。 仅在具有多个地址的系统上有用。

所以试试

 ssh -b 192.168.1.30 [email protected] 

如果您没有连接到本地networking上的主机,那么sshd看到的地址将不会是192.168.1.30 ,不能通过Internet路由。 如果您要连接本地networking上的主机,大多数系统将首先尝试IPv6。 由于主机经常configuration为使用隐私扩展并定期更改其IP地址,因此IPv6并不像每个主机那样容易被阻塞。

如果这是您可以访问的主机,那么在使用ssh连接之后,检查您所连接的IP地址。 sshd将有关连接的信息导出到环境中。 你可以用命令env | grep SSH查看这个 env | grep SSH

你可能想实现一个大部分closures的configuration。 testing时保持连接打开configuration/etc/hosts.deny来阻止所有这样的连接。

 ALL: ALL :\ spawn (/bin/echo "%d connection to %N from %n[%a] denied" | \ /usr/bin/mailx -s "Access Denied - %d" root@localhost) 

然后允许通过服务和/或networking地址进行访问。 您可以阻止DNS PTRlogging查找返回的名称以及地址范围。 以下仅允许从本地networking进行访问,但阻止访问路由器。

 sshd: 192.168.0.0/24 127.0.0.1 \ [ffe0::]/10 [::1] \ EXCEPT 192.168.0.1 

您也可以添加电子邮件通知以接受规则。 如果你不想收到通知,你可以删除它们。 如果您的服务不logging访问,则可以使用类似的规则通过向日志守护进程发送消息来logging访问。