需要 iptables 规则来接受所有传入的流量

47 linux firewall iptables

对于我的测试环境,我想接受所有传入的流量,有人可以给我要添加的 iptable 规则。

我当前的iptables -L -n输出看起来像这样

Chain INPUT (policy ACCEPT) target prot opt source
destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0
0.0.0.0/0 ACCEPT all -- 0.0.0.0 /0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0
reject-with icmp-host-prohibited ACCEPT tcp -- 0.0.0.0/0
0.0.0.0/0 tcp dpt:8443 接受 tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 接受 tcp -- 0.0.0.0/0 0.0.0.0. dpt:9443 接受 tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:2124

Chain FORWARD (policy ACCEPT) target prot opt source
destination REJECT all -- 0.0.0.0/0 0.0.0.0/0
reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT) target prot opt source
destination

谢谢

Ale*_*son 70

运行以下。它将在 iptables 的顶部插入规则,并允许所有流量,除非随后由另一个规则处理。

iptables -I INPUT -j ACCEPT
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 -A INPUT -i lo -j ACCEPT -m comment --comment "Allow all loopback traffic"
iptables -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT -m comment --comment "Drop all traffic to 127 that doesn't use lo"
iptables -A OUTPUT -j ACCEPT -m comment --comment "Accept all outgoing"
iptables -A INPUT -j ACCEPT -m comment --comment "Accept all incoming"
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow all incoming on established connections"
iptables -A INPUT -j REJECT -m comment --comment "Reject all incoming"
iptables -A FORWARD -j REJECT -m comment --comment "Reject all forwarded"
Run Code Online (Sandbox Code Playgroud)

如果您想让您的流量更安全,请不要使用接受所有传入规则,或使用“iptables -D INPUT -j ACCEPT -m comment --comment "Accept all传入”" 将其删除,并添加更多具体规则如:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT -m comment --comment "Allow HTTP"
iptables -I INPUT -p tcp --dport 443 -j ACCEPT -m comment --comment "Allow HTTPS"
iptables -I INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT -m comment --comment "Allow SSH"
iptables -I INPUT -p tcp --dport 8071:8079 -j ACCEPT -m comment --comment "Allow torrents"
Run Code Online (Sandbox Code Playgroud)

注意:它们需要位于底部的 2 个拒绝规则之上,因此使用 I 将它们插入顶部。或者如果你像我一样肛门,使用“iptables -nL --line-numbers”获取行号,然后使用“iptables -I INPUT ...”在特定行号插入规则。

最后,保存您的工作:

iptables-save > /etc/network/iptables.rules #Or wherever your iptables.rules file is
Run Code Online (Sandbox Code Playgroud)

  • 这个答案终于结束了我的痛苦。这个答案解决了“我如何让 #@$#% iptables 做我想要它做的事情并且只做我想要它做的事情”的问题,我建议的唯一改进是添加一个转发端口的示例。(即,从 80 到 8080 和 443 到 8443)我认为 99% 的关于 iptables 的问题都会在 1 个帖子中得到解答。 (2认同)

小智 19

要接受您可以使用以下命令的所有传入流量,-P 是将默认策略设置为接受

iptables -P INPUT ACCEPT  
Run Code Online (Sandbox Code Playgroud)

如果您不需要以前的规则,只需刷新/删除它们,然后使用上面的命令。
刷新所有规则使用

iptables -F    
Run Code Online (Sandbox Code Playgroud)