IBM Cloud Docs
リモート・ホスト認証のための SSH 鍵の生成および使用

リモート・ホスト認証のための SSH 鍵の生成および使用

SSH 鍵は、公開鍵暗号方式およびチャレンジ応答認証を使用する SSH サーバーに対してユーザー自身を識別する方法です。 従来型のパスワード認証と比べた場合のこの方式の直接の利点は、ネットワークでパスワードを送信せずにサーバーで認証が行える点です。 また、この方式では無人サーバー通信が可能なため、自動化で使用することができます。

Linux での SSH 鍵の生成

Linux サーバーで SSH 鍵を生成するには、コマンド ssh-keygenを実行します。 生成される鍵のタイプ、および鍵の生成に使用される署名アルゴリズムをカスタマイズする場合、このコマンドはフラグを取ることができます。 以下の例は、パスフレーズのない標準の 2048 ビット RSA 鍵を生成します。 このコマンドは、鍵を保管する場所 (デフォルトは $HOME/.ssh/) と、SSH 鍵を保護するためのパスフレーズを求めるプロンプトを出します。

root@bck2:/etc# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx root@bck2.example.com
The key's randomart image is:
+---[RSA 2048]----+
|.  oo*%=+o..     |
|.++.oX+=. .      |
|..+ooo=. .       |
|   E.+. .o.      |
|    +   S..+     |
|     . +. =      |
|      o  = o     |
|      .o+ +      |
|       +o.       |
+----[SHA256]-----+

リモート・ホストへの公開鍵のコピー

公開 SSH 鍵を使用してリモート・ホストで認証するには、 ssh-copy-id コマンドを使用します。 -i フラグを使用して、リモート・ホストにコピーする公開鍵を指定します。

root@bck2: # ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.176.18.15
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.176.18.15 (10.176.18.15)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.176.18.15's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.176.18.15'"
and check to make sure that only the key(s) you wanted were added.

ssh-copy-id コマンドは、リモート・ホストの .ssh/authorized_key ファイルに鍵を追加します。

鍵が正しくコピーされたことのテスト

公開鍵がリモート・ホストに適切にコピーされたことをテストするには、リモート・ホストに SSH 接続します。

root@bck2:/etc# ssh root@10.176.18.15
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-53-generic x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

Last login: Fri Feb 10 16:51:51 2017 from 169.46.3.91
root@bck1:~#

リモート・ホストに SSH でログインするときに、パスワードの入力を求めるプロンプトは出されません。

パスフレーズ付きの SSH 鍵

SSH 鍵のパスフレーズを指定すると、追加のセキュリティー・レイヤーが提供されますが、保護された鍵を必要とする自動化スクリプトを実行する際に問題が発生する可能性もあります。

ssh-agent は鍵を管理できます。 ユーザーはパスフレーズを 1 回入力します。 ssh-agent はメモリー内に鍵を保持し、必要なときに取り出します。 ssh-agent で鍵を管理する場合は、以下のコマンドを発行します。

eval $(ssh-agent)

プログラムが ssh-add コマンドを開始して公開鍵をエージェントに追加すると、ssh-add ユーティリティーはデフォルトの鍵の名前 ( id_rsa は 1) を検索し、それらを ssh-agent に追加します。 パスワードを入力すると、「アンロックされた」鍵が ssh-agent に保管され、他のサーバーでの認証に使用できます。

root@bck1:~# ssh-add
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
root@bck1:~#

新しい端末セッションを開くたびに、鍵パスフレーズの入力を求めるプロンプトが出されます。 ssh-agent がすべての bash セッションを開始して鍵を追加するように、以下のコマンドを実行して .bash_profile ファイルを追加することを検討してください。

echo ‘eval $(ssh-agent)’ >> ~/.bash_profile
echo ‘ssh-add’ >> ~/.bash_profile