在 CentOS 7.9 中,默认使用的防火墙服务是firewalld,关闭它分为临时关闭 (重启后恢复)和永久关闭(重启后不自动启动)两种方式,操作如下:
1. 临时关闭 firewalld(即时生效,重启失效)
# 停止防火墙服务
systemctl stop firewalld
2. 永久关闭 firewalld(重启后不自动启动)
# 禁用防火墙开机自启
systemctl disable firewalld
3. 同时执行临时 + 永久关闭(推荐)
systemctl stop firewalld && systemctl disable firewalld
4. 验证防火墙状态
执行以下命令,若输出inactive (dead)则表示已关闭:
systemctl status firewalld
重要注意事项
-
安全风险 :直接关闭防火墙会让服务器暴露在公网中,所有端口都对外开放,生产环境不建议直接关闭,优先选择只放行必要端口(如 80、443、22)。
-
若后续需要重新开启防火墙,执行:
systemctl start firewalld && systemctl enable firewalld
验证外网可访问性
若需要从远程访问 Nginx,需确认防火墙放行 80 端口(HTTP 默认端口):
# 永久开放 80 端口
firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙生效
firewall-cmd --reload
# 查看已开放端口
firewall-cmd --list-ports
之后在本地浏览器输入http://服务器公网IP,若看到 Nginx 默认欢迎页面,说明访问正常。