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

CentOS 7 重大變革:firewalld 取代 iptables 服務

Fedora 18 起以 firewalld 取代原本的 iptables 服務,而 RHEL 7 與 Oracle Linux 7 都源自 Fedora 19,這項轉換因此成為企業級 Linux 的標準配備。

CentOS 7 重大變革:firewalld 取代 iptables 服務

動手前要先分清兩件事:iptables 服務與iptables 指令。firewalld 取代的是「服務」那一層的防火牆管理機制,但它對核心 netfilter 的溝通仍然是呼叫 iptables 指令。所以被換掉的是服務,不是指令——這個區別一開始很容易搞混。

CentOS 7 重大變革:firewalld 取代 iptables 服務

一、退回 iptables 服務

若暫時不想改用 firewalld:

systemctl stop firewalld
systemctl disable firewalld

yum install iptables-services
touch /etc/sysconfig/iptables
systemctl start iptables
systemctl enable iptables

touch /etc/sysconfig/ip6tables
systemctl start ip6tables
systemctl enable ip6tables

之後的防火牆管理就回到熟悉的 iptables 服務模式。

二、安裝與啟動

yum install firewalld firewall-config
systemctl start firewalld.service
systemctl enable firewalld.service
systemctl status firewalld

要停用則是 systemctl stop/disable firewalld.service。

三、圖形介面 firewall-config

可從選單進入(Fedora 在「系統 → 管理 → 防火牆」,RHEL7/OL7 在「應用程式 → 其他 → 防火牆」),或直接下指令:

yum install firewall-config
firewall-config

介面左上角的「設定」下拉可切換 Runtime(目前執行中)與 Permanent(開機後生效)。勾選 SSH、FTP、HTTP 等基本服務即可放行, Ports 分頁則用來手動開啟未被內建服務涵蓋的埠號。要留意 Runtime 的變更重開機後會消失,不確定時就一律改 Permanent,再用「選項 → 重新載入 firewalld」套用。

四、指令 firewall-cmd

firewall-cmd --help
firewall-cmd --state              # 目前狀態
firewall-cmd --get-active-zones   # 使用中的 zone
firewall-cmd --get-services       # 生效中的服務
firewall-cmd --get-services --permanent  # 重載後會生效的服務
firewall-cmd --panic-on           # 全封
firewall-cmd --query-panic
firewall-cmd --panic-off
firewall-cmd --reload             # 以 permanent 設定重載 runtime

預定義服務以 XML 檔放在 /usr/lib/firewalld/services/,不建議直接改;正確做法是把要客製的服務檔複製到 /etc/firewalld/services/ 再編輯,firewalld 會優先採用 etc 下的版本,改完記得 reload。

放行與移除服務:

firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
firewall-cmd --zone=public --list-services
firewall-cmd --permanent --zone=public --remove-service=https

開關埠號(含範圍):

firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
firewall-cmd --permanent --zone=public --remove-port=8080-8081/tcp

更複雜的規則用 rich rule,例如只對特定來源開放 HTTP:

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.0.4/24" service name="http" accept"

此時 /etc/firewalld/zones/public.xml 會多出一段 rule 節點;移除用 --remove-rich-rule,或直接編輯 XML 後 reload。指定來源開放單一埠也是同樣寫法,把 service 換成 port protocol="tcp" port="8080"。

五、備份與搬遷

所有非預設設定都落在 /etc/firewalld/ 之下,把這個目錄連同子目錄打包就是一份完整備份;把它複製到另一台機器,即可複製整套防火牆設定。

Copyright © Linux教程網 All Rights Reserved