🤖 AI文章摘要 qwen-turbo-latest
加载中...

Firewalld

firewalld是一个红帽公司开源的防火墙守护进程,它是内核数据包分类框架nftable的一个前端应用,常用的前端框架例如ufw。

  • 动态防火墙规则管理,防火墙规则的修改无需重启防火墙进程即可生效,且应用服务不会中断。
  • 运行时配置和持久化配置分开配置。运行时配置的修改立即生效,持久化配置的修改重载后生效,运行时配置将在下一次启动时被覆盖。
  • 基于连接(Connection)或接口(Interface)信任等级划分的防火墙区域(Zone)管理。
  • 基于服务(Service)和应用(Application)的防火墙规则配置。
  • 支持IPv4和IPv6防火墙设置。

区域(Zone)

防火墙区域定义了连接(Connection)或接口(Interface)或来源(Source)的信任等级,区域和连接与接口之间是一对多的关系。防火墙针对区域遵循以下原则:

  • 流量的入口仅有一个区域,意味着一个数据包不可能同时来自多个区域。
  • 流量的出口仅有一个区域,意味着一个数据包不可能同时去往多个区域。
  • 防火墙区域定义了来源的信任等级。
  • 区域内部的流量转发默认允许。
  • 区域间的流量转发默认拒绝。

原则4和原则5是原则3的结果。原则4可以通过区域的--remove-forward选项移除,原则5可以通过区域间的防火墙规则配置。

firewalld预定义的几个防火墙区域,在连接或接口未指定区域时,默认为public区域。

  • drop: 丢弃所有入站流量并且不进行应答,允许出站流量。
  • block: 拒绝所有入站流量并进行ICMP应答,仅允许系统定义的出站流量。
  • public: 仅接受系统定义的入站流量,firewalld默认的连接或接口区域。
  • external: 仅接受系统定义的入站流量,出站流量默认进行NAT地址伪装。
  • dmz: 仅接受系统定义的入站流量,并限制该区域访问internal区域的行为。
  • work: 仅接受系统定义的入站流量,代表您信任该区域的流量。
  • home: 和work一致,仅用作区分。
  • internal: 和work一致,仅用作区分。
  • trusted: 接受所有入站流量,代表您完全信任该区域所有流量。

sudo firewall-cmd --info-zone=public

public (default, active)
  target: default
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces: enp5s0f3u1u4c2
  sources: 
  services: audio-share dhcpv6-client kdeconnect ssh sunshine vnc-server
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

sudo firewall-cmd --info-zone=dmz

dmz
  target: default
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

规则(Rule)

规则分为区域规则(Zone Rule)和防火墙规则(Firewall Rule)。区域规则用于管控区域访问运行防火墙主机的行为(iptable中的Input行为),防火墙规则用于管控区域间数据转发行为(iptable中的Forward行为)。

以下是创建区域规则的命令,以下针对public区域允许访问本机的https服务(即放行本机443端口)。


firewall-cmd --permanent --zone public --add-service https
firewall-cmd --reload

以下是创建防火墙规则的命令示例,以下创建了名为是myPolicy的防火墙规则,流量入口为internal,流量出口为external


firewall-cmd --permanent --new-policy myPolicy
firewall-cmd --permanent --policy myPolicy --add-ingress-zone internal
firewall-cmd --permanent --policy myPolicy --add-egress-zone external

服务(Service)

服务是一个基于XML的网络定义文件,firewalld预置的service文件位于/usr/lib/firewalld/services目录下。


ls /usr/lib/firewalld/services
0-AD.xml                 etcd-client.xml                     kubelet-worker.xml              pmwebapi.xml                     stellaris.xml
afp.xml                  etcd-server.xml                     kubelet.xml                     pop3s.xml                        stronghold-crusader.xml
alvr.xml                 factorio.xml                        kube-nodeport-services.xml      pop3.xml                         stuns.xml
amanda-client.xml        finger.xml                          kube-scheduler-secure.xml       postgresql.xml                   stun.xml
amanda-k5-client.xml     foreman-proxy.xml                   kube-scheduler.xml              privoxy.xml                      submission.xml
amqps.xml                foreman.xml                         kube-worker.xml                 prometheus-node-exporter.xml     supertuxkart.xml
amqp.xml                 freeipa-4.xml                       ldaps.xml                       prometheus.xml                   svdrp.xml
anno-1602.xml            freeipa-ldaps.xml                   ldap.xml                        proxy-dhcp.xml                   svn.xml
anno-1800.xml            freeipa-ldap.xml                    libvirt-tls.xml                 ps2link.xml                      syncthing-gui.xml
apcupsd.xml              freeipa-replication.xml             libvirt.xml                     ps3netsrv.xml                    syncthing-relay.xml
aseqnet.xml              freeipa-trust.xml                   lightning-network.xml           ptp.xml                          syncthing.xml
audio-share.xml          ftp.xml                             llmnr-client.xml                pulseaudio.xml                   synergy.xml
audit.xml                galera.xml                          llmnr-tcp.xml                   puppetmaster.xml                 syscomlan.xml
ausweisapp2.xml          ganglia-client.xml                  llmnr-udp.xml                   quassel.xml                      syslog-tls.xml
bacula-client.xml        ganglia-master.xml                  llmnr.xml                       radius.xml                       syslog.xml
bacula.xml               git.xml                             managesieve.xml                 radsec.xml                       telnet.xml
bareos-director.xml      gpsd.xml                            matrix.xml                      rdp.xml                          tentacle.xml
bareos-filedaemon.xml    grafana.xml                         mdns.xml                        redis-sentinel.xml               terraria.xml
bareos-storage.xml       gre.xml                             memcache.xml                    redis.xml                        tftp.xml
bb.xml                   high-availability.xml               minecraft.xml                   RH-Satellite-6-capsule.xml       tile38.xml
bgp.xml                  http3.xml                           minidlna.xml                    RH-Satellite-6.xml               tinc.xml
bitcoin-rpc.xml          https.xml                           mndp.xml                        rootd.xml                        tor-socks.xml
bitcoin-testnet-rpc.xml  http.xml                            mongodb.xml                     rpc-bind.xml                     transmission-client.xml
bitcoin-testnet.xml      ident.xml                           mosh.xml                        rquotad.xml                      turns.xml
bitcoin.xml              imaps.xml                           mountd.xml                      rsh.xml                          turn.xml
bittorrent-lsd.xml       imap.xml                            mpd.xml                         rsyncd.xml                       upnp-client.xml
ceph-exporter.xml        iperf2.xml                          mqtt-tls.xml                    rtsp.xml                         vdsm.xml
ceph-mon.xml             iperf3.xml                          mqtt.xml                        salt-master.xml                  vnc-server.xml
ceph.xml                 ipfs.xml                            mssql.xml                       samba-client.xml                 vrrp.xml
cfengine.xml             ipp-client.xml                      ms-wbt.xml                      samba-dc.xml                     warpinator.xml
checkmk-agent.xml        ipp.xml                             murmur.xml                      samba.xml                        wbem-https.xml
civilization-iv.xml      ipsec.xml                           mysql.xml                       sane.xml                         wbem-http.xml
civilization-v.xml       ircs.xml                            nbd.xml                         settlers-history-collection.xml  wireguard.xml
cockpit.xml              irc.xml                             nebula.xml                      sips.xml                         ws-discovery-client.xml
collectd.xml             iscsi-target.xml                    need-for-speed-most-wanted.xml  sip.xml                          ws-discovery-host.xml
condor-collector.xml     isns.xml                            netbios-ns.xml                  slimevr.xml                      ws-discovery-tcp.xml
cratedb.xml              jenkins.xml                         netdata-dashboard.xml           slp.xml                          ws-discovery-udp.xml
ctdb.xml                 kadmin.xml                          nfs3.xml                        smtp-submission.xml              ws-discovery.xml
dds-multicast.xml        kdeconnect.xml                      nfs.xml                         smtps.xml                        wsmans.xml
dds-unicast.xml          kerberos.xml                        nmea-0183.xml                   smtp.xml                         wsman.xml
dds.xml                  kibana.xml                          nrpe.xml                        snmptls-trap.xml                 xdmcp.xml
dhcpv6-client.xml        klogin.xml                          ntp.xml                         snmptls.xml                      xmpp-bosh.xml
dhcpv6.xml               kpasswd.xml                         nut.xml                         snmptrap.xml                     xmpp-client.xml
dhcp.xml                 kprop.xml                           opentelemetry.xml               snmp.xml                         xmpp-local.xml
distcc.xml               kshell.xml                          openvpn.xml                     spideroak-lansync.xml            xmpp-server.xml
dns-over-quic.xml        kube-apiserver.xml                  ovirt-imageio.xml               spotify-sync.xml                 zabbix-agent.xml
dns-over-tls.xml         kube-api.xml                        ovirt-storageconsole.xml        squid.xml                        zabbix-java-gateway.xml
dns.xml                  kube-controller-manager-secure.xml  ovirt-vmconsole.xml             ssdp.xml                         zabbix-server.xml
docker-registry.xml      kube-controller-manager.xml         plex.xml                        ssh.xml                          zabbix-trapper.xml
docker-swarm.xml         kube-control-plane-secure.xml       pmcd.xml                        statsrv.xml                      zabbix-web-service.xml
dropbox-lansync.xml      kube-control-plane.xml              pmproxy.xml                     steam-lan-transfer.xml           zero-k.xml
elasticsearch.xml        kubelet-readonly.xml                pmwebapis.xml                   steam-streaming.xml              zerotier.xml

ssh服务的定义如下,实际上他定义了TCP协议22端口的服务。


cat  /usr/lib/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="22"/>
</service>

常用的选项如下:

  • port或source-port
    • port
    • protocol
  • destination
    • ipv4=“address[/mask]”
    • ipv6=“address[/mask]”