在 Ubuntu 上,iptables 本身不是一个“服务”,而是内核 netfilter 框架的用户态工具。是否需要“启动”、怎么“启动”,取决于你使用的 Ubuntu 版本和管理方式。
下面按常见情况说明:
只要内核支持,直接执行即可:
sudo iptables -L
如果没报错,说明 iptables 已经在用。
Ubuntu 默认 不会自动保存 iptables 规则。
sudo iptables-save > /etc/iptables/rules.v4
sudo iptables-restore < /etc/iptables/rules.v4
这是最常用、最省心的方式。
sudo apt update
sudo apt install iptables-persistent
安装时会询问是否保存当前规则,选 Yes。
它会通过 systemd 自动启动:
systemctl status netfilter-persistent
如果没启动:
sudo systemctl enable netfilter-persistent
sudo systemctl start netfilter-persistent
sudo netfilter-persistent save
sudo netfilter-persistent reload
Ubuntu 新版本中,iptables 实际是 nftables 的兼容层。
查看:
sudo nft list ruleset
如果你看到规则,说明系统用的是 nftables。
sudo systemctl enable nftables
sudo systemctl start nftables
Ubuntu 默认防火墙是 UFW,它底层就是 iptables。
查看状态:
sudo ufw status
如果你用 UFW,不需要手动操作 iptables。
✅ iptables 不需要“启动”
✅ 用 iptables 命令直接写规则
✅ 用 iptables-persistent 做开机保存
✅ 新系统底层可能是 nftables
如果你愿意,可以告诉我:
我可以给你一份可直接用的配置示例。