如何使用 iptables 转发我的 ubuntu 22.04

如何使用 iptables 转发我的 ubuntu 22.04

您知道如何为我的机器提供 iptables 规则,以允许从我的公共接口 (wlp3s0) 到我的私有接口 (lo) 的流量吗?有点像这种拓扑:192.168.1.1:80 => 127.0.0.1:80

我需要获取这个,因为我正在尝试使用以下命令将我的 Web 服务器与 netcat 连接:nc 192.168.1.23 80-e /bin/bash -v,并且连接服务器没有问题,因为计算机返回我:nc:已连接到 192.168.1.23 80。没问题,但问题是当我使用 nc -lvpn 443 启动侦听器时。保持侦听状态并且未连接:

Ncat: Version 7.80 ( https://nmap.org/ncat ) Ncat: Listening on :::443 Ncat: Listening on 0.0.0.0:443

现在当我检查流程时,我意识到了这一点

TCP gr10-thinkpad-edge-e430:36410->gr21:https

netcat 使用我的公共接口与服务器建立 tcp 连接,但当我监听时,只在私有接口(127.0.0.1)上进行监听,而不是在所有接口上进行监听。因此 netcat 无法将数据从我的公共接口传输到我的私有接口:

ncat 33827 root 4u IPv4 486476 0t0 TCP *:https(监听)

所以我的问题是:基于以前的情况,允许我的 nat 将流量从我的公共接口传输到我的私有接口的最佳 iptable 规则是什么,类型为:public ip: 80 => private ip:80?最好的解决方案是什么?

答案1

sysctl net.ipv4.ip_forward=1

iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination ip1

iptables -t nat -A PREROUTING -j DNAT --to-destination ip2

iptables -t nat -A POSTROUTING -j MASQUERADE

来自 ip1 的所有数据转发到 ip2

相关内容