firewall-cmd 防火墙命令详解 及 TCP Wrappers

firewall-cmd 常用参数及作用

参数	                   作用
--get-default-zone	       查询默认的区域名称
--set-default-zone=<区域名称>      设置默认的区域,使其永久生效
--get-zones	             显示可用的区域
--get-services	            显示预先定义的服务
--get-active-zones	       显示当前正在使用的区域与网卡名称
--add-source=	            将源自此IP或子网的流量导向指定的区域
--remove-source=	      不再将源自此IP或子网的流量导向某个指定区域
--add-interface=<网卡名称>  	 将源自该网卡的所有流量都导向某个指定区域
--change-interface=<网卡名称>	  将某个网卡与区域进行关联
--list-all	          显示当前区域的网卡配置参数、资源、端口以及服务等信息
--list-all-zones	      显示所有区域的网卡配置参数、资源、端口以及服务等信息
--add-service=<服务名>	      设置默认区域允许该服务的流量
--add-port=<端口号/协议>	      设置默认区域允许该端口的流量
--remove-service=<服务名>	  设置默认区域不再允许该服务的流量
--remove-port=<端口号/协议>	  设置默认区域不再允许该端口的流量
--reload	          让“永久生效”的配置规则立即生效,并覆盖当前的配置规则
--panic-on	          开启应急状况模式
--panic-off	          关闭应急状况模式

 

 与Linux系统中其他的防火墙策略配置工具一样,使用firewalld配置的防火墙策略默认为运行时(Runtime)模式,又称为当前生效模式,而且随着系统的重启会失效。如果想让配置策略一直存在,就需要使用永久(Permanent)模式了,方法就是在用firewall-cmd命令正常设置防火墙策略时添加--permanent参数,这样配置的防火墙策略就可以永久生效了。但是,永久生效模式有一个“不近人情”的特点,就是使用它设置的策略只有在系统重启之后才能自动生效。如果想让配置的策略立即生效,需要手动执行firewall-cmd --reload命令。

查看firewalld服务当前所使用的区域:

[root@zhangjh ~]# firewall-cmd --get-default-zone 
public

查看eth0网卡在firewalld服务中的区域:

[root@zhangjh ~]# firewall-cmd --get-zone-of-interface=eth0
public

把firewalld服务中eth0网卡的默认区域修改为external,并在系统重启后生效。分别查看当前与永久模式下的区域名称:

[root@zhangjh ~]# firewall-cmd --permanent --zone=external --change-interface=eth0
success

查看eth0网卡在firewalld服务中的区域:  可以看到,在经过上边的修改之后并没有改变eth0网卡的默认区域,因为我们设置的是--permanent(永久)模式,需要执行命令firewall-cmd --reload 命令才能立即生效。

[root@zhangjh ~]# firewall-cmd --get-zone-of-interface=eth0
public

把firewalld服务中eth0网卡的默认区域修改为external,并在系统重启后生效。分别查看当前与永久模式下的区域名称:

[root@zhangjh ~]# firewall-cmd --zone=external --change-interface=eth0 --permanent 
success
[root@zhangjh ~]# firewall-cmd --get-zone-of-interface=eth0
public
[root@zhangjh ~]# firewall-cmd --get-zone-of-interface=eth0 --permanent 
external

把firewalld服务的当前默认区域设置为public:

[root@zhangjh ~]# firewall-cmd --set-default-zone=public
Warning: ZONE_ALREADY_SET: public
[root@zhangjh ~]# firewall-cmd --get-default-zone 
public

启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎用):

[root@zhangjh ~]# firewall-cmd --panic-on     #阻断一切网络
Warning: ALREADY_ENABLED
[root@zhangjh ~]# firewall-cmd --panic-off 
success

查询public区域是否允许请求SSH和HTTPS协议的流量:  

[root@zhangjh ~]# firewall-cmd --zone=public --query-service=ssh 
yes
[root@zhangjh ~]# firewall-cmd --zone=public --query-service=https
no

把firewalld服务中请求HTTPS协议的流量设置为永久允许,并立即生效:

[root@zhangjh ~]# firewall-cmd --zone=public --add-service=https
success
[root@zhangjh ~]# firewall-cmd --zone=public --add-service=https --permanent 
success
[root@zhangjh ~]# firewall-cmd --reload 
success
[root@zhangjh ~]# firewall-cmd --zone=public --query-service=https
yes

把firewalld服务中请求HTTP协议的流量设置为永久拒绝,并立即生效:

[root@zhangjh ~]# firewall-cmd --reload
success
[root@zhangjh ~]# firewall-cmd --zone=public --query-service=http
no
服务的访问控制列表(TCP wrappers):

TCP Wrappers是RHEL 7系统中默认启用的一款流量监控程序,它能够根据来访主机的地址与本机的目标服务程序作出允许或拒绝的操作。

TCP Wrappers服务的防火墙策略由两个控制列表文件所控制,用户可以编辑允许控制列表文件来放行对服务的请求流量,也可以编辑拒绝控制列表文件来阻止对服务的请求流量。控制列表文件修改后会立即生效,系统将会先检查允许控制列表文件(/etc/hosts.allow),如果匹配到相应的允许策略则放行流量;如果没有匹配,则去进一步匹配拒绝控制列表文件(/etc/hosts.deny),若找到匹配项则拒绝该流量。如果这两个文件全都没有匹配到,则默认放行流量。

客户端类型	  示例	              满足示例的客户端列表
单一主机	    192.168.10.10	          IP地址为192.168.10.10的主机
指定网段	    192.168.10.	          IP段为192.168.10.0/24的主机
指定网段	    192.168.10.0/255.255.255.0	IP段为192.168.10.0/24的主机
指定DNS后缀	.jianghua.com	          所有DNS后缀为.jianghua.com的主机
指定主机名称	www.jianghua.com	      主机名称为www.jianghua.com的主机
指定所有客户端	ALL	                所有主机全部包括在内

在配置TCP Wrappers服务时需要遵循两个原则:

  1. 编写拒绝策略规则时,填写的是服务名称,而非协议名称;
  2. 建议先编写拒绝策略规则,再编写允许策略规则,以便直观地看到相应的效果.

下面编写拒绝策略规则文件,禁止访问本机sshd服务的所有流量(无须/etc/hosts.deny文件中修改原有的注释信息):

[root@zhangjh ~]# vim /etc/hosts.deny 
#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
sshd:*


[root@zhangjh ~]# ssh 192.168.38.251
ssh_exchange_identification: read: Connection reset by peer

  

效果就是客户端ssh连接失败:

[c:~]$ ssh 192.168.38.251


Connecting to 192.168.38.251:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Connection closed by foreign host.

Disconnected from remote host(192.168.38.251:22) at 00:09:10.

Type `help' to learn how to use Xshell prompt.

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

[root@zhangjh ~]# vim /etc/hosts.allow 
#
# hosts.allow   This file contains access rules which are used to
#               allow or deny connections to network services that
#               either use the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
sshd:192.168.38.  

 成功连接:

[c:~]$ ssh 192.168.38.251


Connecting to 192.168.38.251:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Mon May 28 23:54:47 2018 from 192.168.38.1
[root@zhangjh ~]# 
原文地址:https://www.cnblogs.com/zhangjianghua/p/9103023.html