LoginSignup
3
5

More than 3 years have passed since last update.

iptables / firewalldで ポート転送

Last updated at Posted at 2018-11-27

必要になったのでちょっとメモ
作業中のみ有効にすればよいので設定ファイルには記述しない

nat 状態確認

何もない状態

iptables

sudo iptables -t nat -L --line-numbers

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination

firewalld

sudo firewall-cmd --list-forward-ports 

nat ポート転送設定

21番FTP宛の通信を 22番SSHに転送する
(ftpのみファイアウォールで許可されてるけどXウィンドウで作業しなきゃいけない。。ってどんな状況よ)

iptables

sudo iptables -t nat -A PREROUTING -p tcp -m tcp  --dport 21 -j REDIRECT --to-port  22

firewalld

sudo firewall-cmd --add-forward-port=port=21:proto=tcp:toport=22

nat 設定確認

ちゃんと設定入ってる

iptables

sudo iptables -t nat  -L --line-numbers

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination
1    REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:ftp redir ports 22

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination

firewalld

sudo firewall-cmd --list-forward-ports 
port=21:proto=tcp:toport=22:toaddr=

ちなみに永続化する場合は

sudo  firewall-cmd --runtime-to-permanent

nat ポート転送設定削除

iptables

--line-numbers の番号を指定して削除する

sudo iptables -t nat -D PREROUTING 1

firewalld

sudo firewall-cmd --remove-forward-port=port=21:proto=tcp:toport=22:toaddr=

nat 状態確認

消えてる

iptables

sudo iptables -t nat  -L --line-numbers

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination

firewalld

sudo firewall-cmd --list-forward-ports 

FTPポートでXウィンドウ作業

なぜこんなことを。。。

使うソフトは MobaXterm Home Edition
https://mobaxterm.mobatek.net/
インストール不要なポータブルバージョンを利用

FTPなポート向けにSSH接続すると、無事ファイアウォールを超えてサーバに到達
FTPなポート向け通信を iptables がsshなポートに転送してくれるので普通にSSHログイン
xauthやらxtermやらがインストール済みでXが使える環境であること
ログインしたら環境変数 DISPLAY が設定されていること

そしたらxterm起動したりfirefox を起動したりして設定作業を行います。。。

以上
変な制約の上で作業しなくちゃならなくなった時のメモ。。。
いいのか?これ。。

こちらとか参考にさせていただきました
ありがとうございます
80番ポートへ届いたパケットをiptablesでローカルの上位ポートに転送する - Qiita
ipTablesのルールの削除

3
5
0

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
3
5