UNIX的なアレ

UNIX的なこととかいろいろ

手軽な ssh dynamic port fowarding

ポートフォワーディングさせたい機会っていろいろありますよね。
今回はsshで簡単にSocksProxyを利用する方法を紹介します。

さて、sshのmanを見てみましょう。


-D [bind_address:]port
Specifies a local ``dynamic'' application-level port forwarding.
This works by allocating a socket to listen to port on the local
side, optionally bound to the specified bind_address. Whenever a
connection is made to this port, the connection is forwarded over
the secure channel, and the application protocol is then used to
determine where to connect to from the remote machine. Currently
the SOCKS4 and SOCKS5 protocols are supported, and ssh will act
as a SOCKS server. Only root can forward privileged ports.
Dynamic port forwardings can also be specified in the configura-
tion file.
といろいろ書いてありますが、まぁようするに-Dをつけてやればよいわけです。
今回は1080ポートで試してみます。

# ssh -D localhost:1080 {servername} -l {ユーザー名}
これだけでOK。
確認してみましょう。1080番がListenされていればOKです。

# netstat -an | grep 1080
tcp4 0 0 127.0.0.1.1080 *.* LISTEN
あとはFirefoxから利用する場合は以下の設定をいれちゃえば、SocksProxy経由で接続ができます。
Firefox

環境設定

詳細

ネットワークより以下の様に設定します。

これだけでOK。sshするときに接続したサーバーをSocksProxyとしてFirefoxを使用することができます。

今回はFirefoxのみ紹介しましたが、socksに対応していないアプリも対応させることもできます。
Mac/Linuxな人はtsocksでいけますし、Windowsな人はfreecapを使えば利用可能です。

# 正直、簡単すぎて怖いですよねコレ。