Cou*_*oul 7 iptables ubuntu-18.04
我们为服务器提供了 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)
小智 6
这就是我所做的:
像这样创建服务模板:
sudo nano /etc/systemd/system/restore-iptables-rules.service
Run Code Online (Sandbox Code Playgroud)
复制粘贴这个:
[Unit]
Description = Apply iptables rules
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'iptables-restore < /etc/iptables.rules'
[Install]
WantedBy=network-pre.target
Run Code Online (Sandbox Code Playgroud)像这样启用服务:
sudo systemctl enable restore-iptables-rules.service
Run Code Online (Sandbox Code Playgroud)重新启动并检查规则是否已应用:
sudo iptables -L
Run Code Online (Sandbox Code Playgroud)我遇到了各种在 if-up 位置触发 iptables 的问题。随着我的 iptables 脚本变得越来越复杂,它可能为每个接口运行(取决于脚本的确切位置)的事实成为一个问题,如果主机名解析等功能要工作,则需要启动正确的网络接口。这些因素导致启动缓慢和故障。您可以考虑替代方案,即将 iptables 脚本作为 systemd 服务运行。
这可以通过在 /etc/systemd/system/ 中创建一个名为 real_iptables.service 的文件来完成,其内容如下:
[Unit]
Description=Set up the firewall
After=network.target
[Service]
Type=oneshot
ExecStart=/root/iptables
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
如您所见,实际的 iptables 脚本位于 /root/iptables。安装服务:
systemctl enable real_iptables
systemctl start real_iptables
Run Code Online (Sandbox Code Playgroud)
启用该服务后,它将在引导时启动,但仅运行一次。如果您想完全安全,可以在 /etc/network/if-up.d/ 中放置一个脚本,使用 iptables 来阻止所有网络通信。这意味着在服务启动之前什么都不会发生。
归档时间: |
|
查看次数: |
23719 次 |
最近记录: |