上一篇 下一篇 分享链接 返回 返回顶部

Centos操作系统关闭防火墙、添加防火墙例外

发布人:破碎工坊云计算 发布时间:3 天前 阅读量:17
在 CentOS 系统中,防火墙管理主要通过 firewalld 服务(CentOS 7 及以上默认)或 iptables 实现。以下是关闭防火墙和添加防火墙例外(开放端口 / 服务)的详细操作方法:

一、查看防火墙状态

首先确认当前防火墙状态,避免重复操作:

 
 
# 查看firewalld状态(CentOS 7/8默认)
sudo systemctl status firewalld

# 若显示 "active (running)" 表示防火墙正在运行;"inactive (dead)" 表示已关闭
 

二、关闭防火墙(临时 / 永久)

1. 临时关闭防火墙(立即生效,重启后恢复)

适用于临时测试场景,重启服务器后防火墙会自动开启:

sudo systemctl stop firewalld
 

2. 永久关闭防火墙(重启后仍保持关闭)

若需长期关闭防火墙(不推荐,存在安全风险),需同时禁用开机自启:

 
 
# 停止当前运行的防火墙
sudo systemctl stop firewalld
# 禁用开机自启
sudo systemctl disable firewalld

# 验证是否禁用成功(输出中含 "disabled" 即表示成功)
sudo systemctl is-enabled firewalld
 

三、添加防火墙例外(开放端口 / 服务)

不建议直接关闭防火墙,更安全的方式是开放所需端口或服务。以下是常用操作:

1. 开放指定端口(如 80、443、22 等)

(1)临时开放端口(重启防火墙后失效)
 
 
# 开放TCP端口(如80端口)
sudo firewall-cmd --add-port=80/tcp

# 开放UDP端口(如53端口)
sudo firewall-cmd --add-port=53/udp

# 开放端口范围(如1000-2000)
sudo firewall-cmd --add-port=1000-2000/tcp
 
(2)永久开放端口(重启后仍有效)
需添加 --permanent 参数,且需重新加载防火墙规则生效:

 
 
# 永久开放TCP 80端口
sudo firewall-cmd --permanent --add-port=80/tcp

# 永久开放UDP 53端口
sudo firewall-cmd --permanent --add-port=53/udp

# 重新加载规则(使永久配置生效)
sudo firewall-cmd --reload
 

2. 开放指定服务(如 HTTP、SSH 等)

firewalld 预设了常见服务的配置(如 http 对应 80 端口,ssh 对应 22 端口),可直接通过服务名开放:

 
 
# 临时开放HTTP服务(80端口)
sudo firewall-cmd --add-service=http

# 永久开放SSH服务(22端口)
sudo firewall-cmd --permanent --add-service=ssh

# 重新加载规则
sudo firewall-cmd --reload
 

3. 开放指定 IP 地址的访问

仅允许特定 IP(如 192.168.1.100)访问服务器的所有端口或指定端口:

 
# 允许192.168.1.100访问所有端口(临时)
sudo firewall-cmd --add-source=192.168.1.100

# 允许192.168.1.100访问80端口(永久)
sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.100' port protocol='tcp' port='80' accept"

# 重新加载规则
sudo firewall-cmd --reload
 

4. 查看已开放的端口 / 服务

 
 
# 查看临时开放的规则
sudo firewall-cmd --list-ports
sudo firewall-cmd --list-services

# 查看永久开放的规则
sudo firewall-cmd --permanent --list-ports
sudo firewall-cmd --permanent --list-services
 

5. 移除已开放的端口 / 服务

若需关闭已开放的端口或服务,将 --add 改为 --remove 即可:

 
 
# 临时移除80端口
sudo firewall-cmd --remove-port=80/tcp

# 永久移除HTTP服务
sudo firewall-cmd --permanent --remove-service=http

# 重新加载规则
sudo firewall-cmd --reload
 

四、使用 iptables 管理防火墙(适用于旧版本或手动切换场景)

若系统使用 iptables 而非 firewalld(如 CentOS 6,或手动关闭 firewalld 后),操作如下:

1. 安装 iptables 服务(若未安装)

 
 
sudo yum install -y iptables-services
 

2. 关闭 iptables 防火墙

 
 
# 临时关闭
sudo systemctl stop iptables

# 永久关闭
sudo systemctl disable iptables
 

3. 添加 iptables 例外(开放端口)

 
 
# 开放80端口(TCP)
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 开放22端口(SSH)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 保存规则(CentOS 7需用此命令)
sudo service iptables save

# 重启iptables生效
sudo systemctl restart iptables
 
 
 

注意事项

  1. 安全风险:关闭防火墙会使服务器暴露在网络中,建议仅在测试环境临时使用,生产环境需严格通过 “开放必要端口” 来保障安全。
  2. 云服务器额外配置:若使用云服务器(如阿里云、腾讯云),除系统防火墙外,还需在云厂商的 “安全组” 中开放对应端口,否则可能仍无法访问。
  3. 规则生效:永久配置需重新加载防火墙或重启服务才能生效,临时配置在防火墙重启后失效。

通过以上方法,可灵活管理 CentOS 系统的防火墙规则,在安全性和功能性之间取得平衡。
 
 
目录结构
全文
微信客服 微信客服
服务热线: 400-9989-596
电子邮箱: ltd@crash.work