迁移后 iptables 规则丢失通常是因为规则仅存在于内核内存未持久化到磁盘,恢复配置需先通过控制台或备份文件加载规则,再配置开机自动加载服务。若无法访问服务器,需优先通过控制台重置默认策略为 ACCEPT 以防 SSH 失联。
先说结论:iptables 规则重启后丢失属于正常机制,恢复核心在于找到备份文件并使用 iptables-restore 加载,同时确保 persistence 服务已启用。
- 先确认:区分 Linux 发行版(CentOS 7+ 默认 firewalld,Debian 需 iptables-persistent)。
- 先处理:若 SSH 中断,通过云厂商控制台重置安全组或进入单用户模式清除 DROP 规则。
- 再验证:使用 iptables -L -n 检查规则列表,并用 telnet 或 curl 测试业务端口连通性。
命令速用版
以下命令需 root 权限执行,适用于大多数 Linux 发行版恢复场景。
恢复规则:iptables-restore < /etc/sysconfig/iptables 保存当前规则:iptables-save > /etc/sysconfig/iptables 重启服务:systemctl restart iptables 查看状态:iptables -L -n -v
Debian/Ubuntu 系统配置文件路径通常为 /etc/iptables/rules.v4,恢复命令为 iptables-restore < /etc/iptables/rules.v4。
为什么会这样
iptables 规则默认存储在内核 Netfilter 框架的内存中,系统重启后内存清空导致规则丢失。
Linux 发行版对防火墙持久化的处理方式不同,导致迁移后配置失效。RHEL/CentOS 6 使用 service iptables save,而 CentOS 7/8 默认使用 firewalld,若未安装 iptables-services 包,传统 iptables 规则不会自动加载。Ubuntu/Debian 需安装 iptables-persistent 包才能将规则保存至 /etc/iptables/rules.v4 并在启动时还原。迁移过程中若未复制这些特定配置文件或未启用对应服务,规则便会丢失。
分步处理
按以下顺序操作,确保在恢复规则的同时不丢失服务器管理权限。
步骤 1:确保管理通道安全
在恢复严格规则前,先设置默认策略为 ACCEPT,防止因规则错误导致 SSH 断开。
命令:iptables -P INPUT ACCEPT
命令:iptables -P FORWARD ACCEPT
命令:iptables -P OUTPUT ACCEPT
步骤 2:从备份文件恢复规则
若有迁移前的备份文件,直接使用 iptables-restore 加载。
CentOS 命令:iptables-restore < /etc/sysconfig/iptables
Debian 命令:iptables-restore < /etc/iptables/rules.v4
若文件路径不同,请指定实际备份路径。
步骤 3:配置持久化服务
CentOS 7+ 需安装并启用 iptables-services。
命令:yum install iptables-services -y
命令:systemctl enable iptables
命令:systemctl start iptables
Debian 需确保 iptables-persistent 已安装。
命令:apt-get install iptables-persistent
命令:netfilter-persistent save
步骤 4:保存当前运行规则
确保当前内存中的规则已写入磁盘配置文件。
命令:service iptables save 或 iptables-save > /etc/sysconfig/iptables
怎么验证是否生效
执行检查命令确认规则已加载且服务开机自启。
检查规则列表
命令:iptables -L -n -v
预期结果:显示具体的 ACCEPT 或 DROP 规则,而非空列表。
检查服务状态
命令:systemctl status iptables
预期结果:Active 状态为 active (running)。
测试业务端口
命令:curl -v http://localhost:端口 或 telnet IP 端口
预期结果:连接成功,无 Connection timed out 报错。
常见坑
CentOS 7+ firewalld 冲突
CentOS 7 及更高版本默认使用 firewalld,若同时运行 iptables 和 firewalld 可能导致规则覆盖或冲突。若坚持使用 iptables,需停止并禁用 firewalld 服务。
IPv6 规则单独管理
iptables 仅管理 IPv4 流量,IPv6 规则需使用 ip6tables 单独备份和恢复,配置文件通常为 rules.v6 或 ip6tables。
SSH 锁死风险
若 INPUT 链默认策略为 DROP 且未预先添加 22 端口放行规则,恢复规则后会导致 SSH 无法连接。操作前务必先添加 iptables -A INPUT -p tcp `--dport` 22 -j ACCEPT。
常见问题
CentOS 7 恢复 iptables 规则后重启又丢了怎么办?
是因为未启用 iptables-services 服务。需执行 systemctl enable iptables 确保开机加载,并确认 /etc/sysconfig/iptables 文件存在且内容正确。
Debian 系统找不到 iptables 服务命令?
Debian 通常使用 netfilter-persistent 管理持久化。需安装 iptables-persistent 包,规则保存在 /etc/iptables/rules.v4,重启后自动加载。
没有备份文件如何恢复默认配置?
可手动清空规则并设置默认允许。执行 iptables -F 清空规则,执行 iptables -P INPUT ACCEPT 设置默认策略,然后重新配置必要的安全规则。
参考来源
- CSDN 博客 - iptables 规则备份与恢复全攻略:避免服务器重启后规则丢失的坑
- 技术教程 - centos iptables 如何恢复规则配置(2025 年 9 月 28 日发布)
- 技术教程 - centos iptables 怎么恢复配置(2025 年 12 月 9 日发布)
- CSDN 问答 - Linux 开启 iptables 后所有端口不通的常见原因是什么
- 技术教程 - iptables 如何恢复规则集(2025 年 10 月 28 日资料)
- 技术教程 - Linux 命令-iptables-restore 命令 (从文件批量恢复 iptables 防火墙规则)(2026 年 1 月 11 日资料)
- 技术教程 - Debian iptables 如何恢复配置(截至 2025 年 3 月 6 日)