當使用firewalld-cmd添加防火墻規則時,它實際上是將配置轉換成iptables規則后,再應用到系統中。
設有如下網絡拓撲:
內網internal (172.12.0.100)br1--linux---br0---external(10.10.11.250)
1. 查詢系統中的zone信息
firewall-cmd --get-zones
block dmz drop external home internal public trusted work
zone:它是安全域的范圍,就類似于Window上的域網絡,工作網絡,家庭網絡,Internet網絡等,不同的安全作用域其安全級別不同,安全程度不同,家庭zone的安全規則就是最寬松的。
- trusted(信任) ---可接受所有的網絡連
- home(家庭) ---- 用于家庭網路,默認僅接受ssh, mDNS, ipp-client,dhcpv6-client服務連
- internal(內部) --- 用于內部網絡,默認僅接受ssh, mdns, ipp-client,dhcpv6-client服務連
- work(工作) -- 用于工作區域,默認僅接受ssh, dhcpv6-client服務連
- public(公共) --- 在公共區域使用,默認僅接受ssh,dhcpv6-client服務連,為firewalld的默認區域
- external(外部) -- 出去的IPv4網絡連,通過此區域或偽裝(source nat)或是轉發,僅接受ssh服務連接
- dmz(非軍事區) --- 僅接受ssh服務連
- block(限制) ---拒絕所有的網絡連接
- drop(丟棄) ---任何接收的網絡數據包都被丟棄,沒有任何回復
2. 查詢防火墻狀態和配置信息
Firewall運行狀態
[root@localhost ~]# firewall-cmd --state
Running
Firewall接口分配信息
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0 ens4
….
開放端口信息
[root@localhost ~]# firewall-cmd --list-port
500/udp 4500/udp
3. 更改接口所屬區域
當前接口所屬區域
firewall-cmd --get-active-zones
public
interfaces: eth0 ens4
更改接口區域
firewall-cmd --permanent --zone=external --change-interface=eth0
firewall-cmd --permanent --zone=internal --change-interface=ens4
更改后的區域歸屬
firewall-cmd --get-active-zones
internal
interfaces: ens4
external
interfaces: eth0
4. 區域中添加、刪除接口
將接口加入到區域中
firewall-cmd --permanent --zone=internal --add-interface=eth0
將接口從區域中刪除
firewall-cmd --permanent --zone=internal --remove-interface=eth0
參數permanent指的是配置將永久有效
5. 添加、刪除服務或端口
在公共區域添加郵件服務
firewall-cmd --permanent --zone=public --add-service=smtp
在公共區域刪除郵件服務
firewall-cmd --permanent --zone=public --remove-service=smtp
在所有區域添加、刪除udp端口67
firewall-cmd –permanent –add-port=67/udp
firewall-cmd –permanent -remove-port=67/udp
6. 設置、刪除網絡地址到指定的區域
firewall-cmd --permanent --zone=internal --add-source=172.12.0.0/24
firewall-cmd --permanent --zone=internal --remove-source=172.12.0.0/24
7. 添加、刪除DNAT轉發
在本機所有端口上接收到TCP 4522的報文時,將報文的目的地址和端口轉換為內部網絡的172.12.0.11:22端口
firewall-cmd --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
刪除DNAT規則
firewall-cmd --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 --permanent
添加DNAT規則到區域
firewall-cmd --zone=internal --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
從區域中刪除DNAT規則
firewall-cmd --zone=internal --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
8. SNAT規則添加、刪除
設置IP地址偽裝(SNAT)
firewall-cmd --zone=external --add-masquerade –permanent
刪除SNAT規則
firewall-cmd --zone=external --remove-masquerade –permanent
將source為192.168.2.0網段來的數據包偽裝成external(即ens4)的地址
firewall-cmd --permanent --zone=external --add-rich-rule=‘rule family=ipv4 source address=192.168.2.0/24 masquerade’
將source為192.168.2.0網段來的數據包偽裝成external(即ens4)地址的規則移除
firewall-cmd --permanent --zone=external --remove-rich-rule=‘rule family=ipv4 source address=192.168.2.0/24 masquerade’
9. 查詢端口是否打開
firewall-cmd --query-port=80/tcp
10. 重載Firewall使配置生效
firewall-cmd --reload