如何将同一 eth0 接口上的 UDP 流量从一个端口转发到另一个端口

如何将同一 eth0 接口上的 UDP 流量从一个端口转发到另一个端口

我正在尝试遵循一些指南来转发 UDP 流量,但似乎没有任何效果。

我有一个 Syslog 服务器将日志推送到端口 514 上的 Logstash 实例(是的,EC2)。

由于这是一个特权端口,并且由于安全限制,我不想授予 JAVA 在非特权端口上运行的权限,因此我需要将 UDP 流量从一个端口转发到同一接口上的另一个端口。

我没有使用环回接口。

为了测试目的,我使用端口 1025 作为 DST 端口。

首先,我使用 nc 在我的 VPN 网络内生成 UDP 流量,如下所示:

nc -u xx.xx.xx.xx 1025

其中 xx.xx.xx.xx 是内部 IP 地址。无法从互联网访问此机器。

我可以使用 tcpdump 查看端口 1025 上的传入流量,如下所示:

tcpdump -i eth0 UDP port 1025 -vv -X

我希望将此流量转发到端口,比如说 55514。

我的 NAT 表如下所示:

[email protected]:/home/ubuntu# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   udp  --  anywhere             anywhere             udp dpt:1025 redir ports 55514

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

但是,使用 tcpdump 时我没有看到同一接口上的任何流量:

[email protected]:/home/ubuntu# tcpdump -i eth0 UDP port 55514 -vv -X
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

我读过几十篇文章,尝试过各种规则,但我无法将它付诸实践。

据我所知,使用相同接口时,PREROUTING 没有意义。

端口转发已启用:

[email protected]:/home/ubuntu# cat /proc/sys/net/ipv4/conf/eth0/forwarding
1

因为它是 EC2 实例,所以我有禁用源/目标检查,但同样,流量不会离开这台机器,因此它不应该相关。

编辑:

机器是 Ubuntu 16.04.6 LTS。

编辑:

[email protected]:/home/ubuntu# iptables -L -t nat -v -n
Chain PREROUTING (policy ACCEPT 156 packets, 34827 bytes)
 pkts bytes target     prot opt in     out     source               destination
    5   163 REDIRECT   udp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            udp dpt:1025 redir ports 55514
    0     0 REDIRECT   udp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            udp dpt:1025 redir ports 6363

Chain INPUT (policy ACCEPT 157 packets, 34861 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 13 packets, 1201 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 13 packets, 1201 bytes)
 pkts bytes target     prot opt in     out     source               destination

相关内容