FirewallD 詳解


在CentOS7開始,默認是沒有iptables的,而是使用了firewall防火牆.
與時俱進,簡單的整理了一下firewall的使用方法.
關於詳細的介紹參考官網,就不搬字了.這個網站有中文選項.可以直接看中文.關於CentOS7 非常多是資料這里面都能找到.
官方文檔地址:

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld

正文開始
FirewallD 提供了支持網絡/防火牆區域(zone)定義網絡鏈接以及接口安全等級的動態防火牆管理工具。

什么是動態防火牆?

我們首先需要弄明白的第一個問題是到底什么是動態防火牆。為了解答這個問題,我們先來回憶一下 iptables service 管理防火牆規則的模式:用戶將新的防火牆規則添加進  /etc/sysconfig/iptables 配置文件當中,再執行命令 service iptables reload 使變更的規則生效。在這整個過程的背后,iptables service 首先對舊的防火牆規則進行了清空,然后重新完整地加載所有新的防火牆規則,而如果配置了需要 reload 內核模塊的話,過程背后還會包含卸載和重新加載內核模塊的動作,而不幸的是,這個動作很可能對運行中的系統產生額外的不良影響,特別是在網絡非常繁忙的系統中。

如果我們把這種哪怕只修改一條規則也要進行所有規則的重新載入的模式稱為靜態防火牆的話,那么 firewalld 所提供的模式就可以叫做動態防火牆,它的出現就是為了解決這一問題,任何規則的變更都不需要對整個防火牆規則列表進行重新加載,只需要將變更部分保存並更新到運行中的 iptables 即可。

這里有必要說明一下 firewalld 和 iptables 之間的關系, firewalld 提供了一個 daemon 和 service,還有命令行和圖形界面配置工具,它僅僅是替代了 iptables service 部分,其底層還是使用 iptables 作為防火牆規則管理入口。firewalld 使用 python 語言開發,在新版本中已經計划使用 c++ 重寫 daemon 部分。

什么是區域(zone)?

firewalld將網卡對應到不同的區域(zone),zone 默認共有 9個,block dmz drop external home internal public trusted work.
不同的區域之間的差異是其對待數據包的默認行為不同,根據區域名字我們可以很直觀的知道該區域的特征,在CentOS7系統中,默認區域被設置為public.
在最新版本的fedora(fedora21)當中隨着 server 版和 workstation 版的分化則添加了兩個不同的自定義 zone FedoraServer 和 FedoraWorkstation 分別對應兩個版本。

使用下面的命令分別列出所有支持的 zone 和查看當前的默認 zone:

[root@test ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
[root@test ~]# firewall-cmd --get-default-zone
public

區域(zone)說明如下:
iptables service 在 /etc/sysconfig/iptables 中儲存配置
firewalld 將配置儲存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各種 XML 文件里

在/etc/firewalld/的區域設定是一系列可以被快速執行到網絡接口的預設定。列表並簡要說明如下:

drop(丟棄)
任何接收的網絡數據包都被丟棄,沒有任何回復。僅能有發送出去的網絡連接。
block(限制)
任何接收的網絡連接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒絕。
public(公共)
在公共區域內使用,不能相信網絡內的其他計算機不會對您的計算機造成危害,只能接收經過選取的連接。
external(外部)
特別是為路由器啟用了偽裝功能的外部網。您不能信任來自網絡的其他計算,不能相信它們不會對您的計算機造成危害,只能接收經過選擇的連接。
dmz(非軍事區)
用於您的非軍事區內的電腦,此區域內可公開訪問,可以有限地進入您的內部網絡,僅僅接收經過選擇的連接。
work(工作)
用於工作區。您可以基本相信網絡內的其他電腦不會危害您的電腦。僅僅接收經過選擇的連接。
home(家庭)
用於家庭網絡。您可以基本信任網絡內的其他計算機不會危害您的計算機。僅僅接收經過選擇的連接。
internal(內部)
用於內部網絡。您可以基本上信任網絡內的其他計算機不會威脅您的計算機。僅僅接受經過選擇的連接。
trusted(信任)
可接受所有的網絡連接。
指定其中一個區域為默認區域是可行的。當接口連接加入了 NetworkManager,它們就被分配為默認區域。安裝時,firewalld 里的默認區域被設定為公共區域。

什么是服務?

在  /usr/lib/firewalld/services/ 目錄中,還保存了另外一類配置文件,每個文件對應一項具體的網絡服務,如 ssh 服務等.
與之對應的配置文件中記錄了各項服務所使用的 tcp/udp 端口,在最新版本的 firewalld 中默認已經定義了 70+ 種服務供我們使用.
當默認提供的服務不夠用或者需要自定義某項服務的端口時,我們需要將 service 配置文件放置在 /etc/firewalld/services/ 目錄中.
service 配置的好處顯而易見:
第一,通過服務名字來管理規則更加人性化,
第二,通過服務來組織端口分組的模式更加高效,如果一個服務使用了若干個網絡端口,則服務的配置文件就相當於提供了到這些端口的規則管理的批量操作快捷方式。

每加載一項 service 配置就意味着開放了對應的端口訪問,使用下面的命令分別列出所有支持的 service 和查看當前 zone 種加載的 service:

[root@test ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
[root@test ~]# firewall-cmd --list-services
dhcpv6-client ssh

動態添加一條防火牆規則如下:
假設自定義的 ssh 端口號為 12222,使用下面的命令來添加新端口的防火牆規則:

firewall-cmd  --add-port=12222/tcp --permanent

如果需要使規則保存到 zone 配置文件,則需要加參數 –permanent
舉例如下:

[root@test zones]# firewall-cmd --add-port=12222/tcp
success
[root@test zones]# 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"/>
</zone>
[root@test zones]# firewall-cmd --add-port=12222/tcp --permanent
success
[root@test zones]# 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"/>
<port protocol="tcp" port="12222"/>
</zone>

# 注意:防火牆配置文件也可以手動修改,修改后記得重載,重載方法請看下文.

#####firewalld命令#####

# 關閉firewalld
[root@test zones]# systemctl stop firewalld.service

# 啟動firewalld
[root@test zones]# systemctl start firewalld.service

# 把firewalld加入到系統服務
[root@test zones]# systemctl enable firewalld.service

# 從系統服務移除
[root@test zones]# systemctl disable firewalld.service
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'

# 查看firewalld狀態 兩種方法2選1即可
[root@test zones]# firewall-cmd --state
running
[root@test zones]# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
Active: active (running) since Tue 2015-10-06 11:49:06 CST; 2s ago
Main PID: 21716 (firewalld)
CGroup: /system.slice/firewalld.service
└─21716 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Oct 06 11:49:06 test systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 06 11:49:06 test systemd[1]: Started firewalld - dynamic firewall daemon.

# 重讀防火牆
# 以 root 身份輸入以下命令,重新加載防火牆,並不中斷用戶連接,即不丟失狀態信息:

[root@test ~]# firewall-cmd --reload
success

# 以 root 身份輸入以下信息,重新加載防火牆並中斷用戶連接,即丟棄狀態信息:

[root@test ~]# firewall-cmd --complete-reload
success

# 注意:通常在防火牆出現嚴重問題時,這個命令才會被使用。比如,防火牆規則是正確的,但卻出現狀態信息問題和無法建立連接。

# 獲取支持的區域(zone)列表

[root@test zones]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

# 獲取所有支持的服務

[root@test zones]# firewall-cmd --get-services
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https

# 獲取所有支持的ICMP類型

[root@test zones]# firewall-cmd --get-icmptypes
destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded

# 列出全部啟用的區域的特性

[root@test zones]# 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

[root@test zones]# firewall-cmd --list-all
public (default, active)
interfaces: eno16777736
sources:
services: dhcpv6-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
[root@test zones]# firewall-cmd --zone=work --list-all
work
interfaces:
sources:
services: dhcpv6-client ipp-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:

# 獲取默認區域的網絡設置

[root@test zones]# firewall-cmd --get-default-zone
public

# 設置默認區域

[root@test zones]# firewall-cmd --set-default-zone=work
success

# 注意:流入默認區域中配置的接口的新訪問請求將被置入新的默認區域。當前活動的連接將不受影響。

# 獲取活動的區域

[root@test zones]# firewall-cmd --get-active-zones
work
interfaces: eno16777736

# 根據接口獲取區域
firewall-cmd –get-zone-of-interface=<interface>

[root@test zones]# firewall-cmd --get-zone-of-interface=eno16777736
public

## 以下關於區域和接口的操作,就不一一舉例了,可以根據實際情況修改.
# 將接口增加到區域

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

# 查詢應急模式

firewall-cmd  --query-panic

# 啟用區域中的一種服務

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>

##########

#####通過配置文件來使用Firewalld的方法#####
系統本身已經內置了一些常用服務的防火牆規則,存放在/usr/lib/firewalld/services/
注意!
請勿編輯/usr/lib/firewalld/services/ ,只有 /etc/firewalld/services 的文件可以被編輯。

[root@test ~]# ls /usr/lib/firewalld/services/
amanda-client.xml dhcpv6.xml high-availability.xml ipp-client.xml kpasswd.xml libvirt.xml mysql.xml pmcd.xml pop3s.xml RH-Satellite-6.xml smtp.xml tftp.xml
bacula-client.xml dhcp.xml https.xml ipp.xml ldaps.xml mdns.xml nfs.xml pmproxy.xml postgresql.xml rpc-bind.xml ssh.xml transmission-client.xml
bacula.xml dns.xml http.xml ipsec.xml ldap.xml mountd.xml ntp.xml pmwebapis.xml proxy-dhcp.xml samba-client.xml telnet.xml vnc-server.xml
dhcpv6-client.xml ftp.xml imaps.xml kerberos.xml libvirt-tls.xml ms-wbt.xml openvpn.xml pmwebapi.xml radius.xml samba.xml tftp-client.xml wbem-https.xml

以下例子均以系統自帶的public zone 為例子.

1. 如果想開放80端口供外網訪問http服務,例子如下

1.1 將 http.xml復制到/etc/firewalld/services/下面,以服務形式管理防火牆,
# 這個cp命令其實是可以省略的,系統會優先去讀取 /etc/firewalld 里面的文件,讀取完畢后,會去/usr/lib/firewalld/services/ 再次讀取.為了方便修改和管理,建議復制到/etc/firewalld

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

1.2 修改public.xml,加入http服務

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>

1.3 重新載入 兩條命令都可以的

以 root 身份輸入以下命令,重新加載防火牆,並不中斷用戶連接,即不丟失狀態信息:

[root@test ~]# firewall-cmd --reload
success

以 root 身份輸入以下信息,重新加載防火牆並中斷用戶連接,即丟棄狀態信息:

[root@test ~]# firewall-cmd --complete-reload
success

注意:通常在防火牆出現嚴重問題時,這個命令才會被使用。比如,防火牆規則是正確的,但卻出現狀態信息問題和無法建立連接。

2. 修改防火牆ssh的端口方法
2.1 復制ssh.xml文件到/etc/firewalld/services/

[root@test ~]# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

2.2 修改ssh.xml文件 12222為ssh端口

[root@test ~]# 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>

2.3 重新載入 兩條命令都可以的

以 root 身份輸入以下命令,重新加載防火牆,並不中斷用戶連接,即不丟失狀態信息:

[root@test ~]# firewall-cmd --reload
success

以 root 身份輸入以下信息,重新加載防火牆並中斷用戶連接,即丟棄狀態信息:

[root@test ~]# firewall-cmd --complete-reload
success

注意:通常在防火牆出現嚴重問題時,這個命令才會被使用。比如,防火牆規則是正確的,但卻出現狀態信息問題和無法建立連接。

3. 指定ip訪問ssh端口
3.1 修改/etc/firewalld/zones/

# rule字段內為要限制的ip和使用的服務

[root@test ~]# 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>

3.3 重新載入 兩條命令都可以的

以 root 身份輸入以下命令,重新加載防火牆,並不中斷用戶連接,即不丟失狀態信息:

[root@test ~]# firewall-cmd --reload
success

以 root 身份輸入以下信息,重新加載防火牆並中斷用戶連接,即丟棄狀態信息:

[root@test ~]# firewall-cmd --complete-reload
success

注意:通常在防火牆出現嚴重問題時,這個命令才會被使用。比如,防火牆規則是正確的,但卻出現狀態信息問題和無法建立連接。

4. 添加自定義服務 舉例,添加8080端口為新的Service
4.1 添加新文件 8080.xml

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>

4.2 編輯public.xml 文件,加入相應的Server

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>

4.3 重新載入 兩條命令都可以的

以 root 身份輸入以下命令,重新加載防火牆,並不中斷用戶連接,即不丟失狀態信息:

[root@test ~]# firewall-cmd --reload
success

以 root 身份輸入以下信息,重新加載防火牆並中斷用戶連接,即丟棄狀態信息:

[root@test ~]# firewall-cmd --complete-reload
success

注意:通常在防火牆出現嚴重問題時,這個命令才會被使用。比如,防火牆規則是正確的,但卻出現狀態信息問題和無法建立連接。

#####

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM