パスワードを忘れた? アカウント作成
15167472 journal
UNIX

yuuka_maniaの日記: ssh のポートフォワード 3

日記 by yuuka_mania

ssh のポートフォワードは、随分と長い間お世話になっている機能だけれども、
文法や -L と -R の違いを忘れてしまい、加えてman を見ると、いつも混乱
してしまうのだ。そしてその混乱の原因が自分でも整理できていなかった。
つまり、雰囲気で使っていたのである。

以下は、手元の ssh の man からの引用。

:
          -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 set-
                          ting. However, an explicit bind_address may be used to bind the connection to
                          a specific address. The bind_address of ``localhost'' indicates that the lis-
                          tening port be bound for local use only, while an empty address or `*' indi-
                          cates that the port should be available from all interfaces.

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.

という、上記の一部だけが目に入り、指定されたポートが、リモートにフォワー
ドされるのだなと読んでしまう。

私がやりたいことは、 ssh 踏台越しに、リモートホストのポートをロー
カルに持ってきたいのだから、 -L じゃないなと。

そして、-R の部分の man を読むと、

:
          -R [bind_address:]port:host:hostport
          -R [bind_address:]port:local_socket
          -R remote_socket:host:hostport
          -R remote_socket:local_socket
          -R [bind_address:]port
                          Specifies that connections to the given TCP port or Unix socket on the remote
                          (server) host are to be forwarded to the local side.

                          This works by allocating a socket to listen to either a TCP port or to a Unix
                          socket on the remote side. Whenever a connection is made to this port or Unix
                          socket, the connection is forwarded over the secure channel, and a connection
                          is made from the local machine to either an explicit destination specified by
                          host port hostport, or local_socket, or, if no explicit destination was speci-
                          fied, ssh will act as a SOCKS 4/5 proxy and forward connections to the destina-
                          tions requested by the remote SOCKS client.

                          Port forwardings can also be specified in the configuration file. Privileged
                          ports can be forwarded only when logging in as root on the remote machine.
                          IPv6 addresses can be specified by enclosing the address in square brackets.

                          By default, TCP listening sockets on the server will be bound to the loopback
                          interface only. This may be overridden by specifying a bind_address. An empty
                          bind_address, or the address `*', indicates that the remote socket should lis-
                          ten on all interfaces. Specifying a remote bind_address will only succeed if
                          the server's GatewayPorts option is enabled (see sshd_config(5)).

                          If the port argument is `0', the listen port will be dynamically allocated on
                          the server and reported to the client at run time. When used together with -O
                          forward the allocated port will be printed to the standard output.

Specifies that connections to the given TCP port or Unix socket on the
remote(server) host are to be forwarded to the local side.

と書いてあり、ローカルホストに、ポートが転送されるのか。

そうだ、私がやりたいのは、-R オプションのポートフォワードだと。

が、実際は、-R ではなく、-L なんですよね…

といった具合の混乱をいつもしてしまい、Ctl-r で、古いコマンドの履歴を確
認して、あぁ、 -L だったと思いだし、そして、また、忘れる...

この混乱はなぜなのか。自分なりに考えてみたわけなんだが、1つは、ポート
フォワードという名前がいけないんじゃないかと思いはじめた。

-L の man を冷静に読むと、

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.

ポートが、フォワードされるとは書いておらず、コネクションがフォワードさ
れると書いてある。ローカルへの *接続* が、リモートへフォワードされると。

一方の -R も、

Specifies that connections to the given TCP port or Unix socket on the remote
(server) host are to be forwarded
to the local side.

リモートへの *接続* が、ローカルにフォワードされるよと。

なるほど。ガッテンガッテンガッテン!

なんとなく、お前は、何に混乱して、何に納得してるのか、伝わらない気がし
ていますが、良いです。自分のメモだから。そして、こんなに納得して
も、半年後には、また混乱してそうだけど。

まとめ
ちゃんと英語読め

p.s.
ところで、-R って、どういう時に使いますかね。私は、今まで、-L ばかり使う場面が多
く、-R を有効的に使う機会というのが思い浮かばなかったというのも混乱し
てた原因の1つかとも思いました。が、最近、VPN が使えないセグメントで、
仕事しなければならなくなり、加えて、テレワークで、どうしたもんかなぁと
悩んでいたのですが、-R で、グローバルにある ssh サーバに接続することで、
家から、

% ssh -J GLOBALSSHHOST -p PORTNUMBER localhost

することで、無事、仕事できるような機会があったりしました。後に、VPN も整
備されたのですが、VPN だと、トラヒック全部 VPN に吸われるのもなんだか気
分が良くなくて、今でも ssh -R してたりする今日この頃です。

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

物事のやり方は一つではない -- Perlな人

読み込み中...