Jer*_*oen 308 linux firewall iptables
我分别在端口8006和8007上托管特殊的HTTP和HTTPS服务.我用iptables来"激活"服务器; 即路由传入的HTTP和HTTPS端口:
iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8006 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8007 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8006
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8007
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-ports 8006
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 443 -j REDIRECT --to-ports 8007
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力.但是,我想创建另一个脚本,再次禁用我的服务器; 即在运行上面的行之前将iptables恢复到它所处的状态.但是,我很难找出删除这些规则的语法.似乎唯一有效的是完全冲洗:
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
Run Code Online (Sandbox Code Playgroud)
但这也将删除其他不受欢迎的iptables规则.
Eli*_*uft 440
执行相同的命令,但将"-A"替换为"-D".例如:
iptables -A ...
Run Code Online (Sandbox Code Playgroud)
变
iptables -D ...
Run Code Online (Sandbox Code Playgroud)
dom*_*i27 423
您也可以使用规则的编号( - line-numbers):
iptables -L INPUT --line-numbers
Run Code Online (Sandbox Code Playgroud)
示例输出:
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT udp -- anywhere anywhere udp dpt:domain
2 ACCEPT tcp -- anywhere anywhere tcp dpt:domain
3 ACCEPT udp -- anywhere anywhere udp dpt:bootps
4 ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
Run Code Online (Sandbox Code Playgroud)
所以,如果你想删除第二条规则:
iptables -D INPUT 2
Run Code Online (Sandbox Code Playgroud)
如果你使用(d)一个特定的表(例如nat),你必须将它添加到删除命令(thx到@ThorSummoner的评论)
sudo iptables -t nat -D PREROUTING 1
Run Code Online (Sandbox Code Playgroud)
ETe*_*ech 29
对我来说没有任何问题的最佳解决方案就是这样:
1.添加一些注释的临时规则:
comment=$(cat /proc/sys/kernel/random/uuid | sed 's/\-//g')
iptables -A ..... -m comment --comment "${comment}" -j REQUIRED_ACTION
Run Code Online (Sandbox Code Playgroud)
2.当规则添加并且您希望删除它(或带有此注释的所有内容)时,请执行以下操作:
iptables-save | grep -v "${comment}" | iptables-restore
Run Code Online (Sandbox Code Playgroud)
因此,您将100%删除与$ comment匹配的所有规则,并保持其他行不变.这个解决方案在过去的2个月里工作,每天约100次规则变更 - 没有问题.希望,这有帮助
ciz*_*ixs 12
使用-D命令,这是man页面解释它的方式:
-D, --delete chain rule-specification
-D, --delete chain rulenum
Delete one or more rules from the selected chain.
There are two versions of this command:
the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match.
Run Code Online (Sandbox Code Playgroud)
一定要意识到这个命令,就像所有其他命令(-A,-I)在某些表上工作一样。如果您不在默认表(filter表)上工作,请使用-t TABLENAME指定该目标表。
iptables -D INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
注意:这只会删除匹配的第一个规则。如果您有许多匹配的规则(这可能发生在 iptables 中),请多次运行。
iptables -D INPUT 2
Run Code Online (Sandbox Code Playgroud)
除了计算数字之外,您还可以列出带有--line-number参数的行号,例如:
iptables -t nat -nL --line-number
Run Code Online (Sandbox Code Playgroud)
小智 6
首先使用此命令列出所有iptables规则:
iptables -S
Run Code Online (Sandbox Code Playgroud)
它列出如下:
-A XYZ -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
然后复制所需的行,只需更换-A与-D删除:
iptables -D XYZ -p ...
Run Code Online (Sandbox Code Playgroud)
假设,如果要删除 NAT 规则,
使用下面的命令列出附加的 IPtables,
# sudo iptables -L -t nat -v
Chain PREROUTING (policy ACCEPT 18 packets, 1382 bytes)
pkts bytes target prot opt in out source destination
7 420 DNAT tcp -- any any anywhere saltmaster tcp dpt:http to:172.31.5.207:80
0 0 DNAT tcp -- eth0 any anywhere anywhere tcp dpt:http to:172.31.5.207:8080
Run Code Online (Sandbox Code Playgroud)
如果你想从 IPtables 中删除 nat 规则,只需执行命令,
# sudo iptables -F -t nat -v
Flushing chain `PREROUTING'
Flushing chain `INPUT'
Flushing chain `OUTPUT'
Flushing chain `POSTROUTING'
Run Code Online (Sandbox Code Playgroud)
然后,您可以验证,
# sudo iptables -L -t nat -v
Run Code Online (Sandbox Code Playgroud)