• CentOS7中firewalld的安装与使用详解


    一、软件环境

    [root@Geeklp201 ~]# cat /etc/redhat-release 
    CentOS Linux release 7.4.1708 (Core) 

    二、安装firewalld

    1、firewalld提供了支持网络/防火墙区域(zone)定义网络链接以及接口安全等级的动态防火墙管理工具。它支持 IPv4, IPv6 防火墙设置以及以太网桥接,并且拥有运行时配置和永久配置选项。它也支持允许服务或者应用程序直接添加防火墙规则的接口。firewall daemon 动态管理防火墙,不需要重启整个防火墙便可应用更改。因而也就没有必要重载所有内核防火墙模块了。不过,要使用 firewall daemon 就要求防火墙的所有变更都要通过该守护进程来实现,以确保守护进程中的状态和内核里的防火墙是一致的。另外,firewall daemon 无法解析由 ip*tables 和 ebtables 命令行工具添加的防火墙规则。守护进程通过 D-BUS 提供当前激活的防火墙设置信息,也通过 D-BUS 接受使用 PolicyKit 认证方式做的更改。

    检查系统是否已经安装了firewalld和firewall-config。

    [root@Geeklp201 ~]# rpm -qa|grep firewalld;rpm -qa|grep firewall-config
    firewalld-filesystem-0.4.4.4-6.el7.noarch
    firewalld-0.4.4.4-6.el7.noarch
    firewall-config-0.4.4.4-6.el7.noarch
    2、在CentOS7.4中,系统已经默认安装了firewalld,我们只需要安装firewall-config(注意:这里是firewall可不是firewalld)即可。
    [root@Geeklp201 ~]# yum -y install firewall-config
    3、如果你的发行版已经安装了安装了firewalld但是不确定是否为最新版,那么就需要进行一下更新。
    [root@Geeklp201 ~]# yum -y update firewalld
    
    4、安装完成,顺便查询一下安装详细信息
    [root@Geeklp201 ~]# rpm -qi firewalld firewall-config 
    Name        : firewalld
    Version     : 0.4.4.4
    Release     : 6.el7
    Architecture: noarch
    Install Date: 2017年10月18日 星期三 16时31分44秒
    Group       : Unspecified
    Size        : 1852322
    License     : GPLv2+
    Signature   : RSA/SHA256, 2017年08月10日 星期四 23时54分42秒, Key ID 24c6a8a7f4a80eb5
    Source RPM  : firewalld-0.4.4.4-6.el7.src.rpm
    Build Date  : 2017年08月05日 星期六 05时14分47秒
    Build Host  : c1bm.rdu2.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://www.firewalld.org
    Summary     : A firewall daemon with D-Bus interface providing a dynamic firewall
    Description :
    firewalld is a firewall service daemon that provides a dynamic customizable
    firewall with a D-Bus interface.
    Name        : firewall-config
    Version     : 0.4.4.4
    Release     : 6.el7
    Architecture: noarch
    Install Date: 2017年10月25日 星期三 15时32分18秒
    Group       : Unspecified
    Size        : 1158110
    License     : GPLv2+
    Signature   : RSA/SHA256, 2017年08月10日 星期四 23时54分38秒, Key ID 24c6a8a7f4a80eb5
    Source RPM  : firewalld-0.4.4.4-6.el7.src.rpm
    Build Date  : 2017年08月05日 星期六 05时14分47秒
    Build Host  : c1bm.rdu2.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://www.firewalld.org
    Summary     : Firewall configuration application
    Description :
    The firewall configuration application provides an configuration interface for
    firewalld.
    
    三、配置与使用

    启动firewalld服务。

    [root@Geeklp201 ~]# systemctl start firewalld
    
    查看firewalld状态。
    [root@Geeklp201 ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: active (running) since 三 2017-10-25 15:34:33 CST; 44min ago
         Docs: man:firewalld(1)
     Main PID: 2159 (firewalld)
       CGroup: /system.slice/firewalld.service
               └─2159 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
    
    10月 25 15:34:33 Geeklp201 systemd[1]: Starting firewalld - dynamic firewall daemon...
    10月 25 15:34:33 Geeklp201 systemd[1]: Started firewalld - dynamic firewall daemon.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    
    我们可以看到,firewalld已经正常运行,但是有一些警告。顺便提一下,firewalld服务的停止与禁用。

    停止firewalld服务。

    [root@Geeklp201 ~]# systemctl stop firewalld
    [root@Geeklp201 ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: inactive (dead) since 三 2017-10-25 16:22:24 CST; 32s ago
         Docs: man:firewalld(1)
      Process: 2159 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
     Main PID: 2159 (code=exited, status=0/SUCCESS)
    
    10月 25 15:34:33 Geeklp201 systemd[1]: Starting firewalld - dynamic firewall daemon...
    10月 25 15:34:33 Geeklp201 systemd[1]: Started firewalld - dynamic firewall daemon.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 16:22:23 Geeklp201 systemd[1]: Stopping firewalld - dynamic firewall daemon...
    10月 25 16:22:24 Geeklp201 systemd[1]: Stopped firewalld - dynamic firewall daemon.
    
    firewalld服务的禁用,也就是说在系统下次启动时是不会自动启动firewalld服务的。

    [root@Geeklp201 ~]# systemctl disable firewalld
    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@Geeklp201 ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    
    10月 25 15:34:33 Geeklp201 systemd[1]: Starting firewalld - dynamic firewall daemon...
    10月 25 15:34:33 Geeklp201 systemd[1]: Started firewalld - dynamic firewall daemon.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 16:22:23 Geeklp201 systemd[1]: Stopping firewalld - dynamic firewall daemon...
    10月 25 16:22:24 Geeklp201 systemd[1]: Stopped firewalld - dynamic firewall daemon.
    
    恢复开机自动启动firewalld服务。
    [root@Geeklp201 ~]# systemctl enable firewalld
    Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
    Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
    [root@Geeklp201 ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    
    10月 25 15:34:33 Geeklp201 systemd[1]: Starting firewalld - dynamic firewall daemon...
    10月 25 15:34:33 Geeklp201 systemd[1]: Started firewalld - dynamic firewall daemon.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
    10月 25 15:34:34 Geeklp201 firewalld[2159]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
    10月 25 16:22:23 Geeklp201 systemd[1]: Stopping firewalld - dynamic firewall daemon...
    10月 25 16:22:24 Geeklp201 systemd[1]: Stopped firewalld - dynamic firewall daemon.
    

    很多人都会使用Linux系统man或者help获取帮助,在此就不做介绍了。

    四、firewalld的使用详解

    1、使用之前,几个需要理解的概念。

    在正式使用firewalld之前,有几个概念我们必须了解一下。

    由firewalld 提供的区域按照从不信任到信任的顺序排序。

    丢弃(drop)任何流入网络的包都被丢弃,不作出任何响应。只允许流出的网络连接。

    阻塞(block):任何进入的网络连接都被拒绝,并返回 IPv4 的 icmp-host-prohibited 报文或者 IPv6 的 icmp6-adm-prohibited 报文。只允许由该系统初始化的网络连接。

    公开(public):用以可以公开的部分。你认为网络中其他的计算机不可信并且可能伤害你的计算机。只允许选中的连接接入。(You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.)

    外部(external):用在路由器等启用伪装的外部网络。你认为网络中其他的计算机不可信并且可能伤害你的计算机。只允许选中的连接接入。

    隔离区(dmz):用以允许隔离区(dmz)中的电脑有限地被外界网络访问。只接受被选中的连接。

    工作(work):用在工作网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。

    家庭(home):用在家庭网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。

    内部(internal):用在内部网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。

    受信任的(trusted):允许所有网络连接。

    2、常见问题FQA。

    (1)我应该选用哪个区域?
    例如,公共的 WIFI 连接应该主要为不受信任的,家庭的有线网络应该是相当可信任的。根据与你使用的网络最符合的区域进行选择。
    如何配置或者增加区域?
    你可以使用任何一种 firewalld 配置工具来配置或者增加区域,以及修改配置。工具有例如 firewall-config 这样的图形界面工具, firewall-cmd 这样的命令行工具,以及D-BUS接口。或者你也可以在配置文件目录中创建或者拷贝区域文件。 @PREFIX@/lib/firewalld/zones 被用于默认和备用配置,/etc/firewalld/zones 被用于用户创建和自定义配置文件。
    (2)如何为网络连接设置或者修改区域?
    区域设置以 ZONE= 选项 存储在网络连接的ifcfg文件中。如果这个选项缺失或者为空,firewalld 将使用配置的默认区域。
    如果这个连接受到 NetworkManager 控制,你也可以使用 nm-connection-editor 来修改区域。
    (3)由 NetworkManager 控制的网络连接
    防火墙不能够通过 NetworkManager 显示的名称来配置网络连接,只能配置网络接口。因此在网络连接之前 NetworkManager 将配置文件所述连接对应的网络接口告诉 firewalld 。如果在配置文件中没有配置区域,接口将配置到 firewalld 的默认区域。如果网络连接使用了不止一个接口,所有的接口都会应用到 fiwewalld。接口名称的改变也将由 NetworkManager 控制并应用到firewalld。
    如果一个接口断开了, NetworkManager 也将告诉 firewalld 从区域中删除该接口。当 firewalld 由 systemd 或者 init 脚本启动或者重启后,firewalld 将通知 NetworkManager 把网络连接增加到区域。

    3、使用firewalld防火墙

    • 在不改变状态的条件下重新加载防火墙:
     firewall-cmd --reload
    

    如果你使用 --complete-reload ,状态信息将会丢失。这个选项应当仅用于处理防火墙问题时,例如,状态信息和防火墙规则都正常,但是不能建立任何连接的情况。

    • 获取支持的区域列表
     firewall-cmd --get-zones
    

    这条命令输出用空格分隔的列表。

    • 获取所有支持的服务
     firewall-cmd --get-services
    

    这条命令输出用空格分隔的列表。

    • 获取所有支持的ICMP类型
     firewall-cmd --get-icmptypes
    

    这条命令输出用空格分隔的列表。

    • 列出全部启用的区域的特性
     firewall-cmd --list-all-zones
    

    输出格式是:

     <zone>
       interfaces: <interface1> ..
       services: <service1> ..
       ports: <port1> ..
       forward-ports: <forward port1> ..
       icmp-blocks: <icmp type1> ..
       
       ..
    
    • 输出区域 <zone> 全部启用的特性。如果生略区域,将显示默认区域的信息。
     firewall-cmd [--zone=<zone>] --list-all
    
    • 获取默认区域的网络设置
     firewall-cmd --get-default-zone
    
    • 设置默认区域
     firewall-cmd --set-default-zone=<zone>
    

    流入默认区域中配置的接口的新访问请求将被置入新的默认区域。当前活动的连接将不受影响。

    • 获取活动的区域
     firewall-cmd --get-active-zones
    

    这条命令将用以下格式输出每个区域所含接口:

     <zone1>: <interface1> <interface2> ..
     <zone2>: <interface3> ..
    
    • 根据接口获取区域
     firewall-cmd --get-zone-of-interface=<interface>
    

    这条命令将输出接口所属的区域名称。

    • 将接口增加到区域
     firewall-cmd [--zone=<zone>] --add-interface=<interface>
    

    如果接口不属于区域,接口将被增加到区域。如果区域被省略了,将使用默认区域。接口在重新加载后将重新应用。

    • 修改接口所属区域
     firewall-cmd [--zone=<zone>] --change-interface=<interface>
    

    这个选项与 --add-interface 选项相似,但是当接口已经存在于另一个区域的时候,该接口将被添加到新的区域。

    • 从区域中删除一个接口
     firewall-cmd [--zone=<zone>] --remove-interface=<interface>
    
    • 查询区域中是否包含某接口
     firewall-cmd [--zone=<zone>] --query-interface=<interface>
    

    返回接口是否存在于该区域。没有输出。

    • 列举区域中启用的服务
     firewall-cmd [ --zone=<zone> ] --list-services
    
    • 启用应急模式阻断所有网络连接,以防出现紧急状况
     firewall-cmd --panic-on
    
    • 禁用应急模式
     firewall-cmd --panic-off
    
    Note.png
    应急模式在 0.3.0 版本中发生了变化
    在 0.3.0 之前的 FirewallD版本中, panic 选项是 --enable-panic  --disable-panic.
    • 查询应急模式
     firewall-cmd --query-panic
    

    此命令返回应急模式的状态,没有输出。可以使用以下方式获得状态输出:

     firewall-cmd --query-panic && echo "On" || echo "Off"
    

    处理运行时区域

    运行时模式下对区域进行的修改不是永久有效的。重新加载或者重启后修改将失效。

    • 启用区域中的一种服务
     firewall-cmd [--zone=<zone>] --add-service=<service> [--timeout=<seconds>]
    

    此举启用区域中的一种服务。如果未指定区域,将使用默认区域。如果设定了超时时间,服务将只启用特定秒数。如果服务已经活跃,将不会有任何警告信息。

    • 例: 使区域中的 ipp-client 服务生效60秒:
     firewall-cmd --zone=home --add-service=ipp-client --timeout=60
    
    • 例: 启用默认区域中的http服务:
     firewall-cmd --add-service=http
    
    • 禁用区域中的某种服务
     firewall-cmd [--zone=<zone>] --remove-service=<service>
    

    此举禁用区域中的某种服务。如果未指定区域,将使用默认区域。

    • 例: 禁止 home 区域中的 http 服务:
     firewall-cmd --zone=home --remove-service=http
    

    区域种的服务将被禁用。如果服务没有启用,将不会有任何警告信息。

    • 查询区域中是否启用了特定服务
     firewall-cmd [--zone=<zone>] --query-service=<service>
    

    如果服务启用,将返回1,否则返回0。没有输出信息。

    • 启用区域端口和协议组合
     firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
    

    此举将启用端口和协议的组合。端口可以是一个单独的端口 <port> 或者是一个端口范围 <port>-<port> 。协议可以是 tcp  udp

    • 禁用端口和协议组合
     firewall-cmd [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
    
    • 查询区域中是否启用了端口和协议组合
     firewall-cmd [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
    

    如果启用,此命令将有返回值。没有输出信息。

    • 启用区域中的 IP 伪装功能
     firewall-cmd [--zone=<zone>] --add-masquerade
    

    此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公有IP。这是地址转换的一种形式,常用于路由。由于内核的限制,伪装功能仅可用于IPv4。

    • 禁用区域中的 IP 伪装
     firewall-cmd [--zone=<zone>] --remove-masquerade
    
    • 查询区域的伪装状态
     firewall-cmd [--zone=<zone>] --query-masquerade
    

    如果启用,此命令将有返回值。没有输出信息。

    • 启用区域的 ICMP 阻塞功能
     firewall-cmd [--zone=<zone>] --add-icmp-block=<icmptype>
    

    此举将启用选中的 Internet 控制报文协议 (ICMP) 报文进行阻塞。 ICMP 报文可以是请求信息或者创建的应答报文,以及错误应答。

    • 禁止区域的 ICMP 阻塞功能
     firewall-cmd [--zone=<zone>] --remove-icmp-block=<icmptype>
    
    • 查询区域的 ICMP 阻塞功能
     firewall-cmd [--zone=<zone>] --query-icmp-block=<icmptype>
    

    如果启用,此命令将有返回值。没有输出信息。

    • 例: 阻塞区域的响应应答报文:
     firewall-cmd --zone=public --add-icmp-block=echo-reply
    
    • 在区域中启用端口转发或映射
     firewall-cmd [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    

    端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。

    • 禁止区域的端口转发或者端口映射
     firewall-cmd [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    
    • 查询区域的端口转发或者端口映射
     firewall-cmd [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    

    如果启用,此命令将有返回值。没有输出信息。

    • 例: 将区域 home 的 ssh 转发到 127.0.0.2
     firewall-cmd --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
    

    处理永久区域

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

    • 获取永久选项所支持的服务
     firewall-cmd --permanent --get-services
    
    • 获取永久选项所支持的ICMP类型列表
     firewall-cmd --permanent --get-icmptypes
    
    • 获取支持的永久区域
     firewall-cmd --permanent --get-zones
    
    • 启用区域中的服务
     firewall-cmd --permanent [--zone=<zone>] --add-service=<service>
    

    此举将永久启用区域中的服务。如果未指定区域,将使用默认区域。

    • 禁用区域中的一种服务
     firewall-cmd --permanent [--zone=<zone>] --remove-service=<service>
    
    • 查询区域中的服务是否启用
     firewall-cmd --permanent [--zone=<zone>] --query-service=<service>
    

    如果服务启用,此命令将有返回值。此命令没有输出信息。

    • 例: 永久启用 home 区域中的 ipp-client 服务
     firewall-cmd --permanent --zone=home --add-service=ipp-client
    
    • 永久启用区域中的一个端口-协议组合
     firewall-cmd --permanent [--zone=<zone>] --add-port=<port>[-<port>]/<protocol>
    
    • 永久禁用区域中的一个端口-协议组合
     firewall-cmd --permanent [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
    
    • 查询区域中的端口-协议组合是否永久启用
     firewall-cmd --permanent [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
    

    如果服务启用,此命令将有返回值。此命令没有输出信息。

    • 例: 永久启用 home 区域中的 https (tcp 443) 端口
     firewall-cmd --permanent --zone=home --add-port=443/tcp
    
    • 永久启用区域中的伪装
     firewall-cmd --permanent [--zone=<zone>] --add-masquerade
    

    此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公有IP。这是地址转换的一种形式,常用于路由。由于内核的限制,伪装功能仅可用于IPv4。

    • 永久禁用区域中的伪装
     firewall-cmd --permanent [--zone=<zone>] --remove-masquerade
    
    • 查询区域中的伪装的永久状态
     firewall-cmd --permanent [--zone=<zone>] --query-masquerade
    

    如果服务启用,此命令将有返回值。此命令没有输出信息。

    • 永久启用区域中的ICMP阻塞
     firewall-cmd --permanent [--zone=<zone>] --add-icmp-block=<icmptype>
    

    此举将启用选中的 Internet 控制报文协议 (ICMP) 报文进行阻塞。 ICMP 报文可以是请求信息或者创建的应答报文或错误应答报文。

    • 永久禁用区域中的ICMP阻塞
     firewall-cmd --permanent [--zone=<zone>] --remove-icmp-block=<icmptype>
    
    • 查询区域中的ICMP永久状态
     firewall-cmd --permanent [--zone=<zone>] --query-icmp-block=<icmptype>
    

    如果服务启用,此命令将有返回值。此命令没有输出信息。

    • 例: 阻塞公共区域中的响应应答报文:
     firewall-cmd --permanent --zone=public --add-icmp-block=echo-reply
    
    • 在区域中永久启用端口转发或映射
     firewall-cmd --permanent [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    

    端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp 或udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。

    • 永久禁止区域的端口转发或者端口映射
     firewall-cmd --permanent [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    
    • 查询区域的端口转发或者端口映射状态
     firewall-cmd --permanent [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    

    如果服务启用,此命令将有返回值。此命令没有输出信息。

    • 例: 将 home 区域的 ssh 服务转发到 127.0.0.2
     firewall-cmd --permanent --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
    

    直接选项

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

    选项 --direct 需要是直接选项的第一个参数。

    • 将命令传递给防火墙。参数 <args> 可以是 iptables, ip6tables 以及 ebtables 命令行参数。
     firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb } <args>
    
    • 为表 <table> 增加一个新链 <chain> 。
     firewall-cmd --direct --add-chain { ipv4 | ipv6 | eb } <table> <chain>
    
    • 从表 <table> 中删除链 <chain> 。
     firewall-cmd --direct --remove-chain { ipv4 | ipv6 | eb } <table> <chain>
    
    • 查询 <chain> 链是否存在与表 <table>. 如果是,返回0,否则返回1.
     firewall-cmd --direct --query-chain { ipv4 | ipv6 | eb } <table> <chain>
    

    如果启用,此命令将有返回值。此命令没有输出信息。

    • 获取用空格分隔的表 <table> 中链的列表。
     firewall-cmd --direct --get-chains { ipv4 | ipv6 | eb } <table>
    
    • 为表 <table> 增加一条参数为 <args> 的链 <chain> ,优先级设定为 <priority>。
     firewall-cmd --direct --add-rule { ipv4 | ipv6 | eb } <table> <chain> <priority> <args>
    
    • 从表 <table> 中删除带参数 <args> 的链 <chain>。
     firewall-cmd --direct --remove-rule { ipv4 | ipv6 | eb } <table> <chain> <args>
    
    • 查询 带参数 <args> 的链 <chain> 是否存在表 <table> 中. 如果是,返回0,否则返回1.
     firewall-cmd --direct --query-rule { ipv4 | ipv6 | eb } <table> <chain> <args>
    

    如果启用,此命令将有返回值。此命令没有输出信息。

    • 获取表 <table> 中所有增加到链 <chain> 的规则,并用换行分隔。
     firewall-cmd --direct --get-rules { ipv4 | ipv6 | eb } <table> <chain>

    参考文章:

    http://fedoraproject.org/wiki/FirewallD/zh-cn#.E4.BD.BF.E7.94.A8_FirewallD_.E6.9E.84.E5.BB.BA.E5.8A.A8.E6.80.81.E9.98.B2.E7.81.AB.E5.A2.99

    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec-using_firewalls#sec-Introduction_to_firewalld

  • 相关阅读:
    设计模式学习每日一记(1.简单工厂模式)
    poj1068
    设计模式学习每日一记(2.外观模式)
    qooxdoo一句话备忘录[不断更新]
    设计模式学习每日一记(4.抽象工厂)
    C# 各种数据类型的最大值和最小值常数
    ASP.NET中实现二级或多级域名(修改UrlRewrite)
    网站中定时发送邮件
    重提URL Rewrite(1):IIS与ASP.NET
    Server Application Unavailable 的解决方法
  • 原文地址:https://www.cnblogs.com/aceboy/p/7820199.html
Copyright © 2020-2023  润新知