CentOS7默認防火牆 firewalld簡單使用
特點
- firewalld對比 iptables優點是支持動態更新, 每次更改規則可以不重啓服務
- 加入了
zone
(域)的概念 - 與 iptables相同之處是都是使用 Linux內核上的 netfilter來實現防火牆的
安裝
- yum install firewalld
啓動
- systemctl start firewalld 或 systemctl start firewalld.service
關閉
- systemctl stop firewalld 或 systemctl stop firewalld.service
重啓
- systemctl restart firewalld 或 systemctl restart firewalld.service
查看狀態
- systemctl status firewalld 或 systemctl status firewalld.service
開機啓用
- systemctl enable firewalld 或 systemctl enable firewalld.service
開機禁用
- systemctl disable firewalld 或 systemctl disable firewalld.service
firewall-cmd命令
常用參數 | 說明 |
---|---|
--help |
查看幫助 |
--state |
查看狀態 |
--reload |
不中斷服務的重新加載 |
--complete-reload |
中斷服務的重新加載 |
--runtime-to-permanent |
將當前防火牆的規則永久保存 |
--get-zones |
查看所有的域 block dmz drop external home internal public trusted work |
--zone |
--zone=<zone> 指定域 |
--get-active-zones |
查看當前活動域 |
--get-default-zone |
查看默認域 |
--set-default-zone |
設置默認域 |
--list-ports |
查看所有打開的端口 |
--add-port |
--add-port=<portid>[-<portid>]/<protocol> 指定要打開的端口和協議 |
--remove-port |
--remove-port=<portid>[-<portid>]/<protocol> 刪除已打開的端口和協議 |
--query-port |
--query-port=<portid>[-<portid>]/<protocol> 查看指定的端口和協議是否存在 |
--list-all |
查看 --zone 域的相關信息 |
--permanent |
永久保存, 無此參數重啓後失效 |
--direct |
直接模式, 可以使用iptables, ip6tables中的規則進行配置 |
--get-all-rules |
查看一配置的所有規則 |
實例
查看指定 public域的所有打開的端口
- firewall-cmd --zone=public --list-ports
打開[協議 tcp的80端口]同時永久保存
- firewall-cmd --zone=public --add-port=80/tcp --permanent
刪除配置中的[協議 tcp的80端口]同時永久保存
- firewall-cmd --zone=public --remove-port=80/tcp --permanent
查看通過直接模式配置的所有規則
- firewall-cmd --direct --get-all-rules
拒絕所有 IP訪問22端口
- firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -j REJECT
指定允許的 IP:192.168.2.25訪問 端口:22
- firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -s 192.168.2.25 -j ACCEPT
拒絕單一的 IP:192.168.2.26訪問 端口:22
- firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -s 192.168.2.26 -j REJECT
指定刪除已配置的規則
- firewall-cmd --direct --remove-rule ipv4 filter INPUT 0 -p tcp --dport 22 -s 192.168.2.26 -j REJECT
如果您覺得有幫助,歡迎點贊哦 ~ 謝謝!!