1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

外部のサーバに SSH 接続する場合、最も単純には

User@Computer ~ % ssh user@xxx.xx.xx.xx

としてパスワード入力することになりますが、サーバで起動した X Window System を使用するソフト(e.g., GrADS)の画面表示の表示を見るためには -X -Y オプションなどが必要になったり、踏み台サーバがある場合は ProxyCommand= を設定する必要があったり、そもそも接続先の IP アドレスを覚える必要があったりと、コマンドが長くなりがちで面倒です。そこで、これらを簡便化するために、SSH 接続のための config ファイルを設定し、公開鍵認証を行うことで接続時のパスワード認証も排除するようにします。

ここでは、接続先に踏み台サーバが存在し、その LAN 内に目的のサーバが存在するという 2 段階接続の状況を仮定します。踏み台サーバと目的のサーバの名前・IP アドレスは、以下のように表記することにします。以下の表記の箇所は、適宜変更して下さい。

サーバ名 IP アドレス
踏み台サーバ bastion xxx.xx.xx.xx
目的のサーバ private01 yyy.yyy.yyy.yyy

公開鍵と秘密鍵の生成

サーバへの SSH 接続で使用する公開鍵と秘密鍵のペアを作成します。 ssh-keygen のデフォルトでは 2048 bit の RSA 暗号鍵が生成されますが、この形式は最近あまり推奨されません。ここでは、より強固なEd25519 形式の鍵を生成することにします。

User@Computer ~ % mkdir ~/.ssh
User@Computer ~ % cd ~/.ssh
User@Computer ~/.ssh % ssh-keygen -t ed25519 -f id_ed25519

以上 3 つのコマンドを実行すると、

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):

と表示されます。パスフレーズを要求されますが、パスフレーズを設定したくない場合は何も入力せずに Enter を押しても問題ありません。その後、

Enter same passphrase again:

と表示されて再度入力を求められるため、先程のパスフレーズを入力するか、もう一度 Enter を押します。すると、

Your identification has been saved in /Users/*user*/.ssh/id_ed25519
Your public key has been saved in /Users/*user*/.ssh/id_ed25519.pub
The key fingerprint is:
.
.
.

と表示され、秘密鍵( ~/.ssh/id_ed25519 )と公開鍵( ~/.ssh/id_ed25519.pub )が生成されます。

鍵のアップロードと config の設定

踏み台サーバへの鍵のアップロード

まず、踏み台サーバ( bastion )に前節で作成した公開鍵を登録します。

User@Computer ~/.ssh % ssh-copy-id user@xxx.xx.xx.xx

上のコマンドを実行すると、サーバに初めて接続する場合は以下の確認がでるので、

The authenticity of host 'xxx.xx.xx.xx (xxx.xx.xx.xx)' can’t be established.
ED25519 key fingerprint is hogehoge.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?

yes と入力します。すると、

user@xxx.xx.xx.xx’s password:

と表示されるため、踏み台サーバ( bastion )のパスワードを入力します。以上で、踏み台サーバ( bastion )に公開鍵がアップロードされました。

config の設定

次に、config を設定します。 ~/.ssh/config というファイルを作成し、

~/.ssh/config
Host bastion
    HostName xxx.xx.xx.xx
    User user

Host private01
    HostName yyy.yyy.yyy.yyy
    User user
    ProxyJump bastion

    # Jupyterサーバを利用する場合など、ポートフォワーディングが必要な場合はそのポート番号を指定しておく
    LocalForward zzzz localhost:zzzz  # 任意のポート番号に読み替えてください

	# X11転送を要求するソフトのために必要な設定(-X、-Yオプションに相当)
    ForwardX11 yes
    ForwardX11Trusted yes

と入力して保存します(後述する目的のサーバ private01 の設定も併せて記述してあります)。

ここまでで、

User@Computer ~ % ssh bastion

を実行することで、公開鍵認証によってパスワードなしで踏み台サーバにログインすることができるようになります。

目的のサーバ

続いて、以下のコマンドを実行し、踏み台サーバ( bastion )経由で目的のサーバ( private01 )に公開鍵を登録します。

User@Computer ~ % ssh-copy-id private01

初めて接続する場合、先程と同様にフィンガープリントの確認が表示されるため、 yes と入力し、続けて目的のサーバ( private01 )のパスワードを入力します。

以上で、

User@Computer ~ % ssh private01

と入力することによって、踏み台サーバの LAN 内にある目的のサーバにパスワードなしで SSH 接続することができます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?