CentOS7使用firewalld打開(kāi)關(guān)閉防火墻與端口
1撬统、firewalld的基本使用
啟動(dòng): systemctl start firewalld
查看狀態(tài): systemctl status firewalld
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
2.systemctl是CentOS7的服務(wù)管理工具中主要的工具,它融合之前service和chkconfig的功能于一體敦迄。
啟動(dòng)一個(gè)服務(wù):systemctl start firewalld.service
關(guān)閉一個(gè)服務(wù):systemctl stop firewalld.service
重啟一個(gè)服務(wù):systemctl restart firewalld.service
顯示一個(gè)服務(wù)的狀態(tài):systemctl status firewalld.service
在開(kāi)機(jī)時(shí)啟用一個(gè)服務(wù):systemctl enable firewalld.service
在開(kāi)機(jī)時(shí)禁用一個(gè)服務(wù):systemctl disable firewalld.service
查看服務(wù)是否開(kāi)機(jī)啟動(dòng):systemctl is-enabled firewalld.service
查看已啟動(dòng)的服務(wù)列表:systemctl list-unit-files|grep enabled
查看啟動(dòng)失敗的服務(wù)列表:systemctl --failed
3.配置firewalld-cmd
查看版本: firewall-cmd --version
查看幫助: firewall-cmd --help
顯示狀態(tài): firewall-cmd --state
查看所有打開(kāi)的端口: firewall-cmd --zone=public --list-ports
更新防火墻規(guī)則: firewall-cmd --reload
查看區(qū)域信息: firewall-cmd --get-active-zones
查看指定接口所屬區(qū)域: firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:firewall-cmd --panic-on
取消拒絕狀態(tài): firewall-cmd --panic-off
查看是否拒絕: firewall-cmd --query-panic
那怎么開(kāi)啟一個(gè)端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效恋追,沒(méi)有此參數(shù)重啟后失效)
重新載入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
刪除
firewall-cmd --zone= public --remove-port=80/tcp --permanent、
- 添加防火墻規(guī)則(對(duì)指定ip開(kāi)放指定端口)
(1) Postgresql端口設(shè)置罚屋。允許192.168.142.166訪問(wèn)5432端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.166" port protocol="tcp" port="5432" accept"
(2)redis端口設(shè)置苦囱。允許192.168.142.166訪問(wèn)6379端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
(3)redis端口設(shè)置。允許192.168.1網(wǎng)段訪問(wèn)6379端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="6379" accept"
(4)重啟防火墻沿后,使配置生效
systemctl restart firewalld.service
- 刪除規(guī)則
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.1.166" port protocol="tcp" port="11300" accept"
systemctl restart firewalld.service