![iptables-persistent 和 netfilter-persistent 实际上无法在 Ubuntu Server 16.04.3 x86_64 上运行](https://linux22.com/image/1005414/iptables-persistent%20%E5%92%8C%20netfilter-persistent%20%E5%AE%9E%E9%99%85%E4%B8%8A%E6%97%A0%E6%B3%95%E5%9C%A8%20Ubuntu%20Server%2016.04.3%20x86_64%20%E4%B8%8A%E8%BF%90%E8%A1%8C.png)
我已经iptables-persistent
安装netfilter-persistent
:
$ dpkg -l '*-persistent'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-============================================-===========================-===========================-==============================================================================================
ii iptables-persistent 1.0.4 all boot-time loader for netfilter rules, iptables plugin
ii netfilter-persistent 1.0.4 all boot-time loader for netfilter configuration
我还将规则保存在以下位置/etc/iptables/rules.v4
(目前我只关心 IPv4):
$ cat /etc/iptables/rules.v4
# Generated by iptables-save v1.6.0 on Fri Jan 19 09:49:17 2018
*security
:INPUT ACCEPT [11740:1271860]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [9784:2123999]
COMMIT
# Completed on Fri Jan 19 09:49:17 2018
# Generated by iptables-save v1.6.0 on Fri Jan 19 09:49:17 2018
*raw
:PREROUTING ACCEPT [18262:1677349]
:OUTPUT ACCEPT [9784:2123999]
COMMIT
# Completed on Fri Jan 19 09:49:17 2018
# Generated by iptables-save v1.6.0 on Fri Jan 19 09:49:17 2018
*nat
:PREROUTING ACCEPT [7367:452849]
:INPUT ACCEPT [872:48764]
:OUTPUT ACCEPT [500:37441]
:POSTROUTING ACCEPT [500:37441]
COMMIT
# Completed on Fri Jan 19 09:49:17 2018
# Generated by iptables-save v1.6.0 on Fri Jan 19 09:49:17 2018
*mangle
:PREROUTING ACCEPT [18262:1677349]
:INPUT ACCEPT [18259:1677229]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [9784:2123999]
:POSTROUTING ACCEPT [9784:2123999]
COMMIT
# Completed on Fri Jan 19 09:49:17 2018
# Generated by iptables-save v1.6.0 on Fri Jan 19 09:49:17 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m multiport --dports 25,587,465
-A INPUT -p tcp -m state --state NEW -m multiport --dports 110,995
-A INPUT -p tcp -m state --state NEW -m multiport --dports 143,993
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m multiport --dports 3721:3725 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j DROP
-A FORWARD -j DROP
-A OUTPUT -j ACCEPT
COMMIT
# Completed on Fri Jan 19 09:49:17 2018
我真正感兴趣的规则是最后的那条规则:
-A INPUT -p tcp -m state --state NEW -m multiport --dports 3721:3725 -j ACCEPT
但是,当我重新启动服务器时,我没有得到该规则:
$ sudo iptables -4 -L
[sudo] password for kal:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere 127.0.0.0/8 reject-with icmp-port-unreachable
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
tcp -- anywhere anywhere state NEW multiport dports smtp,submission,urd
tcp -- anywhere anywhere state NEW multiport dports pop3,pop3s
tcp -- anywhere anywhere state NEW multiport dports imap2,imaps
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT icmp -- anywhere anywhere icmp echo-request
LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
Chain f2b-shadowsocks (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
Chain f2b-sshd (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
还要注意,iptables 中有一些规则在我安装之前就已经存在了iptables-persistent
,netfilter-persistent
例如 http、smtp、pop3、imap、ssh 的规则。我不知道它们从何而来。当然,我安装了 openssh 和 nginx,并启用了它们的服务,但我从未亲自为它们添加过 iptables 规则。
如果我查看 journalctl 的输出,netfilter-persistent.service 确实成功启动:
$ sudo journalctl -xu netfilter-persistent.service
-- Logs begin at Fri 2018-01-19 18:55:13 HKT, end at Fri 2018-01-19 19:05:41 HKT. --
Jan 19 18:55:13 yuma systemd[1]: Starting netfilter persistent configuration...
-- Subject: Unit netfilter-persistent.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit netfilter-persistent.service has begun starting up.
Jan 19 18:55:13 yuma netfilter-persistent[1997]: run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables start
Jan 19 18:55:14 yuma netfilter-persistent[1997]: run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables start
Jan 19 18:55:14 yuma systemd[1]: Started netfilter persistent configuration.
-- Subject: Unit netfilter-persistent.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit netfilter-persistent.service has finished starting up.
--
-- The start-up result is done.
如果我手动重启 netfilter-persistent.service
机器完全启动后,我得到了我想要的规则:
$ sudo iptables -4 -L
[...]
ACCEPT tcp -- anywhere anywhere state NEW multiport dports 3721:3725
[...]
那么为什么netfilter-persistent
在启动时实际上不起作用?
是否有东西完全覆盖 iptables后 netfilter-persistent
?
我能做什么呢?
更新
我也没有ufw
或firewalld
。
答案1
因此事实证明我的服务器有一个名为的文件/etc/iptables.firewall.rules
,并且规则正在从中恢复/etc/network/if-pre-up.d/firewall
:
#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules
/etc/iptables.firewall.rules
似乎是我在 2015 年按照旧教程创建的一个旧文件,用于保留 iptables 规则。
然后,我要做的就是将新规则添加到该文件中。
我想教训是,在检查了常见的ufw
和iptables-persistent
/netfilter-persistent
内容后,我应该只是grep -rn iptables-restore /etc/
。如果没有找到任何东西,还有可能通过动态添加规则dbus
,如果firewalld
已启用。
答案2
因此,显然使用 netfilter-persistent 将 iptables 的当前状态保存到 /etc/iptables/rules.v4 和 .v6 是不够的。您还必须安排它们在启动时重新加载。这意味着让 netfilter-persistent 在启动时运行,如下所示:sudo systemctl enable netfilter-persistent
这是来自:https://blog.sleeplessbeastie.eu/2018/09/10/how-to-make-iptables-configuration-persistent/
答案3
它是netfilter-persistent save