在 CentOS 中,防火墙的配置位置和方式取决于系统版本:
/etc/firewalld/
主要配置文件:
/etc/firewalld/firewalld.conf(主配置)/etc/firewalld/zones/(区域配置,如 public.xml)# 查看防火墙状态
systemctl status firewalld
# 启动 / 停止 / 开机自启
systemctl start firewalld
systemctl stop firewalld
systemctl enable firewalld
# 查看已开放端口
firewall-cmd --list-ports
# 开放端口(如 80)
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
# 移除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --reload
/etc/sysconfig/iptables
service iptables status
service iptables start
service iptables stop
修改规则后需重启:
service iptables restart
如果禁用了 firewalld,可直接用:
iptables -L -n
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
firewalld(配置文件在 /etc/firewalld/)iptables(配置文件在 /etc/sysconfig/iptables)如果你是 服务器环境 / 云服务器,还要注意 安全组规则 也会影响端口访问。