firewalld防火墙配置和应用

firewalld介绍


firewalld和iptables的区别(动态防火墙和静态防火墙)

  1. 我们首先需要弄明白的第一个问题是到底什么是动态防火墙。为了解答这个问题,我们先来回忆一下 iptables service 管理防火墙规则的模式:用户将新的防火墙规则添加进 /etc/sysconfig/iptables 配置文件当中,再执行命令 service iptables reload 使变更的规则生效。在这整个过程的背后,iptables service 首先对旧的防火墙规则进行了清空,然后重新完整地加载所有新的防火墙规则,而如果配置了需要 reload 内核模块的话,过程背后还会包含卸载和重新加载内核模块的动作,而不幸的是,这个动作很可能对运行中的系统产生额外的不良影响,特别是在网络非常繁忙的系统中。

  2. 如果我们把这种哪怕只修改一条规则也要进行所有规则的重新载入的模式称为静态防火墙的话,那么 firewalld所提供的模式就可以叫做动态防火墙,它的出现就是为了解决这一问题,任何规则的变更都不需要对整个防火墙规则列表进行重新加载,只需要将变更部分保存并更新到运行中的 iptables 即可。

  3. 这里有必要说明一下 firewalld 和 iptables 之间的关系, firewalld 提供了一个 daemon 和 service,还有命令行和图形界面配置工具,它仅仅是替代了 iptables service 部分,其底层还是使用 iptables 作为防火墙规则管理入口。firewalld 使用 python 语言开发.

区域的概念


firewalld将网卡对应到不同的区域(zone),zone 默认共有9个,block dmz drop external home internal public trusted work.
不同的区域之间的差异是其对待数据包的默认行为不同,根据区域名字我们可以很直观的知道该区域的特征,在CentOS7系统中,默认区域被设置为public.

[root@CGW4700 ~]$ firewall-cmd --get-zones
work drop internal external trusted home dmz public block // firewalld的所有区域


[root@CGW4700 ~]$ firewall-cmd --get-default-zone
public      // 默认的

区域说明:
1. iptables service 在 /etc/sysconfig/iptables 中储存配置
2. firewalld 将配置储存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件里
3. 在/etc/firewalld/的区域设定是一系列可以被快速执行到网络接口的预设定。列表并简要说明如下:

drop(丢弃)
任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
drop
  target: DROP
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

block(限制)
任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝。
block
  target: %%REJECT%%
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

public(公共)
在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client http ssh
  ports: 8090/tcp 53/udp 88/tcp 53/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 


external(外部)
特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。
external
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  masquerade: yes
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

dmz(非军事区)
用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。
dmz
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 


work(工作)
用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。
work
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 


home(家庭)
用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

internal(内部)
用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
internal
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 


trusted(信任)
可接受所有的网络连接。
指定其中一个区域为默认区域是可行的。当接口连接加入了 NetworkManager,它们就被分配为默认区域。安装时,firewalld 里的默认区域被设定为公共区域。
trusted
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

firewalld支持的服务


  1. 在 /usr/lib/firewalld/services/ 目录中,还保存了另外一类配置文件,每个文件对应一项具体的网络服务,如 ssh 服务等.
  2. 与之对应的配置文件中记录了各项服务所使用的 tcp/udp 端口,在最新版本的 firewalld 中默认已经定义了 70+ 种服务供我们使用.
  3. 当默认提供的服务不够用或者需要自定义某项服务的端口时,我们需要将 service 配置文件放置在 /etc/firewalld/services/ 目录中.

  4. service 配置的好处显而易见:
    第一,通过服务名字来管理规则更加人性化,
    第二,通过服务来组织端口分组的模式更加高效,如果一个服务使用了若干个网络端口,则服务的配置文件就相当于提供了到这些端口的规则管理的批量操作快捷方式。

[root@CGW4700 ~]$ cat /usr/lib/firewalld/services/http.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>WWW (HTTP)</short>
  <description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>
  <port protocol="tcp" port="80"/>
</service>


# 列出firewalld支持的所有service
[root@CGW4700 ~]$ firewall-cmd --get-services 
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server


#列出当前zone支持的所有service,未指定zone为默认zone
[root@CGW4700 ~]$ firewall-cmd --list-services 
dhcpv6-client http ssh

firewalld配置使用详解

firewalld的配置使用命令firewall-cmd命令,或者可以使用图形界面配置
Example:

firewalld命令


# 关闭firewalld
[root@CGW4700 ~]$ systemctl  stop firewalld.service 
# 开启firewalld
[root@CGW4700 ~]$ systemctl start firewalld.service
# 把firewalld加入到系统服务中
[root@CGW4700 ~]$ systemctl enable firewalld.service
# 把firewalld从系统服务中移除
[root@CGW4700 ~]$ systemctl disable firewalld.service
# 查看firewalld状态,以下方法二选一
[root@CGW4700 ~]$ systemctl status firewalld.service
[root@CGW4700 ~]$ firewall-cmd --state

动态添加一条防火墙规则


# 假设自定义的ssh端口为12222,使用下面的命令动态添加一条防火墙规则
# 使用--permanent参数配置的规则,会添加到zone配置文件中,未指定则默认
[root@CGW4700 ~]$ firewall-cmd --add-port=12222/tcp --permanent


# 防火墙的配置文件也可以手动修改,修改后记得重载
# 重读防火墙,并不中断用户连接,即不丢失状态信息
[root@CGW4700 ~]$ firewall-cmd --reload


# 重读防火墙,中断用户连接,即丢失状态信息
/* 注意:通常在防火墙出现严重问题时,这个命令才会被使用。Ex:防火墙规则正确,但是出现状态信息问题和无法建立连接 */
[root@CGW4700 ~]$ firewall-cmd --complete-reload

服务相关操作


# 获取支持的所有服务列表
[root@CGW4700 ~]$ firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server


# 获取支持的icmp类型
[root@CGW4700 ~]$ firewall-cmd --get-icmptypes
destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded timestamp-reply timestamp-request


# 列举区域中启用的服务
[root@CGW4700 ~]$ firewall-cmd --zone=internal --list-services
mdns http https dhcpv6-client ssh dns samba-client


# 启用区域中的一种服务
/* 使区域中的http服务生效60秒 */
[root@CGW4700 ~]$ firewall-cmd --zone=internal --add-service=http --timeout=60


# 禁用区域中的某种服务
/* 如果当前区域中的服务没有启用,将不会有任何的警告信息 */
[root@CGW4700 ~]$ firewall-cmd --zone=internal --remove-service=ssh


# 查询区域中是否启用和某种服务
/* 如果服务启用返回1否则返回0,没有输出信息。 */
[root@CGW4700 ~]$ firewall-cmd --query-service=http



区域相关操作


# 列出全部启用的区域的特性
/* 列出的特性就在上述的区域介绍中 */
[root@CGW4700 ~]$ firewall-cmd --list-all-zones


# 获取默认区域的网络设置
[root@CGW4700 ~]$ firewall-cmd --get-default-zone
public


# 设置默认区域
/* 注意:流入默认区域中配置的接口的新访问请求将被置入新的默认区域。当前活动的连接不受影响 */
[root@CGW4700 ~]$ firewall-cmd --set-default-zone=external


# 获取活动的区域
[root@CGW4700 ~]$ firewall-cmd --get-active-zones
internal
  interfaces: br1 br0


# 获取接口区域
[root@CGW4700 ~]$ firewall-cmd --get-zone-of-interface=br0
internal


# 将接口添加入区域中,省略区域名,则添加入默认区域中
/* 将br0口添加入internal区域中 */
[root@CGW4700 ~]$ firewall-cmd --zone=internal --add-interface=br0


# 修改接口所属的区域
/* 将eth1接口修改到external区域中
   这个选项和--add-interface选项相似,但是当这个接口存在于另一个区域中,该接口将被添加进新的接口中
    */
[root@CGW4700 ~]$ firewall-cmd --zone=external --change-interface=eht1


# 从区域中删除某个接口
[root@CGW4700 ~]$ firewall-cmd --zone=internal --remove-interface=br0


# 查询某个接口是否存在于该区域中
/* 注意:返回该接口是否存在于该区域,没有输出 */
[root@CGW4700 ~]$ firewall-cmd --zone=internal --query-interface=br0


# 启用区域中的ip伪装功能
/* 此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公网IP。这是地址转换的一种形式,常用于路由。由于内核限制,伪装功能仅可用于IPv4 */
[root@CGW4700 ~]$ firewall-cmd --zone=external --add-masquerade


# 禁用区域中的ip伪装功能
[root@CGW4700 ~]$ firewall-cmd --zone=external --remove-masquerade


#查询区域的伪装状态
[root@CGW4700 ~]$ firewall-cmd --zone=external --query-masquerade


# 启用区域的ICMP阻塞功能
/* 此举将启用选中的Internet控制报文协议(ICMP)报文阻塞。ICMP报文可以是请求信息或者创建的应答报文,以及错误应答。 */
[root@CGW4700 ~]$ firewall-cmd --zone=external  --add-icmp-block=destination-unreachable


# 禁用区域中的ICMP阻塞功能
[root@CGW4700 ~]$ firewall-cmd --zone=external --remove-icmp-block=destination-unreachable


# 查询区域的ICMP阻塞功能
[root@CGW4700 ~]$ firewall-cmd --zone=external --query-icmp-block=destination-unreachable



启用端口和协议组合


# 启用tcp协议53端口,生效时长60s,端口可以是单独的端口或者端口范围
[root@CGW4700 ~]$ firewall-cmd --add-port=53/tcp --timeout=60 


# 禁用端口和协议组合
[root@CGW4700 ~]$ firewall-cmd --remove-port=53/tcp 


# 查询区域中是否启用某端口和协议组合
[root@CGW4700 ~]$ firewall-cmd --query-port=53/tcp 

端口映射和转发

# 启用端口映射,8023端口的报文映射到172.16.2.1地址的23端口
/* 端口可以映射到另一台主机的同一端口,也可以是同一台主机的不同端口。端口号可以是一个单独的端口或者是端口范围,协议可以是tcp或者udp。目标端口可以是端口号或者端口范围。目标地址可以IPv4地址。受内核限制,端口转发功能只可用于IPv4 */
[root@CGW4700 ~]$ firewall-cmd --zone=external --add-forward-port=8023:proto=tcp:toport=23:toaddr=172.16.2.1


# 禁止某一个端口转发或映射
[root@CGW4700 ~]$ firewall-cmd --zone=external --remove-forward-port=8023:proto=tcp:toport=23:toaddr=172.16.2.1


# 查询区域中的端口转发或映射
[root@CGW4700 ~]$ firewall-cmd --zone=external --query-forward-port=8023:proto=tcp:toport=23:toaddr=172.16.2.1

处理永久区域


# 永久选项不直接影响运行时的状态。这些选项仅在重载时或者重启服务时可用。为了使用运行时和永久设置,需要分别设置两者。选项--permanent是永久设置的第一个参数。


# 获取永久选项支持的服务
[root@CGW4700 ~]$ firewall-cmd --permanent --get-services


# 获取永久选项支持的icmp类型列表
[root@CGW4700 ~]$ firewall-cmd --permanent --get-icmp-types


# 获取支持的永久区域
[root@CGW4700 ~]$ firewall-cmd --permanent --get-zones


# 永久启用区域中的服务
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --add-service=http


# 查询区域中的服务是否永久启用
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --query-service=http


# 永久启用区域中的一个端口和协议组合
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --add-port=443/tcp


# 永久禁用区域中的一个端口 协议组合
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --remove-port=443/tcp


# 查询区域中的端口协议组合是否永久
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --query-port=443/tcp


# 永久启用区域中的伪装
[root@CGW4700 ~]$ firewall-cmd --permanent --zon=external --add-masquerade


# 永久禁用区域中的伪装
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --remove-masquerade


# 查询区域中的伪装是否为永久状态
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --query-masquerade


# 在区域中永久启用ICMP阻塞
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --add-icmp-block=echo-reply


# 在区域中永久禁用ICMP阻塞
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --remove-icmp-block=echo-reply


# 查询区域中是否永久启用ICMP阻塞功能
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --query-icmp-block=echo-reply


# 在区域中永久启用端口映射或转发
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --add-forward-port=port=8023:proto=tcp:toport=23:toaddr=172.16.2.1


# 在区域中永久禁用端口映射或转发
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --remove-forward-port=port=8023:proto=tcp:toport=23:toaddr=172.16.2.1


# 查询区域中是否永久开启端口映射或转发
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --query-forward-port=port=8023:proto=tcp:topport=23:toaddr=172.16.2.1

直接选项

# 直接选项主要用于使服务和应用程序能够增加规则。规则不会被保存,在重载和重启之后必须再次提交。传递的参数<args>与iptables,ip6tables以及ebtables一致。


# --direct是直接选项的第一个参数
# 将命令传递给防火墙,参数<args>可以是iptables,ip6tables和ebtables命令行参数。
[root@CGW4700 ~]$ firewall-cmd --direct --passthrough {ipv4 | ipv6 | eb} <args>


# 配置报文匹配mark重定向到指定地址的端口
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=internal --direct --passthrough ipv4 -t nat -I PREROUTING -p tcp -m mark --mark 0x1 -j DNAT --to-destination 172.16.1.1:9081




# 为表<table>增加一个新链<chain>
[root@CGW4700 ~]$ firewall-cmd --direct --add-chain { ipv4 | ipv6 | eb } <table> <chain>


# 从表<table>中删除链<chain>
[root@CGW4700 ~]$ firewall-cmd --direct --remove-chain { ipv4 | ipv6 | eb } <table> <chain>


# 查询<chain>链是存在表<table>
[root@CGW4700 ~]$ firewall-cmd --direct --query-chain { ipv4 | ipv6 | eb } <table> <chain>


# 获取用空格分隔的表<table>中链的列表
[root@CGW4700 ~]$ firewall-cmd --direct --get-chains { ipv4 | ipv6 | eb } <table>


# 为表 <table> 增加一条参数为 <args> 的链 <chain> ,优先级设定为 <priority>。


[root@CGW4700 ~]$ firewall-cmd --direct --add-rule { ipv4 | ipv6 | eb } <table> <chain> <priority> <args>
# 从表 <table> 中删除带参数 <args> 的链 <chain>。


[root@CGW4700 ~]$ firewall-cmd --direct --remove-rule { ipv4 | ipv6 | eb } <table> <chain> <args>
# 查询 带参数 <args> 的链 <chain> 是否存在表 <table> 中. 如果是,返回0,否则返回1.


[root@CGW4700 ~]$ firewall-cmd --direct --query-rule { ipv4 | ipv6 | eb } <table> <chain> <args>
# 如果启用,此命令将有返回值。此命令没有输出信息。


# 获取表 <table> 中所有增加到链 <chain> 的规则,并用换行分隔。


[root@CGW4700 ~]$ firewall-cmd --direct --get-rules { ipv4 | ipv6 | eb } <table> <chain>

防火墙应急操作


# 下面两条简单来说就是断网和连网
# 启用应急模式阻断所有网络连接,以防出现紧急情况
[root@CGW4700 ~]$ firewall-cmd --panic-on


# 禁用应急模式
[root@CGW4700 ~]$ firewall-cmd --panic-off 


# 查询应急模式
[root@CGW4700 ~]$ firewall-cmd --query-panic

通过配置文件使用firewalld


  1. 系统本身已经内置了一些常用服务的防火墙规则,存放在/usr/lib/firewalld/services/
    注意:请勿编辑/usr/lib/firewalld/services下的文件,只有在/etc/firewalld/services下才可被编辑

  2. 以下想开放80端口供外网访问http服务

/*1. 先将/usr/lib/firewalld/services下的http.xml文件cp至/etc/firewalld/services下
  2. 注:其实这个cp命令是可以省略的,因为系统会优先读取/etc/firewalld/services下的http.xml文件,如果此路径下没有,会默认到/usr/lib/firewalld/services路径下寻找http.xml文件
  3. 但是为了保证文件的完整性,应该cp到指定路径后,修改指定路径下的文件
*/


[root@CGW4700 ~]$ cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services/
[root@CGW4700 ~]$ ls /etc/firewalld/services/
http.xml
[root@CGW4700 ~]$ ls /etc/firewalld/zones/
public.xml public.xml.old


# 修改指定分区的文件
[root@CGW4700 ~]$ cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>
<service name="ssh"/>
<service name="http"/>
# 这行是后加的,要匹配 /etc/firewalld/services/文件夹下的文件名
</zone>


# 重载firewalld
[root@CGW4700 ~]$ firewall-cmd --reload 
  1. 修改ssh端口
# 1. 将/usr/lib/firewalld/service/ssh.xml文件copy到/etc/firewalld/services/ssh.xml
[root@CGW4700 ~]$ cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh.xml 


# 2. 修改/etc/firewalld/services/ssh.xml,将本来22端口修改为12222端口
[root@CGW4700 ~]$ cat /etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="12222"/>
</service>


#3. 重载firewalld
[root@CGW4700 ~]$ firewall-cmd --reload
  1. 指定ip访问ssh端口
# 修改指定区域的配置文件
# rule字段内为要限制的ip和使用的服务
[root@CGW4700 ~]$ cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>


<rule family="ipv4">
<source address="192.168.23.1"/>
<service name="ssh"/>
<accept/>
</rule>


</zone>
  1. 添加自定义服务,Ex:8080端口为新服务
# 1. 添加新的服务文件
[root@CGW4700 ~]$ cat /etc/firewalld/services/8080.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>8080 Test</short>
<description>此处为文字说明</description>
<port protocol="tcp" port="8080"/>
</service>


#编辑区域配置文件
[root@CGW4700 ~]$ cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>
<service name="ssh"/>
<service name="8080"/> # 这行是后加的,要匹配 /etc/firewalld/services/8080.xml 文件名
</zone>

实际环境实现


  1. 首先划分端口

    • 内网(Lan)端口划分到internal区,Ex:br口
    • 外网(Wan)端口划分到external区,Ex:eth,pppoe口
    • 外网端口选择external区的原因是:external区默认开启地址伪装功能(masquerade)
  2. 配置external区:

    • 将所有的wan口都添加进extrernal区
    • 开放某个端口,Ex:88(网关web)
    • 开放某个服务,Ex:ssh登录
    • 配置端口映射
  3. 配置internal区域:

    • 将所有lan口都添加进internal区
    • 配置mark match重定向报文,Ex:0x1—->9081
    • 配置开放某个端口,Ex:9081
    • 配置开放某个服务,Ex:http,ssh,https,dns
`配置external区域`
# 这边需要考虑是否需要加上--permanent参数
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=external --add-interface=eth0 
# 注意使用--permanent参数配置的防火墙规则,必须执行下面重启的命令
[root@CGW4700 ~]$ firewall-cmd --reload


# 开放网关web端口,生效时长一小时
[root@CGW4700 ~]$ firewall-cmd --zone=external --add-port=88/tcp --timeout=3600


# 开放某个服务,生效时长一小时
[root@CGW4700 ~]$ firewall-cmd --zone=external --add-service=ssh --timeout=3600


# 配置端口映射,生效时长一小时
[root@CGW4700 ~]$ firewall-cmd --zone=external --add-forward-port=port=8023:proto=tcp:toaddr=172.16.2.1:toport=23 --timeout=3600




`配置intrernal区域`
# 将lan口添加入internal区域
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=internal --add-interface=br0


[root@CGW4700 ~]$ firewall-cmd --reload  


# 配置mark为0x1的报文重定向到9081端口
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=internal --direct --passthrough ipv4 -t nat -I PREROUTING -p tcp  -m mark --mark 0x1 -j DNAT --to-destination 172.16.1.1:9081 


# 配置开放某个端口,9081端口需要开放,才能进行重定向
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=internal --add-port=9081/tcp


# 配置开放某个服务
[root@CGW4700 ~]$ firewall-cmd --permanent --zone=internal --add-service=http

具体实现


  1. 针对默认配置的操作
    • 默认配置应该被配置为永久生效,Ex:Lan口开启80和443端口
  2. 针对页面配置的操作
    • 其他修改导致的防火墙更改
      (1). 端口属性的修改,
      Ex:端口从lan口切换成wan口,需要修改后的wan口加入到external区中
      Ex:增加桥,需要将新增加的桥增加到internal区中
      Ex:端口映射,增加,删除页面,增加需要开启端口映射的时长
      (2). 网关安全策略页面的配置
      Ex:配置选项包括,区域,需要开启的服务,端口等,另外提供查看操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值