掌握 iptables 防火牆的永久與即時控制,以及端口開放配置,是 Linux 伺服器維運的基本功。
# 開啟防火牆
chkconfig iptables on
# 關閉防火牆
chkconfig iptables off
# 開啟防火牆
service iptables start
# 關閉防火牆
service iptables stop
上述指令模式適用於大多數 System V 風格服務管理。
編輯 /etc/sysconfig/iptables,在 RH-Firewall-1-INPUT 鏈中加入:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
分別開放 HTTP (80) 與 SSH (22)。修改後執行 service iptables restart 生效。生產環境建議僅開放必要端口,遵循最小權限原則。