iptables 错误:未知选项 --dport

Cal*_*phi 27 networking linux firewall iptables

在定义规则时,命令 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.
Run Code Online (Sandbox Code Playgroud)

上面的添加规则命令只是启用 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            
Run Code Online (Sandbox Code Playgroud)

我也在尝试定义一个自定义链(受这个问题的启发)来接受 tcp 和 udp 连接,这样我就不必为我想要启用 tcp 和 udp 的所有内容定义两个规则(例如 Minecraft 或泰拉瑞亚服务器,或完全其他服务)。但即使这样也行不通:

[root@dragonweyr /home/calyodelphi]# iptables -P ACCEPT_TCP_UDP DROP
iptables: Bad built-in chain name.
Run Code Online (Sandbox Code Playgroud)

用礼貌的话来说,这变得非常令人沮丧(与此相关的大量咒骂会让水手告诉我要注意我的嘴)。我的 Google-fu 很糟糕,所以我还没有找到任何可行的解决方案。我在路由器上运行 CentOS 6.5。你们可以提供的任何帮助和指示都会很棒。

编辑:

额外问题:我还计划配置端口转发。是否仍然需要设置规则以接受通过特定端口的传入连接?

dmo*_*ati 41

首先给出一个-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但我从未这样做过,并且在示例中找不到太多支持。

  • -p 的选项必须是具有端口概念的单个协议(出于显而易见的原因,`-p ICMP --dport 无论如何` 也不起作用)。 (3认同)
  • 我只是用 `-p all` 尝试了这个,我得到了完全相同的错误。`未知选项--dport`。它与 `-p tcp` 一起工作,但在这种情况下这并不能真正帮助我,因为它只会导致同样的问题:为所有内容定义单独的 tcp/udp 规则。 (2认同)

Die*_*sen 10

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

-p tcp
Run Code Online (Sandbox Code Playgroud)

  • 就我个人而言,即使使用“-p tcp”我也会出现错误。`$ sudo iptables -A INPUT -p tcp --dport 62668 -j ACCEPT` 返回 `iptables v1.8.7(旧版):未知选项“--dport”` ` (2认同)

小智 6

另一个可能的解决方案是您忘记以 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 -I PREROUTING --src 0/0 --dst 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-ports 8080
# OK
Run Code Online (Sandbox Code Playgroud)

  • 哇,这是一个非常糟糕的错误消息...谢谢! (7认同)

wes*_*man 5

如果iptables报告它使用nftables,则需要改用iptables-legacy

例如:
使用iptables-legacy -A INPUT -p tcp --dport 22 -m state --state NEW -j DROP
而不是iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j DROP


这是一个老问题,但这是搜索结果中的第一个问题。