CentOS 6/7 防火牆設定 | 開啟/關閉防火牆和埠
前言
CentOS 預設防火牆狀態是開啟的,所有埠狀態都是關閉的,無法直接使用。因此很多時候為了服務能正常使用,我們需要手動關閉防火牆,或者開啟指定防火牆埠以供使用。
CentOS 7 防火牆為 firewalld,舊版本的 CentOS 6 防火牆則是用的 iptables,因此操作指令也有所不同。
CentOS 開啟/關閉防火牆
CentOS 7
關閉防火牆
# 關閉 systemctl stop firewalld # 禁止開機自啟 sytemctl disable firewalld
開啟防火牆
# 開啟 systemctl start firewalld # 設定開機自啟 systemctl enable firewalld
檢視防火牆狀態
# 檢視狀態 firewall-cmd --state # 重啟 firewall-cmd --reload
CentOS 6
關閉防火牆
# 關閉 service iptables stop # 禁止開機自啟 chkconfig iptables off
開啟防火牆
# 開啟 service iptables start # 設定開機自啟 chkconfig iptables on
檢視防火牆狀態
# 檢視狀態 service iptables status # 重啟 service iptables restart
CentOS 開放/關閉防火牆埠
CentOS 7
開放/關閉指定埠
# 開啟 80 埠,協議 tcp,--permanent 永久生效 firewall-cmd --zone=public --add-port=80/tcp --permanent # 關閉埠 firewall-cmd --zone=public --remove-port=80/tcp --permanent # 檢視埠狀態 firewall-cmd --query-port=80/tcp
CentOS 6
開放/關閉指定埠
# 開啟 80 埠,協議 tcp iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 關閉埠 iptables -I INPUT -p tcp --dport 80 -j DROP # 儲存 service iptables save