![如何在重新启动时加载已保存的 iptables 规则](https://linux22.com/image/1012083/%E5%A6%82%E4%BD%95%E5%9C%A8%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%E6%97%B6%E5%8A%A0%E8%BD%BD%E5%B7%B2%E4%BF%9D%E5%AD%98%E7%9A%84%20iptables%20%E8%A7%84%E5%88%99.png)
我使用了 /etc/network/interfaces 中的 pre-up。奇怪的是,我有两个 Ubuntu 服务器,一个是 PC,另一个是 VPS。此方法在我的 PC 上有效,但在 VPS 上无效。
这是我的电脑上的 /etc/network/interfaces
# ifupdown has been replaced by netplan(5) on this system. See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
# sudo apt install ifupdown
auto dsl-provider
iface dsl-provider inet ppp
pre-up /bin/ip link set eno1 up # line maintained by pppoeconf
provider dsl-provider
#auto eno1
#iface eno1 inet manual
pre-up iptables-restore < /etc/iptables.rules
这是我的 VPS 上的 /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
pre-up iptables-restore < /etc/iptables.rules
source /etc/network/interfaces.d/*
答案1
Netplan 不支持钩子脚本:
https://netplan.io/faq#use-pre-up-post-up-etc-hook-scripts
解决方法是使用 networkd-dispatcher。上面的常见问题解答提供了有关如何操作的示例。
以下是使用 networkd-dispatcher 通过安装在 /etc/networkd-dispatcher/routable.d/50-ifup-hooks 中的脚本运行现有 ifup 钩子的示例:
#!/bin/sh
for d in up post-up; do
hookdir=/etc/network/if-${d}.d
[ -e $hookdir ] && /bin/run-parts $hookdir
done
exit 0
类似地,这里有一个安装在 /etc/networkd-dispatcher/off.d/50-ifdown-hooks 中的 ifdown 钩子的示例:
#!/bin/sh
for d in down post-down; do
hookdir=/etc/network/if-${d}.d
[ -e $hookdir ] && /bin/run-parts $hookdir
done
exit 0