LoginSignup
27

More than 1 year has passed since last update.

色々なport forwarding方法

Last updated at Posted at 2013-08-12

http://192.168.11.13:8080/ にアクセスすると http://10.0.3.2/ の画面を出す設定例

redirを使う方法

# lport : 転送先ポート、cport : 転送元ポート
redir --lport=8080 --cport=80
# IPアドレスを指定する場合
redir --lport=8080 --cport=80 --caddr=10.0.3.2
# バックグラウンドで動かす場合
nohup redir --lport=8080 --cport=80 &

stoneを使う方法

80->8080
$ stone 10.0.3.2:80 8080
22->443
$ sudo stone 10.0.3.2:22 443

iptables

設定初期化

$ sudo iptables -F

設定

forwarding.sh
# 変数
PUBLIC_IP=192.168.11.13
LXC_IP=10.0.3.2
HOST_PORT=8080
GUEST_PORT=80

# ルーティング
sudo iptables -t nat -A PREROUTING -p tcp -d $PUBLIC_IP --dport $HOST_PORT \
    -j DNAT --to-destination ${LXC_IP}:${GUEST_PORT}

# 通信許可設定(なくてもよい)
# sudo iptables -A FORWARD -p tcp -d $LXC_IP --dport $GUEST_PORT -j ACCEPT

# 必須
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
  • iptablesの場合、localhost:8080 にアクセスしても 10.0.3.2:80に転送されません。

iptablesメモ

変更する番号を確認

iptables -nL --line-numbers

変更する場合

iptables -R INPUT [変更する番号] -s [変更するIP] -p tcp --dport 22 -j ACCEPT

削除

iptables -D INPUT [削除する番号]

port forwarding

iptables -A INPUT -p [tcp|udp] -s [送信元IP] --sport [送信元ポート] -d [送信先IP] --dport [送信先ポート] -j [ACCEPT|DROP|REJECT]

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27