あるデバイスから別のネットワーク上の別のデバイスへのポート転送

あるデバイスから別のネットワーク上の別のデバイスへのポート転送

私のネットワーク設定は3台のコンピュータ(デバイスA、B、C)で構成されています。デバイスA、B、Cはスイッチを使用してイーサネットを介して接続されています。デバイスAだけがインターネットにアクセスできます(Wi-Fiルーター、ポート8000​​-8002に転送されます)。

デバイスA、B、CがHTTPサーバーを実行しており、デバイスD(インターネット上のコンピューター)からそのサーバーにアクセスしようとしているとします。ただし、デバイスDはデバイスAを介してのみBとCにアクセスできるため、いくつかのポート魔法が必要です。

次の住所とタイトルがあるとします。

Device A: 
  192.168.5.100 (wlan0)  
  10.42.0.1 (eth0)
  server on port 80
Device B:
  10.42.0.2 (eth0)
  server on port 80
Device C:
  10.42.0.3 (eth0)
  server on port 80
Device D:
  somewhere on the Internet

どうやって渡しますか?

Device D -> 192.168.5.100:8000 (A) -> 10.42.0.1:80 (A)
Device D -> 192.168.5.100:8001 (A) -> 10.42.0.2:80 (B)
Device D -> 192.168.5.100:8002 (A) -> 10.42.0.3:80 (C)

私は次のような他のコマンドを使ってiptablesを試しました。

sudo iptables -t nat -A PREROUTING -p tcp --dport {from_port} -j DNAT --to-destination {to_ip}:{to_port}
sudo iptables -t nat -A POSTROUTING -d {to_ip} -p tcp --dport {to_port} -j SNAT --to-source {from_ip}

どこ

from_ip is 192.168.5.100
from_port is either 8000, 8001, or 8002
to_ip is either 10.42.0.1, 10.42.0.2, or 10.42.0.3
to_port is 80

しかし、私は成功しませんでした。

コマンドに問題がありますか?それとも、iptablesを使用するよりも優れた選択肢がありますか?役に立つと、3台のコンピュータすべてに物理的にアクセスできますが、デバイスAにはグラフィカルインターフェイスはありません。デバイスAとBは常にLinuxにあり、デバイスCは時々Windowsにあり、時にはLinuxにあります。

ベストアンサー1

# Enable routing
sudo echo 1 > /proc/sys/net/ipv4/ip_forward
# Forward specific ports to specific destinations
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0 --dport 8000 -j DNAT --to-destination 10.42.0.1:80
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0--dport 8001 -j DNAT --to-destination 10.42.0.2:80
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0 --dport 8002 -j DNAT --to-destination 10.42.0.3:80
# Enable NAT (aka Masqurade)
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

おすすめ記事