我在Ubuntu中的iptables配置文件
/etc/iptables/filter
中进行了更改,并希望重新加载它们。我阅读了手册,也进行了谷歌搜索,但没有找到相关信息。非常感谢您的帮助。/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
*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
/etc/iptables.firewall.rules
文件,但是sudo iptables-restore < /etc/iptables/rules.v4
对我有效。 - timbo/etc/init.d/iptables restart
/etc/init.d/networking restart
/etc/init.d/firewall restart
iptables-apply -t 60 your_rules_file
这将应用规则60秒(默认为10秒),如果您不确认它们,将会恢复原状。这将在您因规则而被系统踢出时保护您(例如,如果您通过ssh进行操作)。
您可以使用以下内容作为替代:
iptables-restore < your_rules_file; sleep 60; iptables-restore < clean_rules
sudo ufw reload
将重新加载防火墙及其规则。
(假设您正在使用基于iptables的ufw。)
经过一点谷歌搜索,我找到了重新启动iptables的方法. . . sudo /etc/init.d/firewall restart