余白の書きなぐり

aueweのブログ

sshで多段接続 (ncも-Wもない人用)

sshで多段接続したい。多段接続について一番わかり易くまとまってるサイトは ncある限りぼくはどこまででもいけるッ! だと思う。ただ残念なことに、うちの環境には nc が入ってなかった。

$ ssh remote
bash: nc: command not found
ssh_exchange_identification: Connection closed by remote host

最近は nc を使わずに ssh -W オプションで代用できるらしい。 今後はこちらが推奨されるとのこと。 ProxyCommandによるsshの多段接続について

しかし僕の環境にはssh -Wもなかった。さてどうするか。

connect の導入

そういうときは connect を使う。 connect を使って簡単に多段 SSH を実現する方法 にそのへんの事情が載っている。

connectコマンドの導入はとても簡単で、 connect.c中継地に ダウンロードした後、

$ gcc connect.c -o connect

コンパイルすればおわり。

環境設定

localから、中継地stepを経由して、remoteに接続したい。 さっきも書いたけど、中継地に connect を導入すること。

local -----------> step -----------> remote
                ↑connect導入
localの環境

localでは~/.ssh/configだけ設定。

# local (~/.ssh/config)

Host remote
Hostname remote.url.jp   # remoteのurl
ProxyCommand ssh step /home/step/bin/connect %h %p
#                     ↑stepサーバー内でのconnectパスを指定
User remote_account_name # stepからremoteに接続するときのアカウント名

# ---------------

Host step
Hostname step.url.jp   # stepのurl
User step_account_name # localからstepに接続するときのアカウント名
stepの環境

stepでは~/.ssh/config~/.ssh/authorized_keysを設定。 まず~/.ssh/config

# step (~/.ssh/config)

Host remote
Hostname remote.url.jp   # remoteのurl
User remote_account_name # stepからremoteに接続するときのアカウント名

.ssh/authorized_keys

# step (~/.ssh/authorized_keys)
localの~/.ssh/id_rsa.pubを登録
remoteの環境

remoteでは~/.ssh/authorized_keysを設定。

# remote (~/.ssh/authorized_keys)
stepの~/.ssh/id_rsa.pubを登録
localの~/.ssh/id_rsa.pubも登録