我有一个像这样的小脚本来配置 iptables:
#!/bin/bash
PRE_STR="iptables -t nat -A PREROUTING -p tcp -j DNAT"
FOR_STR="iptables -A FORWARD -p tcp -j ACCEPT"
#####################################
# instances
CM="10.0.1.137"
MASTER="10.0.1.149"
MYSQL="10.0.1.83"
REPORTING="10.0.1.85"
#####################################
# Clear Iptables
iptables -F
iptables -t nat -F
#####################################
# Forward to enable Internet on private nodes
iptables -t nat -A POSTROUTING -j MASQUERADE
#####################################
# Port forwarding
forward()
{
$PRE_STR --dport $1 --to $2:$3
$FOR_STR --dport $3 -d $2
}
#what from to ip to port
forward 3222 $CM 22
forward …Run Code Online (Sandbox Code Playgroud) 我们为服务器提供了 Chef,因此我们对 Ubuntu 16.04 和 18.04 版本具有相同的配置。恢复 iptables 规则也有相同的规则,
cat /etc/network/if-pre-up.d/iptables_load
/sbin/iptables-restore < /etc/iptables/general
但它不适用于 Ubuntu 18.04。
如果我手动运行它,它会起作用。这是否意味着此脚本未在启动时运行?
更新
我按照此处的描述创建了 systemd 服务,并且运行良好。
[Unit]
Description = Apply iptables rules
[Service]
Type=oneshot
ExecStart=/etc/network/if-pre-up.d/iptables_load
[Install]
WantedBy=network-pre.target
Run Code Online (Sandbox Code Playgroud)