R.A. Epigonos et al.

[ssh] ssh-keygen で公開鍵と秘密鍵を作る

ssh-keygen で ssh の秘密鍵と公開鍵を作る。秘密鍵を用いてログインしたいホストの ~/.ssh/authorized_keys に生成された公開鍵を追記する。秘密鍵は ssh-keygen を走らせたマシン (秘密鍵を作成したマシン) から移動させてはいけない。

手元のコンピュータで ssh-keygen を走らせる。

$ ssh-keygen -b 4096 -t rsa -C ''
Generating public/private rsa key pair.
Enter file in which to save the key (/home/**********/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/**********/.ssh/id_rsa.
Your public key has been saved in /home/**********/.ssh/id_rsa.pub.
The key fingerprint is:
**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**
The key's randomart image is:
+---[RSA 4096]----+
|*****************|
|*****************|
|*****************|
|*****************|
|*****************|
|*****************|
|*****************|
|*****************|
|*****************|
+-----------------+

メッセージに出ているように、id_rea (秘密鍵) と id_rsa.pub (公開鍵) が手元のマシンの ~/.ssh/ ディレクトリ内に作成された。

$ ls ~/.ssh/
authorized_keys  id_rsa  id_rsa.pub  known_hosts

秘密鍵と公開鍵の例は以下。

$ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
****************************************************************
************************************************************
-----END RSA PRIVATE KEY-----
$ cat ~/.ssh/id_rsa.pub
ssh-rsa ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

作成された id_rsa.pub を何らかの方法でログインしたいホストの ~/.ssh/authorized_keys に追記する。追記後は以下のようにして秘密鍵を用いてログインできるようになる。

$ ssh -l ***** ********************
The authenticity of host '******************** (**************)' can't be established.
RSA key fingerprint is ***********************************************.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '********************,**************' (RSA) to the list of known hosts.
Enter passphrase for key '/home/****/.ssh/id_rsa':
Last login: Fri May 27 19:19:41 2011 from ***************************

リファレンス

  1. ssh 公開鍵 - Google 検索
  2. @IT:Linuxでsshの鍵を作成するには

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2009-12-27T19:18:43+09:00
  2. Modified: 2009-12-27T19:18:43+09:00
  3. Generated: 2023-08-27T23:09:16+09:00