0
2

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 3 years have passed since last update.

macで秘密鍵と公開鍵を作成する(ssh-keygen)

Last updated at Posted at 2020-06-23

SSHとは

Secure Shellの略。
ユーザーが安全にサーバーを遠隔操作するために通信を暗号化する手段のこと。

  • パスワード認証方式
    ユーザーアカウントにパスワードを設定する方式

  • 公開鍵認証方式
    ユーザーが秘密鍵をもち、サーバー側にそれに紐づく公開鍵を登録しておくことで通信を暗号化する方式。

ssh-keygenとは秘密鍵と公開鍵のセットを作るコマンドのこと。

mac環境

1. 鍵作成(ssh-keygen)

$ssh-keygen -t rsa -b 4096 -C "メールアドレス"

  • gitHubなどに登録しているメールアドレスを設定。
  • 鍵の種類はrsa, rsa1, dsa, ecdsa, ed25519 (rsaがデフォルト)

2. どこに保存するか聞かれる

Generating public/private rsa key pair. Enter file in which to save the key (/Users/home/.ssh/id_rsa)

  • デフォルトで~/.sshに保存される。
  • ファイル指定にこだわりがなければそのままEnter

3. PassPhraseの設定を求められる

Enter passphrase (empty for no passphrase): Enter same passphrase again:

  • パスフレーズとは、秘密鍵が盗まれた時に秘密鍵を解読されるのを防ぐためのもの
  • そのままEnterを2回押せばパスフレーズ未設定で鍵作成

4. 鍵が作成される

Your identification has been saved in /test/.ssh/id_rsa. Your public key has been saved in /test/.ssh/id_rsa.pub. The key fingerprint is:

  • ~/.sshに秘密鍵と公開鍵が保存されているのを確認(デフォルト)
    $cat .ssh/id_rsa $cat .ssh/id_rsa.pub

  • Githubなどには公開鍵(id_rsa.pub)を登録する

ハマったエラー

SSH接続時に **WARNING: UNPROTECTED PRIVATE KEY FILE!**と警告が出て接続できない。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: UNPROTECTED PRIVATE KEY FILE! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0666 for 'id_rsa' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key "id_rsa": bad permissionsPermission denied (publicly).

秘密鍵のパーミッションが0666(書き換え可能)なので危ないよ!と忠告してくれているらしい。
なので、パーミッションを変更すれば解決できる。
$chmod 0600 id_rsa

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?