當(dāng)使用firewalld-cmd添加防火墻規(guī)則時(shí),它實(shí)際上是將配置轉(zhuǎn)換成iptables規(guī)則后,再應(yīng)用到系統(tǒng)中。
設(shè)有如下網(wǎng)絡(luò)拓?fù)洌?/p>
內(nèi)網(wǎng)internal (172.12.0.100)br1--linux---br0---external(10.10.11.250)
1. 查詢系統(tǒng)中的zone信息
firewall-cmd --get-zones
block dmz drop external home internal public trusted work
zone:它是安全域的范圍,就類似于Window上的域網(wǎng)絡(luò),工作網(wǎng)絡(luò),家庭網(wǎng)絡(luò),Internet網(wǎng)絡(luò)等,不同的安全作用域其安全級(jí)別不同,安全程度不同,家庭zone的安全規(guī)則就是最寬松的。
- trusted(信任) ---可接受所有的網(wǎng)絡(luò)連
- home(家庭) ---- 用于家庭網(wǎng)路,默認(rèn)僅接受ssh, mDNS, ipp-client,dhcpv6-client服務(wù)連
- internal(內(nèi)部) --- 用于內(nèi)部網(wǎng)絡(luò),默認(rèn)僅接受ssh, mdns, ipp-client,dhcpv6-client服務(wù)連
- work(工作) -- 用于工作區(qū)域,默認(rèn)僅接受ssh, dhcpv6-client服務(wù)連
- public(公共) --- 在公共區(qū)域使用,默認(rèn)僅接受ssh,dhcpv6-client服務(wù)連,為firewalld的默認(rèn)區(qū)域
- external(外部) -- 出去的IPv4網(wǎng)絡(luò)連,通過此區(qū)域或偽裝(source nat)或是轉(zhuǎn)發(fā),僅接受ssh服務(wù)連接
- dmz(非軍事區(qū)) --- 僅接受ssh服務(wù)連
- block(限制) ---拒絕所有的網(wǎng)絡(luò)連接
- drop(丟棄) ---任何接收的網(wǎng)絡(luò)數(shù)據(jù)包都被丟棄,沒有任何回復(fù)
2. 查詢防火墻狀態(tài)和配置信息
Firewall運(yùn)行狀態(tài)
[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. 更改接口所屬區(qū)域
當(dāng)前接口所屬區(qū)域
firewall-cmd --get-active-zones
public
interfaces: eth0 ens4
更改接口區(qū)域
firewall-cmd --permanent --zone=external --change-interface=eth0
firewall-cmd --permanent --zone=internal --change-interface=ens4
更改后的區(qū)域歸屬
firewall-cmd --get-active-zones
internal
interfaces: ens4
external
interfaces: eth0
4. 區(qū)域中添加、刪除接口
將接口加入到區(qū)域中
firewall-cmd --permanent --zone=internal --add-interface=eth0
將接口從區(qū)域中刪除
firewall-cmd --permanent --zone=internal --remove-interface=eth0
參數(shù)permanent指的是配置將永久有效
5. 添加、刪除服務(wù)或端口
在公共區(qū)域添加郵件服務(wù)
firewall-cmd --permanent --zone=public --add-service=smtp
在公共區(qū)域刪除郵件服務(wù)
firewall-cmd --permanent --zone=public --remove-service=smtp
在所有區(qū)域添加、刪除udp端口67
firewall-cmd –permanent –add-port=67/udp
firewall-cmd –permanent -remove-port=67/udp
6. 設(shè)置、刪除網(wǎng)絡(luò)地址到指定的區(qū)域
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轉(zhuǎn)發(fā)
在本機(jī)所有端口上接收到TCP 4522的報(bào)文時(shí),將報(bào)文的目的地址和端口轉(zhuǎn)換為內(nèi)部網(wǎng)絡(luò)的172.12.0.11:22端口
firewall-cmd --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
刪除DNAT規(guī)則
firewall-cmd --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 --permanent
添加DNAT規(guī)則到區(qū)域
firewall-cmd --zone=internal --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
從區(qū)域中刪除DNAT規(guī)則
firewall-cmd --zone=internal --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent
8. SNAT規(guī)則添加、刪除
設(shè)置IP地址偽裝(SNAT)
firewall-cmd --zone=external --add-masquerade –permanent
刪除SNAT規(guī)則
firewall-cmd --zone=external --remove-masquerade –permanent
將source為192.168.2.0網(wǎng)段來的數(shù)據(jù)包偽裝成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網(wǎng)段來的數(shù)據(jù)包偽裝成external(即ens4)地址的規(guī)則移除
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