重新加载iptables

我在Ubuntu中的iptables配置文件/etc/iptables/filter中进行了更改,并希望重新加载它们。我阅读了手册,也进行了谷歌搜索,但没有找到相关信息。非常感谢您的帮助。

1你既没有提供你使用的Ubuntu版本的任何信息,也没有在发布这个问题之前好好搜索一下网络。 - Puspendu Banerjee
6个回答

通常情况下,您的防火墙规则在配置文件/etc/iptables.firewall.rules中。
要激活文件中定义的规则,您必须将它们发送到iptables-restore(如果需要,可以使用其他文件)。
sudo iptables-restore < /etc/iptables.firewall.rules

你可以检查它们是否已激活:
sudo iptables -L

如果你想在每次开机时激活相同的规则,请创建这个文件:
sudo nano /etc/network/if-pre-up.d/firewall

有了这个内容:

#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules

并给予它执行的权限。
sudo chmod +x /etc/network/if-pre-up.d/firewall

希望能对你有所帮助 =)
/etc/iptables.firewall.rules的示例文件:
*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow SSH connections
#
#  The -dport number should be the same port number you set in sshd_config
#
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT

编辑于2021年08月:

刚刚在升级到Ubuntu 20.04.2 LTS时遇到了问题。 iptables-restore 的位置从 /sbin/iptables-restore 更改为 /usr/sbin/iptables-restore

请务必使用 whereis iptables-restore 命令检查您的系统位置,否则您的网络接口将无法启动。

如果升级后没有网络连接,您可以使用 sudo systemctl status networking.service -l 命令检查原因,以我的情况为例:

Failed to start Raise network interfaces.
if-pre-up.d/firewall: 2: /sbin/iptables-restore: not found

6在Ubuntu 14.10上,我没有/etc/iptables.firewall.rules文件,但是sudo iptables-restore < /etc/iptables/rules.v4对我有效。 - timbo

最简单的方法是重新启动(如果上述方法不起作用,请重新启动并检查是否有所改变)。
其次,可以通过使用iptables配置重启守护程序来实现(谷歌搜索:重启Ubuntu守护程序)。
示例(取决于您的配置):
/etc/init.d/iptables restart  

/etc/init.d/networking restart  

/etc/init.d/firewall restart

13没有名为/etc/init.d/iptables的文件。 - Raccha
1/etc/init.d中存在哪些与网络相关的内容?尝试重新启动它。 - Juha
3重启网络服务? - Juha
1死链是无效的。 - Dustin Graham
@DustinGraham 谢谢,已经移除了损坏的链接。 - Juha
1这些都在Ubuntu 16.04上无法运行。 - chovy
@chovy:即使是重新启动?你的系统其他地方可能有问题。 - Juha
这可能导致网络完全不可用,并且即使重启也无法恢复。请查看此链接以获取解决方案:https://askubuntu.com/a/376586/250315 - van abel

如果您已经执行了规则,它们已经在运行中,不需要重新加载。如果您有一个配置文件但尚未执行,目前我见过的最好方法是使用iptables-apply(一种iptables扩展)。
iptables-apply -t 60 your_rules_file

这将应用规则60秒(默认为10秒),如果您不确认它们,将会恢复原状。这将在您因规则而被系统踢出时保护您(例如,如果您通过ssh进行操作)。

您可以使用以下内容作为替代:

iptables-restore < your_rules_file; sleep 60; iptables-restore < clean_rules

不,重新加载对于DDNS主机解析是绝对必要的。如果任何被引用主机的IP发生变化,那么iptables需要重新加载。理想情况下,您应该每30分钟从cron执行此操作。每30分钟重启并不方便。 - mckenzm

sudo ufw reload

将重新加载防火墙及其规则。

(假设您正在使用基于iptables的ufw。)


经过一点谷歌搜索,我找到了重新启动iptables的方法. . . sudo /etc/init.d/firewall restart


8没有名为 /etc/init.d/firewall 的文件。 - Raccha

如果你想重新加载IPtables以验证刚刚所做的更改,你也可以使用以下命令行重启Apache:
/etc/init.d/apache2 stop /etc/init.d/apache2 start
这些命令可能会因为你使用的Ubuntu版本不同而有所变化,以及之前可能进行的修改。
希望对你有所帮助。
Pierre

2我怀疑普通用户是否在运行Apache 2 Web服务器,我强烈不建议为了重新加载防火墙规则而重启Apache2。 - kaiya