Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

1. 安裝並清理衝突服務

firewalld 是預設包,需要時也可手動安裝:

yum install firewalld -y
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

由於與 iptables 服務衝突,必須先停掉並 mask 它:

systemctl status iptables
systemctl stop iptables
systemctl mask iptables
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

2. 認識 firewalld 的區域(Zone)

區域代表被信任或被拒絕的等級,用來套用於啟用的網路接口,每個區域可包含服務與端口。預設可用的區域如下:

drop:所有進入的封包直接丟棄,等同 iptables -j drop,不回應,只有對外的連接可用。

block:拒絕進入的連接並回送 icmp-host-prohibited,只有本機主動建立的連接被允許。

public:公共區域,只放行自訂的連接,僅特定端口開放,其餘丟棄。

external:為路由器場景設計,啟用偽裝(masquerade),未指定的連接一律丟棄。

dmz:若公眾需要訪問某些服務可定義於此,同樣只接受選擇的進入連接。

work:工作區,允許內部(私人)網絡流量。

home:家庭區,用來信任家裡的其他電腦,只允許選擇的進入連接。

internal:類似 work 區,選擇性允許連接。

trusted:信任區,所有流量全部接受。

firewall-cmd --get-zones        #列出可用區域
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則
firewall-cmd --get-default-zone #查看預設區域
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則
firewall-cmd --list-all-zones   #列出所有區域及其服務、端口、富規則

3. 設定預設區域

firewall-cmd --set-default-zone=internal
firewall-cmd --get-default-zone
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

查看某個網卡綁在哪個區域(此例為 enp0s3):

firewall-cmd --get-zone-of-interface=enp0s3
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

列出支援的 ICMP 類型:

firewall-cmd --get-icmptypes

4. 建立自訂服務

firewall-cmd --get-services     #列出可用服務
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

所有預設服務的定義檔都在這個目錄:

cd /usr/lib/firewalld/services/
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

自訂服務要放在 /etc/firewalld/services/。以新增 RTMP(1935 端口)為例,先複製一份現成服務:

cd /etc/firewalld/services/
cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
cd /etc/firewalld/services/
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

把 ssh.xml 改名為 rtmp.xml:

CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

編輯檔案中的標題、描述、協議與端口號:

CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

重載使其生效並確認:

firewall-cmd --reload
firewall-cmd --get-services
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

如何配置 firewalld

5. 分配服務到區域

firewall-cmd --state            #查看防火牆狀態
firewall-cmd --get-active-zones #查看活動區域
firewall-cmd --get-service      #列出 enp0s3 所在 public 區的可用服務

public 是預設區域,定義在 /etc/firewalld/firewalld.conf 的 defaultzone。

6. 在區域中增刪服務

firewall-cmd --add-service=rtmp              #添加(臨時)
firewall-cmd --zone=public --remove-service=rtmp   #移除
firewall-cmd --add-service=rtmp --permanent  #永久生效
firewall-cmd --reload

接著定義來源網段與開放端口,例如放行 192.168.0.0/24 與 1935/tcp:

firewall-cmd --permanent --add-source=192.168.0.0/24
firewall-cmd --permanent --add-port=1935/tcp
firewall-cmd --reload
firewall-cmd --list-all
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

7. 用富規則為指定網段放行多個服務

# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="http" accept'
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="http" accept' --permanent
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="https" accept'
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="https" accept' --permanent
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="vnc-server" accept'
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="vnc-server" accept' --permanent
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="postgresql" accept'
# firewall-cmd --add-rich-rule 'rule family="ipv4" source address="192.168.0.0/24" service name="postgresql" accept' --permanent

每條規則都要先執行一次立即生效,再帶 --permanent 存成永久規則,最後别忘了重載並檢查:

firewall-cmd --reload
firewall-cmd --list-all
CentOS 7.2 的 firewalld 防火牆詳解:區域、服務與富規則

想深入掌握的話,直接 man firewalld。

Copyright © Linux教程網 All Rights Reserved