Firewalld-端口轉發-刪除-配置

基本介紹

Centos7之後,棄用Iptables,不過firewall也更加舒服。
推薦使用~

服務配置

啓動服務:systemctl start firewalld

關閉服務:systemctl stop firewalld

重啓服務:systemctl restart firewalld

查看服務狀態:systemctl status firewalld

開機自啓服務:systemctl enable firewalld

開機禁用服務:systemctl disable firewalld

查看是否開機自啓:systemctl is-enable firewalld

PS:systemctl常見其他命令:

查看已啓動的服務列表:systemctl list-unit-files | grep enabled

查看啓動失敗的服務列表:systemctl --failed

規則配置

查看版本:firewall-cmd --version

查看幫助:firewall-cmd --help

查看狀態:firewall-cmd --state

查看所有打開的端口:firewall-cmd --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

端口轉發

開啓防火牆僞裝:firewall-cmd --add-masquerade --permanent //開啓後才能轉發端口
將本機80端口轉發到192.168.1.1的8080端口上
添加轉發規則:firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.1 --permanent

刪除轉發規則:firewall-cmd --remove-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.1 --permanent

可能的錯誤

如果配置完以上規則後仍不生效,可能是由於內核參數文件sysctl.conf未配置ip轉發功能,具體配置如下:
vi /etc/sysctl.conf
在文本內容中添加:net.ipv4.ip_forward = 1
保存文件後,輸入命令sysctl -p生效

執行完所有的命令,都需要reload,不然不生效。
firewall-cmd --reload

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章