LoginSignup
2
0

【初心者向け🔰】SSH鍵を生成するコマンドと全手順解説(まとめ)

Posted at

概要

あんまり頻繁に使わないため、備忘録ついでに初心者向けに情報をまとめて残す。

SSH 鍵を生成する ssh-keygen コマンドの基本的な使い方
その際、ファイル名を指定して生成する方法や、その他関連コマンドなどの備忘録

利用用途

GitHub や GitLab 等のサービスとの SSH 通信時に必要になる鍵ファイル(公開鍵/秘密鍵)を生成する

手順

実行するコマンドの例は以下

ssh-keygen -C "comment" -f id_rsa_file_name

実際に実行すると以下のような動きをする

# SSH 鍵生成コマンドを実行
$ ssh-keygen -C "to3izo@example.com" -f ~/.ssh/id_rsa_to3izo

# 実行後: 対話式の質問がはじまる
Generating public/private rsa key pair.     # 訳:「公開鍵/秘密鍵 を生成します」
Enter passphrase (empty for no passphrase): # そのままエンターキーでもOK
Enter same passphrase again:                # そのままエンターキーでもOK
# 処理結果
Your identification has been saved in id_rsa_to3izo # 秘密鍵「id_rsa_to3izo」生成
Your public key has been saved in id_rsa_to3izo.pub # 公開鍵「id_rsa_to3izo.pub」生成
The key fingerprint is:                     # 指紋 (キーマップ) を表示
SHA256:xxxxxxxxxxxxxxxxx to3izo@example.com
The key's randomart image is:
+---[RSA 3072]----+
|    xxxxx        |
|     xx  x       |
|    xx xx        |
|   x xx xxx      |
+----[SHA256]-----+

# 処理完了

→ 上記コマンドを実行すると ~/.ssh/id_rsa_to3izo という秘密鍵と、id_rsa_to3izo.pub という公開鍵を生成する。

その後、「公開鍵」(.pub が付いたファイル名) の方を、以下のように cat コマンド等でファイルの中身を表示する。

# コマンド実行で中身を表示
$ cat id_rsa_to3izo.pub

# 実行結果 (ファイルの中身)
ssh-rsa Xxxxx(略)xxxxX to3izo3@example.com

表示された「ssh-rsa」から始まる文字列を全てコピーして、GitHub 等の SSH Key 登録画面にペーストして使用する。
すると、次回から公開鍵を登録したサイトとの SSH 通信ができるようになる。

コマンドの意味

基本コマンド

ただ鍵を生成するだけのコマンド

# 鍵生成コマンド
ssh-keygen

デフォルトの生成ファイルの出力先は ~/.ssh/
デフォルトの生成ファイル名は id_rsaid_rsa.pub

名前を指定して作成

ssh-keygen でファイル名を指定して作成する方法。
-f オプションでファイル名(厳密には保存先)が決められる

# 例1. ファイル名を指定して鍵生成
ssh-keygen -f id_rsa_file_name

# 例2. ファイルのフォルダも指定する場合
ssh-keygen -f ./path/to/directory/id_rsa_file_name

標準出力なしで作成

-q 標準出力(実行結果をコンソールに表示すること)を無効にする = キーマップが出ない

ssh-keygen -q -f id_rsa_file_name

コメントの指定

-C オプション
ユーザ名@ホスト名(デフォルト)」以外にする場合に使用
別のメールアドレスとかを使用する(xxx@example.com

ssh-keygen -C comment -f id_rsa_file_name

暗号化形式の指定

-t オプション
暗号化形式を「rsa(デフォルト)」以外にする場合、「dsa」「ecdsa」「ed25519」から指定

ssh-keygen -t ed25519 -f id_rsa_file_name

fingerprint の指定

-E オプション
鍵のfingerprintを「sha256(デフォルト)」以外にする場合、「md5」指定

ssh-keygen -E md5 -f id_rsa_file_name

エラー: SSH鍵の権限がオープン過ぎて怒られた場合

何かしら SSH 通信を利用したときに、以下の場合は、SSH キーの権限を変更する必要があります。

発生するエラー: Permissions 0644 for 'id_rsa_xxxxx' are too open.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/xxxxx/.ssh/id_rsa_xxxxx' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/xxxxx/.ssh/id_rsa_xxxxx": bad permissions
git@github.com: Permission denied (publickey).

👉 要訳:「秘密鍵のパーミッションが "644" なので、他のユーザーからアクセスできるような権限では使えないよ !」

権限を変更する

chmod コマンドで、書き込み・読み取り・実行権限を変更する(詳しい説明は割愛)

# 対象のキーを指定(600 もしくは 400)
chmod 600 id_rsa_xxxxx

おまけ

生成したSSH鍵の公開鍵をコピーするときは、以下でも可能

macOSの場合

pbcopy < ~/.ssh/ファイル名.pub

Windows で Git Bash など使っていた場合

clip < ~/.ssh/id_rsa.pub
2
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
2
0