在 Debian 中,iptables 本身不是“服务”,所以不能直接用 systemctl enable iptables 来开机自启(新版本 Debian 默认也没有 iptables 服务)。常见做法是:开机时自动恢复保存好的 iptables 规则。
下面给你几种常用、可靠的方法。
sudo apt update
sudo apt install iptables-persistent
安装过程中会问你是否保存当前规则:
sudo netfilter-persistent save
Debian 会自动启用:
systemctl status netfilter-persistent
重启后规则会自动加载。
iptables-save > /etc/iptables/rules.v4
(目录不存在就自己建)
sudo nano /etc/systemd/system/iptables-restore.service
内容:
[Unit]
Description=Restore iptables rules
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /etc/iptables/rules.v4
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
sudo systemctl enable iptables-restore
如果你用的是老 Debian 且有 rc.local:
iptables-restore < /etc/iptables/rules.v4
加到 /etc/rc.local 的 exit 0 前。
nft list ruleset 有内容,说明系统在用 nftablesupdate-alternatives --config iptables
切换为 iptables-legacy如果你愿意,可以告诉我:
cat /etc/debian_version)我可以给你最合适的一套配置。