第三篇:使用firewall-cmd

本文来源:Working with firewalld         其它参考文档:redhat官方中文文档

 


 

1.查询类命令

2.设置类命令

3.运行时zone设置

4.永久性zone设置

5.直接选项

6.firewalld目前提供的功能

 

查询类命令

  • 获取firewalld的状态
    [root@bogon ~]# firewall-cmd --state         #返回firewalld的状态,没有输出
    not running
    [root@bogon ~]#  firewall-cmd --state && echo "Running" || echo "Not running"              #返回firewalld的状态,并输出
    not running
    Not running
  • 重新加载防火墙而不丢失状态信息:
    [root@bogon ~]# firewall-cmd --reload
    success

     如果您正在使用--complete-reload,则状态信息将丢失。这个选项只能用于遇到严重的防火墙问题时,例如,如果存在状态信息问题,即无法建立连接但防火墙规则正确。

  • 获取所有支持的zone的列表
    [root@bogon ~]# firewall-cmd --get-zones
    block dmz drop external home internal public seccon trusted work
  • 获取所有的zone(带已启用功能)
    [root@bogon ~]# firewall-cmd --list-all-zones
    block
      target: %%REJECT%%
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: 
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
        
    
    dmz
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    
    ......
  • 打印指定的zone(带已启用功能)。如果省略zone,将使用默认区域。
    [root@bogon ~]# firewall-cmd --zone=public --list-all
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: enp0s3
      sources: 
      services: ssh dhcpv6-client rabbitmq-server
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
  • 获取网络连接的默认zone
    [root@bogon ~]# firewall-cmd --get-default-zone
    public
  • 获取活动zone
    [root@bogon ~]# firewall-cmd --get-active-zones
    public
      interfaces: enp0s3
  • 获取与网络接口相关的zone
    [root@bogon ~]# firewall-cmd --get-zone-of-interface=enp0s3
    public 
  • 查询接口是否在zone中
    [root@bogon ~]#  firewall-cmd --zone=home --query-interface=enp0s3
    yes
  • 列出zone中启用的服务
    [root@bogon ~]# firewall-cmd --zone=home --list-services
    ssh mdns samba-client dhcpv6-client
  • 获取所有支持的服务的列表
    [root@bogon ~]# firewall-cmd --get-services
    RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp 
    dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master 
    high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns 
    mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy 
    proxy-dhcp ptp pulseaudio puppetmaster quassel rabbitmq-server radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap 
    spideroak-lansync 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
  • 获取所有支持的icmptypes的列表
    [root@bogon ~]#  firewall-cmd --get-icmptypes
    address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited 
    host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown 
    network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement 
    router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect 
    tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option

 

设置类命令

  • 设置默认zone
    [root@bogon ~]# firewall-cmd --set-default-zone=public           #位于默认区域中的所有接口将被推入新的默认区域,该区域定义新的外部发起的连接尝试的限制。活动的连接不受影响。
    Warning: ZONE_ALREADY_SET: public 
    success 
  • 添加接口到zone

  •  

    [root@bogon ~]# firewall-cmd --zone=dmz --add-interface=enp0s3                           #如果zone中不存在该接口则添加。如果zone选项被省略,则添加到默认的zone
    success
    [root@bogon ~]# firewall-cmd --zone=dmz --list-all
    dmz (active)
      target: default
      icmp-block-inversion: no
      interfaces: enp0s3
      sources: 
      services: ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules:  
  • 更改接口所属的区域

    [root@bogon ~]# firewall-cmd --zone=home --change-interface=enp0s3
    The interface is under control of NetworkManager, setting zone to 'home'.
    success
    [root@bogon ~]# firewall-cmd --zone=dmz --list-all
    dmz
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
        
    [root@bogon ~]# firewall-cmd --zone=home --list-all
    home (active)
      target: default
      icmp-block-inversion: no
      interfaces: enp0s3
      sources: 
      services: ssh mdns samba-client dhcpv6-client
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
        
    [root@bogon ~]# firewall-cmd --zone=public --list-all
    public
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: ssh dhcpv6-client rabbitmq-server
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
        
    [root@bogon ~]# 

     

  • 从zone中删除接口
    [root@bogon ~]# firewall-cmd --zone=dmz --remove-interface=enp0s3
    success
  • 在紧急情况下启用恐慌模式以阻止所有网络流量
    [root@bogon ~]# firewall-cmd --panic-on                  #shell客户端失去连接
    success
    [root@bogon ~]# firewall-cmd --panic-off                 #禁用恐慌模式
    success
    [root@bogon ~]# firewall-cmd --query-panic                #查询恐慌模式
    no

运行时zone设置

在运行模式下,对区域的更改不是永久性的。重新加载或重新启动后,更改将消失。

  • 在zone中的启用服务
    firewall-cmd [--zone = <zone>] --add-service = <service> [--timeout = <seconds>]        #如果未设置zone,则将使用默认zone。如果设置了超时,则该服务将仅启用该zone中设置的秒数。
    • 示例:
      [root@bogon ~]# firewall-cmd --zone=home --add-service=ipp-client --timeout=60      #在jome zone中启用ipp-client服务60秒
      success
      [root@bogon ~]# firewall-cmd --add-service=http                                     #在默认zone中启用http服务:
      You're performing an operation over default zone ('public'),
      but your connections/interfaces are in zone 'home' (see --get-active-zones)
      You most likely need to use --zone=home option.
      
      success
      [root@bogon ~]# firewall-cmd --zone=public --list-all
      public
        target: default
        icmp-block-inversion: no
        interfaces: 
        sources: 
        services: ssh dhcpv6-client rabbitmq-server http
        ports: 
        protocols: 
        masquerade: no
        forward-ports: 
        source-ports: 
        icmp-blocks: 
        rich rules: 
  • 禁用zone中的服务
    firewall-cmd [--zone = <zone>] --remove-service = <service>
    • 示例:
      [root@bogon ~]#  firewall-cmd --zone=public --remove-service=http  #在public zone中禁用http服务
      success
      [root@bogon ~]# firewall-cmd --zone=public --query-service=http    #查看http服务在public zone中是否启用
      no
  • 启用区域中的端口和协议组合,端口可以​​是单个端口<端口>或端口范围<端口> - <端口>。协议可以是tcpudp
    firewall-cmd [--zone = <zone>] --add-port = <port> [ -  <port>] / <protocol> [--timeout = <seconds>]
  • 禁用区域中的端口和协议组合
    firewall-cmd [--zone = <zone>] --remove-port = <port> [ -  <port>] / <protocol>
  • 查询区域中启用的端口和协议组合
     firewall-cmd [--zone = <zone>] --query-port = <port> [ -  <port>] / <protocol>
  • 在区域中启用伪装(masquerade)
    firewall-cmd [--zone = <zone>] --add-masquerade            #使该区域可以伪装。将私有地址映射并隐藏在公共ip地址的后面,是地址转换的一种方式主要用于路由器。由于内核限制,伪装仅支持IPv4。
  • 禁用区域中的伪装
    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>
  • 示例:阻止public zone中的响应应答消息(echo-reply messages ):
     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> }

     端口映射到另一个主机上的同一端口或同一主机上的另一个端口或另一个主机上的另一个端口。该端口可以是单端口<端口>或端口范围<端口> - <端口>。该协议是tcpudptoport是端口<端口>或端口范围<端口> - <端口>。toaddr是一个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 zone 中将 ssh转发到主机127.0.0.2
    [root@bogon ~]# firewall-cmd --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
    success

 

永久zone处理(在“运行时zone”的命令的第一个选项添加--permanent)

 永久选项不会直接影响运行时。这些选项只有在重新加载或重新启动后才可用。要有运行时和永久设置,您需要同时提供两者。--permanent选项须有是所有永久调用的第一个选项。

  • 获取支持的永久服务列表
    [root@bogon ~]# firewall-cmd --permanent --get-services
    RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel rabbitmq-server radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync 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
  • 获取支持的永久icmptypes列表
    [root@bogon ~]# firewall-cmd --permanent --get-icmptypes
    address-unreachable bad-header beyond-scope communication-prohibited destination-unreachable echo-reply echo-request failed-policy fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect reject-route required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option
  • 获取支持的永久区域列表
    [root@bogon ~]# firewall-cmd --permanent --get-zones
    block dmz drop external home internal public seccon trusted work
  • 启用区域中的服务
    firewall-cmd --permanent [--zone = <zone>] --add-service = <service>  #这可以使区域中的服务永久。如果区域选项被省略,则使用默认区域
  • 禁用区域中的服务
    firewall-cmd --permanent [--zone = <zone>] --remove-service = <service>
  • 查询区域中是否启用服务
    firewall-cmd --permanent [--zone = <zone>] --query-service = <service>
  • 示例:在家庭区域中永久启用服务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>
  • 示例:在家庭区域中永久启用端口443 / tcp
    firewall-cmd --permanent --zone = home --add-port = 443 / tcp

  .....等(同运行时zone)

 

直接选项

直接选项可以更直接地访问防火墙。这些选项要求用户知道基本的iptables概念,即table(filter / mangle / nat / ...),chain(INPUT / OUTPUT / FORWARD / ...),命令(-A / -D / -I / .. 。),参数(-p / -s / -d / -j / ...)和目标(ACCEPT / DROP / REJECT / ...)。直接选项应该仅作为最后的手段,当它不可能使用时诸如--add-service=‘service’或--add-rich-rule='rule'的命令时每个选项的第一个参数必须是ipv4ipv6eb使用ipv4,它将用于IPv4(iptables(8)),ipv6用于IPv6(ip6tables(8))和eb用于以太网桥(ethernet bridges)(ebtables(8))。

  • 将命令传递给防火墙。<args>可以是所有的iptables,ip6tables和ebtables命令行参数
    firewall-cmd --direct --passthrough {ipv4 | ipv6 | eb} <args>
  • 将一个新chain <chain> 添加到一个table <table>中。
    firewall-cmd [--permanent] --direct --add-chain { ipv4 | ipv6 | eb } <table> <chain>
  • 从table <table>中删除名称为<chain>的chain 。
    firewall-cmd [--permanent] --direct --remove-chain { ipv4 | ipv6 | eb } <table> <chain>
  • 查询table <table>中是否存在名称<chain>的chain。如果为true,则返回0,否则返回1。
    firewall-cmd [--permanent] --direct --query-chain {ipv4 | ipv6 | eb} <table> <chain>
  • 获取添加到table <table>中的所有的chain列表(用空格分隔)
    firewall-cmd [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } <table>
  • 在table<table>中的带有priority<priority>的chain<chain>中添加一条带有<args>参数的规则
    firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } <table> <chain> <priority> <args>
  • 从table<table>中的chain<chain>中移除一条规则
    firewall-cmd [--permanent] --direct --remove-rule { ipv4 | ipv6 | eb } <table> <chain> <args> 
  • 查询table<table>中的chain<chain>是否存在带参数<args>的规则。如果存在则返回0,否则返回1。
    firewall-cmd [--permanent] --direct --query-rule { ipv4 | ipv6 | eb } <table> <chain> <args>
  • 获取添加到table<table>中的所有<chain>规则(换行符分隔的参数列表)。
    firewall-cmd [--permanent] --direct --get-rules { ipv4 | ipv6 | eb } <table> <chain>

firewalld目前提供的功能

D-BUS Interface

 D-BUS接口提供关于防火墙状态的信息,并使其能够启用、禁用和查询防火墙设置。一般用于其它应用程序调用设置防火墙。

Zones

 网络或防火墙区域定义用于连接的接口的信任级别。firewalld提供了几个预定义的区域。区域配置选项和通用文件信息在firewalld.zone(5)手册页中描述。

Services

 服务可以是本地端口和目的地的列表,此外,如果启用了服务,还可以自动加载防火墙helper模块列表。使用预定义的服务使用户更容易启用和禁用对服务的访问。服务配置选项和通用文件信息在firewalld.service(5)手册页中描述

ICMP types

Internet控制消息协议(ICMP)用于在Internet协议(IP)中交换信息和错误消息。可以在firewalld中使用ICMP类型来限制这些消息的交换。ICMP类型配置选项和通用文件信息在firewalld.icmptype(5)手册页中描述。

Direct interface

 直接接口主要被服务或应用程序用于来添加特定的防火墙规则。

Runtime configuration

 运行时配置不是永久的,只有reload后才会生效。在重启或停止服务或重新启动系统后,这些选项将消失。

Permanent configuration

永久配置存储在配置文件中,并将在每个机器引导或服务重新加载或重新启动时生效。

Tray Applet

托盘applet firewall-applet直观地显示了防火墙的状态,也为用户提供了防火墙的问题。它还可以通过调用firewall-config来配置设置。

Graphical Configuration Tool

......

Command Line client

......

Support for ebtables

......

Default/Fallback configuration in /usr/lib/firewalld

该目录包含由firewalld为icmptypes、服务和区域提供的默认和后备配置。firewalld包中提供的文件不应该被更改。

System configuration settings in /etc/firewalld

这里存储的系统或用户配置要么是由系统管理员创建的,要么是由firewalld的配置接口或手工定制的。这些文件将重载默认的配置文件。

要手动更改预定义的icmptype、区域或服务的设置,请将文件从默认配置目录复制到系统配置目录中的相应目录,并相应地更改它。

如果您正在加载具有默认或后备文件的区域的默认值,那么/etc/firewalld中的文件将被重命名为<file>.old

 

posted on 2018-02-19 13:04  近博  阅读(1558)  评论(0编辑  收藏  举报

导航