2018年12月14日 15:47:45 忒斯特 閱讀數:69網絡
Centos7開始已經放棄iptables,轉而使用firewalld。從本質意義上講,iptables和firewalld是防火牆軟件,其實現方式都是調用內核Netfilter。firewalld提供了一個動態管理的防火牆,造成網絡「zones」規則集,具有支持ipv4和ipv6的能力。firewalld程序提供了圖形化的配置工具firewall-confighe、system-config-firewall和命令行firewall-cmd,用於配置firewalld永久性或非永久性規則。tcp
啓動服務:systemctl start firewalld工具
關閉服務:systemctl stop firewalldspa
重啓服務:systemctl restart firewalld.net
查看服務狀態:systemctl status firewalld命令行
開機自啓服務:systemctl enable firewalldrest
開機禁用服務:systemctl disable firewalld接口
查看是否開機自啓:systemctl is-enable firewalldip
PS:systemctl常見其餘命令:rem
查看已啓動的服務列表:systemctl list-unit-files | grep enabled
查看啓動失敗的服務列表:systemctl --failed
查看版本:firewall-cmd --version
查看幫助:firewall-cmd --help
查看狀態:firewall-cmd --state
查看全部打開的端口:firewalld-cmd --zone=public --list-ports
查看全部規則:firewall-cmd --list-all
重載規則:firewall-cmd --reload
查看區域信息:firewall-cmd --get-active-zones
查看指定接口所屬區域: firewall-cmd --get-zone-of-interface=enp4s0
拒絕全部包:firewall-cmd --panic-on
取消拒絕全部包: firewall-cmd --panic-off
查看是否拒絕: firewall-cmd --query-panic
添加端口:firewall-cmd --add-port=80/tcp --permanent
移除端口:firewall-cmd --remove-port=80/tcp --permanent
查看端口狀態:firewall-cmd --zone=public --query-port=80/tcp
(PS:添加或刪除端口後不會當即生效,配置完 --reload後才能生效;--permanent表明永久生效)
開啓防火牆假裝:firewall-cmd --add-masquerade --permanent //開啓後才能轉發端口
添加轉發規則:firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.1 --permanent
(PS:此規則將本機80端口轉發到192.168.1.1的8080端口上,配置完--reload才生效)
若是配置完以上規則後仍不生效,檢查防火牆是否開啓80端口,若是80端口已開啓,仍沒法轉發,多是因爲內核參數文件sysctl.conf未配置ip轉發功能,具體配置以下:
vi /etc/sysctl.conf
在文本內容中添加:net.ipv4.ip_forward = 1
保存文件後,輸入命令sysctl -p生效