我尝试过许多类似的问题,例如下面这样,在我的例子中,我在虚拟盒中使用了带有 Debian OS 的 VM:
- 如何在 Ubuntu 上启动/停止 iptables?
- https://unix.stackexchange.com/questions/52376/why-do-iptables-rules-disappear-when-restarting-my-debian-system
- https://www.digitalocean.com/community/tutorials/how-to-list-and-delete-iptables-firewall-rules
- https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-iptables-on-ubuntu-14-04
我已尝试使用这些命令来清除 iptables 规则。
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
按照这些命令
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -F
sudo iptables -X
当我重新启动虚拟机时,我仍然得到相同的规则。我尝试使用iptables-save
,但总是得到这些规则。
# Generated by iptables-save v1.4.21 on Wed Mar 28 03:44:14 2018
*nat
:PREROUTING ACCEPT [127:23299]
:INPUT ACCEPT [14:1996]
:OUTPUT ACCEPT [32:1947]
:POSTROUTING ACCEPT [32:1947]
:OUTPUT_direct - [0:0]
:POSTROUTING_ZONES - [0:0]
:POSTROUTING_ZONES_SOURCE - [0:0]
:POSTROUTING_direct - [0:0]
:POST_public - [0:0]
:POST_public_allow - [0:0]
:POST_public_deny - [0:0]
:POST_public_log - [0:0]
:PREROUTING_ZONES - [0:0]
:PREROUTING_ZONES_SOURCE - [0:0]
:PREROUTING_direct - [0:0]
:PRE_public - [0:0]
:PRE_public_allow - [0:0]
:PRE_public_deny - [0:0]
:PRE_public_log - [0:0]
-A PREROUTING -j PREROUTING_direct
-A PREROUTING -j PREROUTING_ZONES_SOURCE
-A PREROUTING -j PREROUTING_ZONES
-A OUTPUT -j OUTPUT_direct
-A POSTROUTING -j POSTROUTING_direct
-A POSTROUTING -j POSTROUTING_ZONES_SOURCE
-A POSTROUTING -j POSTROUTING_ZONES
-A POSTROUTING_ZONES -g POST_public
-A POST_public -j POST_public_log
-A POST_public -j POST_public_deny
-A POST_public -j POST_public_allow
-A PREROUTING_ZONES -g PRE_public
-A PRE_public -j PRE_public_log
-A PRE_public -j PRE_public_deny
-A PRE_public -j PRE_public_allow
COMMIT
# Completed on Wed Mar 28 03:44:14 2018
# Generated by iptables-save v1.4.21 on Wed Mar 28 03:44:14 2018
*mangle
:PREROUTING ACCEPT [4925:1051078]
:INPUT ACCEPT [4925:1051078]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4791:1080206]
:POSTROUTING ACCEPT [4791:1080206]
:FORWARD_direct - [0:0]
:INPUT_direct - [0:0]
:OUTPUT_direct - [0:0]
:POSTROUTING_direct - [0:0]
:PREROUTING_ZONES - [0:0]
:PREROUTING_ZONES_SOURCE - [0:0]
:PREROUTING_direct - [0:0]
:PRE_public - [0:0]
:PRE_public_allow - [0:0]
:PRE_public_deny - [0:0]
:PRE_public_log - [0:0]
-A PREROUTING -j PREROUTING_direct
-A PREROUTING -j PREROUTING_ZONES_SOURCE
-A PREROUTING -j PREROUTING_ZONES
-A INPUT -j INPUT_direct
-A FORWARD -j FORWARD_direct
-A OUTPUT -j OUTPUT_direct
-A POSTROUTING -j POSTROUTING_direct
-A PREROUTING_ZONES -g PRE_public
-A PRE_public -j PRE_public_log
-A PRE_public -j PRE_public_deny
-A PRE_public -j PRE_public_allow
COMMIT
# Completed on Wed Mar 28 03:44:14 2018
# Generated by iptables-save v1.4.21 on Wed Mar 28 03:44:14 2018
*security
:INPUT ACCEPT [4826:1030935]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4814:1084121]
:FORWARD_direct - [0:0]
:INPUT_direct - [0:0]
:OUTPUT_direct - [0:0]
-A INPUT -j INPUT_direct
-A FORWARD -j FORWARD_direct
-A OUTPUT -j OUTPUT_direct
COMMIT
# Completed on Wed Mar 28 03:44:14 2018
# Generated by iptables-save v1.4.21 on Wed Mar 28 03:44:14 2018
*raw
:PREROUTING ACCEPT [4949:1054264]
:OUTPUT ACCEPT [4815:1084545]
:OUTPUT_direct - [0:0]
:PREROUTING_direct - [0:0]
-A PREROUTING -j PREROUTING_direct
-A OUTPUT -j OUTPUT_direct
COMMIT
# Completed on Wed Mar 28 03:44:14 2018
# Generated by iptables-save v1.4.21 on Wed Mar 28 03:44:14 2018
*filter
:INPUT ACCEPT [2653:591941]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2649:609004]
COMMIT
# Completed on Wed Mar 28 03:44:14 2018
我想要的是类似下面的规则。我已将我的设置rules.v4
为这些命令:
# Generated by iptables-save v1.4.21 on Tue Mar 27 02:48:59 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT
# Completed on Tue Mar 27 02:48:59 2018
但每当我重新启动系统时,我总是会得到以前的规则......
我的配置可能出了什么问题?我是不是漏掉了什么
我尝试卸载 iptables-persistent 并重新安装,但仍然得到相同的规则......
答案1
您的配置rules.v4
就是系统启动时要用到的配置。您可以保存正在运行的配置iptables-save > rules.v4
,然后如果您正在运行,它将在重启后继续存在iptables-persistent
这是因为iptables-save
仅将您的配置打印到 STDOUT,但您想将您的配置存储在rules.v4
文件中。
答案2
要将当前规则保存到文件,请使用 iptables-save。例如:
iptables-save > /etc/sysconfig/iptables
要从该文件加载已保存的规则,请使用 iptables-restore。例如:
iptables-restore < /etc/sysconfig/iptables
答案3
我通常会将包含所有 iptables 命令的防火墙脚本放入 /root 文件夹中。最后我添加
iptables-save > /etc/iptables.conf
ip6tables-save > /etc/ip6tables.conf
然后保存,chmod + x 脚本并运行它。
然后在我的 /etc/network/interfaces 中添加
pre-up iptables-restore < /etc/iptables.conf
pre-up ip6tables-restore < /etc/ip6tables.conf
到 iface lo 部分。
这也允许我在服务器之间复制防火墙设置,因为我通常使用策略删除并且只打开所需的端口。
此外,还有一个很大的安全隐患:
sudo iptables -P INPUT ACCEPT
此命令将 POLICY 设置为接受。这意味着您机器上的每个端口都设置为开放。始终使用策略丢弃并仅打开所需的端口。
答案4
您尝试过 UFW(简单防火墙)吗?!https://wiki.debian.org/Uncomplicated%20Firewall%20%28ufw%29
它是一个使用 IPTables 的 cmd 前端程序(如果需要,也可以使用 gufw),使实时使用 iptables 变得非常容易