4
0

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.

【Linux】ssh-keygenコマンドを実行する際に公開鍵にコメントを付与する方法とは?

Posted at

はじめに

業務でssh-keygenコマンドを実行する際に公開鍵にコメントを付与する方法で少し迷ったので、コメントを付与する方法を記事として残したいと思います。

ssh-keygenコマンドを知っている前提での内容になります。

ssh-keygenコマンドのオプション

以下のオプションを付与するとコメントを付与することが可能。

オプション
-C コメント

使用例

今回はコメント付与の紹介にフォーカスしているため、暗号化方式はデフォルトのrsaとさせて頂きます。

コメントを付与するパターン

  • 公開鍵と秘密鍵の作成
コマンド
ssh-keygen -f ~/.ssh/id_test_key -C "comment"

コマンドを実行すると、パスフレーズ(パスワード)が聞かれるので入力する。
※そのままEnterを押下するとパスフレーズ無しにすることが可能

実行例
% ssh-keygen -f ~/.ssh/id_test_key -C "comment"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/ma-sato/.ssh/id_test_key
Your public key has been saved in /Users/ma-sato/.ssh/id_test_key.pub
The key fingerprint is:
〜〜〜〜省略します〜〜〜〜
%
  • 公開鍵と秘密鍵の確認

公開鍵と秘密鍵が作成されていることを確認。

% ls -la ~/.ssh | grep id_test
-rw-------   1 ma-sato  staff  2590  6  6 20:54 id_test_key
-rw-r--r--   1 ma-sato  staff   561  6  6 20:54 id_test_key.pub
%
ファイル 鍵の種類
id_test_key 秘密鍵(人に見せてはいけない鍵)
id_test_key.pub 公開鍵(人に見せても問題ない鍵)
  • 公開鍵にコメントが付与されていることを確認

commentという文字が付与されている。

% cat id_test_key.pub
ssh-rsa AAAAB3NzaC1yc2E 〜〜長いので省略〜〜 XrZ0R4M= comment
%

デフォルトの場合

  • 公開鍵と秘密鍵の作成
コマンド
ssh-keygen -f ~/.ssh/id_test_key
実行例
% ssh-keygen -f ~/.ssh/id_test_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/ma-sato/.ssh/id_test_key
Your public key has been saved in /Users/ma-sato/.ssh/id_test_key.pub
The key fingerprint is:
〜〜〜〜省略します〜〜〜〜
%
  • 公開鍵と秘密鍵の確認

公開鍵と秘密鍵が作成されていることを確認。

% ls -la ~/.ssh | grep id_test
-rw-------   1 ma-sato  staff  2610  6  6 21:03 id_test_key
-rw-r--r--   1 ma-sato  staff   577  6  6 21:03 id_test_key.pub
%
ファイル 鍵の種類
id_test_key 秘密鍵(人に見せてはいけない鍵)
id_test_key.pub 公開鍵(人に見せても問題ない鍵)
  • 公開鍵にユーザー名@ホスト名が付与されていることを確認

ユーザー名@ホスト名がデフォルトで表示されるコメントになります。

% cat id_test_key.pub
ssh-rsa AAAAB3NzaC1yc2E 〜〜長いので省略〜〜 XrZ0R4M=  ma-sato@pc.elecom
%

補足ですが、ユーザー名とホスト名は以下になります。

表示名 種類
ma-sato ユーザー名
pc.elecom ホスト名

参考記事

4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?