iptables 错误:未知选项 --dport

iptables 错误:未知选项 --dport

命令 iptables 不再识别定义规则时最常用的选项之一:--dport

我收到此错误:

[root@dragonweyr /home/calyodelphi]# iptables -A INPUT --dport 7777 -j ACCEPT_TCP_UDP
iptables v1.4.7: unknown option `--dport'
Try `iptables -h' or 'iptables --help' for more information.

上面的添加规则命令只是启用 Terraria 连接的一个示例。

下面是我目前所拥有的基本 iptables 配置(listiptables别名为iptables -L -v --line-numbers),很明显它--dport在过去是起作用的:

root@dragonweyr /home/calyodelphi]# listiptables 
Chain INPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       39  4368 ACCEPT     all  --  lo     any     anywhere             anywhere            
2      114 10257 ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED 
3        1    64 ACCEPT     tcp  --  eth1   any     anywhere             anywhere            tcp dpt:EtherNet/IP-1 
4       72 11610 ACCEPT     all  --  eth1   any     anywhere             anywhere            

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 91 packets, 10045 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain ACCEPT_TCP_UDP (0 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            

我也试图定义一个自定义链(灵感来自这个问题) 来接受 tcp 和 udp 连接,这样我就不必为我想要启用 tcp 和 udp 的所有内容定义两个规则(例如 Minecraft 或 Terraria 服务器,或其他完全不同的服务)。但即使这样也不起作用:

[root@dragonweyr /home/calyodelphi]# iptables -P ACCEPT_TCP_UDP DROP
iptables: Bad built-in chain name.

用礼貌的话说,这真是让人很沮丧(这其中的骂人话连水手都会说我要注意自己的言行)。我的 Google 能力很差,所以我还没有找到任何可行的解决方案。我在路由器上运行的是 CentOS 6.5。如果你们能提供任何帮助和建议,那就太好了。

编辑:

附加问题:我也计划配置端口转发。是否仍需要设置规则来接受通过特定端口传入的连接?

答案1

首先给出一个-p选项,如-p tcp-p udp

例子:

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j DROP

iptables -A 输入 -p udp --dport 53 --sport 1024:65535 -j 接受

您也可以尝试,-p all但我从未这样做过,并且在示例中也没有找到太多支持。

答案2

另一个可能的解决方案是你忘记以 root 身份运行。我在使用 debian 教程时遇到了这个问题

$ iptables -t nat -p tcp -I PREROUTING --src 0/0 --dst 127.0.0.1  --dport 80 -j REDIRECT --to-ports 8080
iptables v1.8.2 (nf_tables): unknown option "--dport"
$ sudo iptables -t nat -p tcp -I PREROUTING --src 0/0 --dst 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
# OK

答案3

如果使用 --dport,则需要协议 (-p)。例如:

-p tcp

答案4

@dmourati 和 @diegows 已经回答了你的第一个问题,所以我将回答你的第二个问题。还有附加问题。我还会给出一个附加提示 ;)

iptables -P 仅有的接受内置链。在filter表中,这将是INPUTOUTPUTFORWARD链。

端口转发不由链处理INPUT,因此您不必在INPUT链中打开端口。它被链条夹住FORWARD,要小心。

额外提示:在学习和/或排除故障时iptables, 的输出iptables-save比 的输出好得多iptables -L -v --line-numbers。 试试看,你会感到惊喜的 :)

相关内容