OpenSSH でローカルポートフォワーディングを行う方法

SSH

OpenSSH を使ってローカルポートフォワーディング( local port forwarding )を行う方法についてかんたんにまとめました。

先に結論ですが -N -L オプションを付けてコマンドを実行します:

ssh -N -L [local port]:[host]:[host port] [destination]

SSH ポートフォワーディングとは

ネットワーク上のデータを SSH 接続上に転送することです。 SSH トンネリング( SSH tunneling )とも呼ばれます。

ポートフォワーディングの中でも「ローカルポートフォワーディング」(以下「ローカルフォワーディング」)というのは、ローカルポート(=クライアントマシンのポート)からリモートマシンのポートに転送することです。 ローカルフォワーディングを行うことで、他のアプリケーションはローカルポートに接続するだけでリモートマシン上のアプリケーションと対話できるようになります。

コマンド

OpenSSH でローカルポートフォワーディングを行うコマンドは次のとおりです:

ssh -N -L [local port]:[host]:[host port] [destination]

サンプル

ローカルのポート 8080 をホスト example.com から見た localhost:3306 に転送したいときは次のようにします:

ssh -N -L 8080:localhost:3306 example@example.com

仮に example.com のポート 3306 で MySQL サーバーが動いているとすると、上のコマンドを動かしている間に MySQL client でローカルのポート 8080 に接続すると example.com 上の MySQL サーバーと対話ができます。

mysql --user=[user] --password=[password] --port=8080

オプション -N -L の意味合いはそれぞれ次のとおりです。

-N

接続先のリモートマシンでコマンドを実行しません。

-N

Do not execute a remote command. This is useful for just forwarding ports. Refer to the description of SessionType in ssh_config(5) for details.

意訳:

リモートコマンドを実行しません。これはポートフォワーディングだけ行いたいときに便利です。詳細は ssh_config(5) の SessionType の記述を参照。

-L

指定されたローカルポートを指定されたホストのポートに転送します(ポートは Unix ソケットでも可)。

-L [bind_address:]port:host:hostport
-L [bind_address:]port:remote_socket
-L local_socket:host:hostport
-L local_socket:remote_socket

Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.

Port forwardings can also be specified in the configuration file. Only the superuser can forward privileged ports. IPv6 addresses can be specified by enclosing the address in square brackets.

By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit bind_address may be used to bind the connection to a specific address. The bind_address of “localhost” indicates that the listening port be bound for local use only, while an empty address or ‘*’ indicates that the port should be available from all interfaces.

意訳:

ローカル(クライアント)ホスト上の TCP ポートまたは Unix ソケットへの接続を、リモート側の指定されたホストとポートまたは Unix ソケットに転送することを指定します。 これは、ローカル側の TCP ポート(オプションで bind_address にバインドされるよう指定することも可)か Unix ソケットをリッスンするソケットを割り当てることで動作します。 ローカルポートまたはソケットに接続が行われるたびに、その接続は安全なチャネル上に転送され、リモートマシンのホストポート hostport または Unix ソケット remote_socket への接続が行われます。

ポートフォワーディングは設定ファイルによる設定も可能です。 スーパーユーザだけが特権ポートを転送できます。 IPv6 アドレスはアドレスを角括弧で囲んで指定できます。

ローカルポートはデフォルトでは GatewayPorts の設定に従ってバインドされますが、明示的に bind_address を指定すれば特定のアドレスにバインドすることもできます。 bind_addresslocalhost を指定するとリッスンするポートがローカル専用にバインドされます。 空のアドレスまたは * が指定された場合はリッスンするポートはすべてのインタフェースから利用できるようになります。

確認時のバージョン

ssh -V
OpenSSH_9.0p1, LibreSSL 3.3.6
❯ sw_vers
ProductName:		macOS
ProductVersion:		13.5.1
BuildVersion:		22G90

参考


アバター
後藤隼人 ( ごとうはやと )

ソフトウェア開発やマーケティング支援などをしています。詳しくはこちら