Tech Blog - mixross

多段ssh

このエントリーをはてなブックマークに追加
LINE

はじめに

例えば、ローカル(local)から踏み台サーバ(server-bastion)経由で目的のサーバ(server-a)にsshログインする場合

ホスト名 IPアドレス ユーザ名
server-bastion xxx.xxx.xxx.xxx user-bastion
server-a yyy.yyy.yyy.yyy user-a
local user-local$ ssh -i ~/.ssh/key-bastion.pem user-bastion@xxx.xxx.xxx.xxx
server-bastion user-bastion$ ssh -i ~/.ssh/key-a.pem user-a@yyy.yyy.yyy.yyy

と、sshログインを2度行う。

これを、毎回行うのは面倒なためlocalにて1度のsshコマンドでserver-aにログインを行う方法が多段ssh。

踏み台サーバへのログインを簡易化

まず、踏み台サーバ(server-bastion)へのログインコマンドも長いのでまずはこれを簡単にする。
sshクライアントの設定ファイル(~/.ssh/config)を編集(追記)する。

local user-local$ vim ~/.ssh/config
~/.ssh/config
Host bastion    # ここのホスト名は好きな名前を付けて良い
  HostName xxx.xxx.xxx.xxx
  User user-bastion
  IdentityFile ~/.ssh/key-bastion.pem

此の時、configファイルを新規作成した場合は権限を変更しておく必要がある。

local user-local$ chmod 700 ~/.ssh/config

これでserver-bastionにログインするsshコマンドが

local user-local$ ssh -i ~/.ssh/key-bastion.pem user-bastion@xxx.xxx.xxx.xxx

だったのが

local user-local$ ssh bastion

で済むようになる。

ローカルから直接server-aにログイン

まず、server-bastionにあるserver-aの認証鍵(~/.ssh/key-a.pem)をローカルに持ってくる。

local user-local$ scp bastion:~/.ssh/key-a.pem ~/.ssh/

次に~/.ssh/configファイルを編集(追記)する。

local user-local$ vim ~/.ssh/config
~/.ssh/config
Host srv-a    # ここのホスト名は好きな名前を付けて良い
  HostName yyy.yyy.yyy.yyy
  ProxyCommand ssh -W %h:%p bastion
  User user-a
  IdentityFile ~/.ssh/key-a.pem

これでlocalからserver-aにログインするsshコマンドが

local user-local$ ssh -i ~/.ssh/key-bastion.pem user-bastion@xxx.xxx.xxx.xxx
server-bastion user-bastion$ ssh -i ~/.ssh/key-a.pem user-a@yyy.yyy.yyy.yyy

だったのが

local user-local$ ssh srv-a

で済むようになる。

RSS