
40.40.40.40:3306にあるリモートmysqlサーバーに接続する必要があるローカルアプリケーションがあります。
デフォルトのファイアウォールはSSHを除くすべての接続をブロックするため、SSHトンネルを確立して問題なくサーバーに接続できます。
ssh [email protected] -L 3306:127.0.0.1:3306 -N
(他の端末から)
$ mysql -udb_user -h127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
[...]
私の目的は、宛先アドレスが40.40.40.40:3306の接続を127.0.0.1:3306のトンネルに転送するようにiptablesを設定することです。
# iptables -t nat -A PREROUTING -d 40.40.40.40 -p tcp --dport 3306 -j DNAT --to-destination 127.0.0.1:3306
# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere 40.40.40.40 tcp dpt:mysql to:127.0.0.1:3306
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
#cat /proc/sys/net/ipv4/ip_forward
1
この設定を使用しても、アプリケーションはまだデータベースに接続できません。接続設定を 127.0.0.1 に変更しても問題ありませんので、アプリケーションが正常に動作しているとします。
答え1
OUTPUT
アウトバウンド接続をローカルポートにリダイレクトするには、チェーンを使用する必要があります。
このルールは必要に応じて機能します。
iptables -t nat -A 出力 -p tcp -d 40.40.40.40 --dport 3306 -j REDIRECT --to-port 3306