天天看點

RHCE(firewalld——檢視及使用)

1.2. 檢視目前狀态和設定 firewalld

1.2.1. 檢視目前狀态 firewalld

防火牆服務 firewalld 被預設安裝在系統中。使用 firewalld CLI 界面檢查該服務是否正在運作。

[[email protected] ~]# firewall-cmd --state
running
           

有關服務狀态的更多資訊,請使用 systemctl status 子指令:

[[email protected] ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor pres>
   Active: active (running) since Thu 2021-12-09 18:19:14 CST; 1min 9s ago
     Docs: man:firewalld(1)
 Main PID: 2023 (firewalld)
    Tasks: 2 (limit: 23489)
   Memory: 31.2M
   CGroup: /system.slice/firewalld.service
           └─2023 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --no>

Dec 09 18:19:14 kittod systemd[1]: Starting firewalld - dynamic firewall daemon...
Dec 09 18:19:14 kittod systemd[1]: Started firewalld - dynamic firewall daemon.
Dec 09 18:19:14 kittod firewalld[2023]: WARNING: AllowZoneDrifting is enabled. This>
           

1.2.3. 使用 CLI 檢視 firewalld 設定

使用 CLI 用戶端可能會對目前防火牆設定有不同的視圖。 --list-all 選項顯示 firewalld 設定的

完整概述。

firewalld 使用 zone 管理流量。如果 --zone 選項沒有指定區,該指令将在配置設定給活躍網絡接口和連

接的預設區裡有效。

要列出預設區的所有相關資訊:

[[email protected] ~]# firewall-cmd --list-all
work (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens192
  sources: 
  services: cockpit dhcpv6-client mysql ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
           

要指定顯示設定的區域,在 firewall-cmd --list-all 指令中添加 --zone=zone-name 參數,例如:

[[email protected] ~]# firewall-cmd --list-all --zone=home 
home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
           

檢視目前區中允許哪些服務:

[[email protected] ~]# firewall-cmd --list-services 
cockpit dhcpv6-client mysql ssh
           

1.3. 使用 firewalld

1.3.1. 使用 CLI 禁用緊急事件的所有流量

在緊急情況下,如系統攻擊,可以禁用所有網絡流量并關閉攻擊者。

要立即禁用網絡流量,請切換 panic 模式:

[[email protected] ~]# firewall-cmd --panic-on
           

重要

啟用 panic 模式可停止所有網絡流量。是以,它應隻在對機器有實體通路權限或者使用序列槽控制台登入時才使用。

關閉 panic 模式會使防火牆恢複到其永久設定。要關閉 panic 模式,請輸入:

[[email protected] ~]# firewall-cmd --panic-off
           

要檢視是否打開或關閉 panic 模式,請使用:

[[email protected] ~]# firewall-cmd --query-panic 
no
           

1.3.2. 使用 CLI 控制預定義服務的流量

控制流量的最簡單方法是在 firewalld 中添加預定義服務。這會打開所有必需的端口并根據 服務定義檔案 修改其他設定。

檢查該服務是否還未被允許:

[[email protected] ~]# firewall-cmd --list-services 
cockpit dhcpv6-client mysql ssh
           

列出所有預定義的服務:

[[email protected] ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bb bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc bittorrent-lsd ceph ceph-mon cfengine cockpit condor-collector ctdb dhcp dhcpv6 dhcpv6-client distcc dns dns-over-tls docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger freeipa-4 freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git grafana gre high-availability http https httptest imap imaps ipp ipp-client ipsec irc ircs iscsi-target isns jenkins kadmin kdeconnect kerberos kibana klogin kpasswd kprop kshell kube-apiserver ldap ldaps libvirt libvirt-tls lightning-network llmnr managesieve matrix mdns memcache minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy prometheus proxy-dhcp ptp pulseaudio puppetmaster quassel radius rdp redis redis-sentinel rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptrap spideroak-lansync spotify-sync squid ssdp ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tentacle tftp tftp-client tile38 tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
           

在允許的服務中添加服務:

[[email protected] ~]# firewall-cmd --add-service=http 
success
           

使新設定永久生效

[[email protected] ~]# firewall-cmd --runtime-to-permanent 
success
           

1.3.3. 添加新服務

可使用工具 firewall-cmd 和 firewall-offline-cmd 添加和删除服務。或者,您可以編輯/etc/firewalld/services/ 中的 XML 檔案。如果使用者沒有添加或更改服務,則在/etc/firewalld/services/ 中沒有找到對應的 XML 檔案。如果要添加或更改服

務, /usr/lib/firewalld/services/ 檔案可作為模闆使用。

注意

服務名稱必須是字母數字,以及 _ (下劃線)和 - (橫線)字元。

運作以下指令以添加新和空服務:

[[email protected] ~]# firewall-cmd --new-service=httptest --permanent 
success
           

更改服務設定後,就會将服務更新副本放入 /etc/firewalld/services/ 中。

[[email protected] ~]# ll /etc/firewalld/services/ 
total 4 
-rw-r--r--. 1 root root 60 Nov 9 14:06 httptest.xml
           

你也可以直接使用指令複制/usr/lib/firewalld/services目錄下的配置檔案到/etc/firewalld/services/目錄中進行配置。

firewalld 第一次從 /usr/lib/firewalld/services 加載檔案。如果檔案放在/etc/firewalld/services 中,且它們有效,就會覆寫 /usr/lib/firewalld/services 中比對的檔案。一旦删除了 /etc/firewalld/services 中的比對檔案,或者要求 firewalld 加載服務的預設值,則将使用 /usr/lib/firewalld/services 中的覆寫檔案。這隻适用于永久性環境。要在運作時環境中擷取這些回退,則需要重新載入。

繼續閱讀