当前位置:  首页>> 技术小册>> Linux应该怎么学(中)

TCP Wrappers是RHEL 7系统中默认启用的一款流量监控程序,它能够根据来访主机的地址与本机的目标服务程序作出允许或拒绝的操作。换句话说,Linux系统中其实有两个层面的防火墙,第一种是前面讲到的基于TCP/IP协议的流量过滤工具,而TCP Wrappers服务则是能允许或禁止Linux系统提供服务的防火墙,从而在更高层面保护了Linux系统的安全运行。
TCP Wrappers服务的防火墙策略由两个控制列表文件所控制,用户可以编辑允许控制列表文件来放行对服务的请求流量,也可以编辑拒绝控制列表文件来阻止对服务的请求流量。控制列表文件修改后会立即生效,系统将会先检查允许控制列表文件(/etc/hosts.allow),如果匹配到相应的允许策略则放行流量;如果没有匹配,则去进一步匹配拒绝控制列表文件(/etc/hosts.deny),若找到匹配项则拒绝该流量。如果这两个文件全都没有匹配到,则默认放行流量。
TCP Wrappers服务的控制列表文件配置起来并不复杂,常用的参数如表8-4所示。

在配置TCP Wrappers服务时需要遵循两个原则:
编写拒绝策略规则时,填写的是服务名称,而非协议名称;
建议先编写拒绝策略规则,再编写允许策略规则,以便直观地看到相应的效果。
下面编写拒绝策略规则文件,禁止访问本机sshd服务的所有流量(无须/etc/hosts.deny文件中修改原有的注释信息):

  1. [root@linuxprobe ~]# vim /etc/hosts.deny
  2. #
  3. # hosts.deny This file contains access rules which are used to
  4. # deny connections to network services that either use
  5. # the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # The rules in this file can also be set up in
  9. # /etc/hosts.allow with a 'deny' option instead.
  10. #
  11. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  12. # for information on rule syntax.
  13. # See 'man tcpd' for information on tcp_wrappers
  14. sshd:*
  15. [root@linuxprobe ~]# ssh 192.168.10.10
  16. ssh_exchange_identification: read: Connection reset by peer

接下来,在允许策略规则文件中添加一条规则,使其放行源自192.168.10.0/24网段,访问本机sshd服务的所有流量。可以看到,服务器立刻就放行了访问sshd服务的流量,效果非常直观:

  1. [root@linuxprobe ~]# vim /etc/hosts.allow
  2. #
  3. # hosts.allow This file contains access rules which are used to
  4. # allow or deny connections to network services that
  5. # either use the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  9. # for information on rule syntax.
  10. # See 'man tcpd' for information on tcp_wrappers
  11. sshd:192.168.10.
  12. [root@linuxprobe ~]# ssh 192.168.10.10
  13. The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.
  14. ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c.
  15. Are you sure you want to continue connecting (yes/no)? yes
  16. Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.
  17. root@192.168.10.10's password:
  18. Last login: Wed May 4 07:56:292017
  19. [root@linuxprobe ~]#

该分类下的相关小册推荐: