參考文檔(寫的很詳細)
https://www.liquidweb.com/kb/an-introduction-to-firewalld/
實現過程
下面的命令將配置192.168.1.0/24
整個網段的IP允許訪問服務器的22
端口
// 先移除默認開啓的沒有訪問限制的ssh服務
# firewall-cmd --permanent --remove-service=ssh
// 添加複雜規則,只允許指定IP段訪問22端口
# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="22" accept'
// 使上面配置生效
# firewall-cmd --reload
// 查看當前配置信息
# firewall-cmd --list-all
備註:
source address
也可以設置爲單個IP地址,例如192.168.1.1
port
可以爲單個端口或端口範圍,例如1-10000
其他常用命令
刪除之前的複雜規則,這裏的內容需要與之前添加時的rule內容完全一致,可以複製粘貼過來
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="22" accept'
手動開放指定端口
firewall-cmd --permanent --add-port=3306/tcp
刪除開放的端口
firewall-cmd --permanent --remove-port=3306/tcp
開放指定服務(系統內置的)
firewall-cmd --permanent --add-service=http
刪除服務
firewall-cmd --permanent --remove-service=http
添加白名單地址(單IP)
firewall-cmd --permanent --add-source=192.168.1.100
注: 白名單中的IP可以任意訪問所有服務器可用的端口 這個白名單的作用不是這樣的,有待繼續研究
添加白名單地址(指定網絡段CIDR格式)
firewall-cmd --permanent --add-source=192.168.1.0/24
刪除白名單地址
firewall-cmd --permanent --remove-source=192.168.1.100
屏蔽指定IP地址
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.100' reject"
屏蔽IP地址段
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.0/24' reject"
手動編輯xml配置文件
除了上面的命令添加規則外,還可以直接編輯/etc/firewalld/zones/public.xml
文件(改完後記得執行firewall-cmd --reload
生效)