一、簡介
firewall-cmd 是firewalld服務的一個命令行客戶端,提供了對防火牆規則的增刪查改。firewalld自身並不具備防火牆的功能。它和iptables一樣需要通過內核netfilter來實現防火牆的功能,相對於iptables來說firewall-cmd使用起來更簡單。
二、防火牆的啓動停止(centos7以上)
systemctl status firewalld.service #查看防火牆狀態
systemctl start firewalld.service #啓動防火牆
systemctl stop firewalld.service #停止防火牆
systemctl restart firewalld.service #重新啓動防火牆
systemctl enable firewalld.service #設置防火牆開機自啓動
systemctl disable firewalld.service #禁止防火牆開機自啓動
二、firewall-cmd參數介紹
1.常規參數
-h,--help #顯示幫助文檔
--version #顯示版本信息
-q,--quiet #不打印狀態信息
2. 狀態參數
--state #檢查firewalld服務運行狀態
--reload #不中斷服務重新加載防火牆規則
--complete-reload #中斷所有連接重新加載防火牆規則
--runtime-to-permanent #將當前防火牆的規則永久保存
--check-config # 檢查配置正確性
--get-log-denied #獲取被拒絕的日誌,日誌沒打開則爲off
#設置記錄被拒絕的日誌,只能爲‘all’,‘unicast’,‘broadcast’,‘multicast’,‘off’ 其中的一個;
-–set-log-denied=off
三、firewall-cmd 使用實例
firewall-cmd 命令的參數實在是太多了,下面只列出了一些常用到的功能,後面如果用到了新的再來補充吧!
更詳細的介紹可以看官方文檔:https://firewalld.org/documentation/man-pages/firewall-cmd
1、 查看開放的端口
firewall-cmd --list-ports
2、開放端口
#將端口添加到域 public,重啓防火牆後失效
firewall-cmd --zone=public --add-port=3456/tcp # tcp 可以是 {'tcp'|'udp'|'sctp'|'dccp'}中的一個
#將端口添加到域 public,想要永久生效,添加 --permanent 參數
firewall-cmd --zone=public --add-port=3456/tcp --permanent
3、關閉開放的端口
#將端口從域 public 中刪除,重啓防火牆後失效
firewall-cmd --zone=public --remove-port=3456/tcp
#將端口從域 public 中刪除,想要永久生效,添加 --permanent 參數
firewall-cmd --zone=public --remove-port=3456/tcp --permanent
4、設置域
# 設置默認接口區域,立即生效無需重啓
firewall-cmd --set-default-zone=public