温馨提示×

centos怎样导入防火墙规则

小樊
48
2026-10-23 23:28:03
栏目: 智能运维

在 CentOS 中,“导入防火墙规则”通常有 两种常见场景,取决于你使用的是哪种防火墙:


一、使用 firewalld(CentOS 7/8/Stream 默认)

1️⃣ 直接加载已有 firewalld 配置

如果你之前导出过 firewalld 配置(如 public.xml):

cp public.xml /etc/firewalld/zones/
systemctl restart firewalld

或重新加载:

firewall-cmd --reload

2️⃣ 使用 firewall-cmd 批量导入规则(推荐)

如果你有一批命令,可以写成脚本:

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
firewall-cmd --reload

3️⃣ 导入 iptables 规则到 firewalld(不推荐)

firewalld 不直接支持 iptables 规则文件导入,需要手动转换。


二、使用 iptables(传统方式)

1️⃣ 导入 iptables 规则文件

如果你有导出文件(如 iptables.rules):

iptables-restore < iptables.rules

查看当前规则:

iptables -L -n

2️⃣ 开机自动导入(CentOS 6 / 老系统)

service iptables save
chkconfig iptables on

CentOS 7+(若使用 iptables 服务):

systemctl enable iptables
systemctl start iptables

三、常见场景示例

✅ 从一台服务器导出再导入到另一台

导出:

iptables-save > rules.txt

导入:

iptables-restore < rules.txt

四、推荐使用方式(总结)

系统 推荐方式
CentOS 7/8 firewalld
老系统 / 特殊需求 iptables

如果你能告诉我:

  • CentOS 版本
  • 是 firewalld 还是 iptables
  • 规则是哪种格式(命令 / xml / rules 文件)

我可以给你 精确的一步一步操作。

0 踩