LoginSignup
18
15

More than 5 years have passed since last update.

SSH Key 生成 for Mac

Last updated at Posted at 2018-02-27

gitlabに登録する公開鍵を生成したので、その際のめも。

とりあえず何を使えばって場合はこれで

ssh-keygen -b 4096 -t rsa

▼オプション補足
-b: 鍵のビット数(設定しない場合は、デフォルトの1024ビット)
-t: 鍵の種類(dsa, ecdsa, ed25519, rsa, rsa1)

※そのほかのオプションについては、下記を参考にしてください。
人によっては、-Cオプションでコメントをつける場合もあるようです。

OpenSSH-7.3p1 日本語マニュアルページ

ssh key生成の手順

ファイル名で見る公開鍵、秘密鍵の違い

・id_rsa.pub = 公開鍵
・id_rsa = 秘密鍵

既存のkeyを確認

ls -al ~/.sshで確認できます。

UserNames-MacBook-Pro:~ UserName$ ls -al ~/.ssh
total 8
drwx------   3 UserName  staff    96  2  7 11:42 .
drwxr-xr-x+ 35 UserName  staff  1120  2 23 11:23 ..
-rw-r--r--   1 UserName  staff   209  2  7 11:42 known_hosts

「id_rsa.pub」も「id_rsa」もないので、既存のkeyは無いことが確認できました。

keyの生成

オプション設定しない場合の鍵生成

ssh-keygenを実行します。

UserNames-MacBook-Pro:~ UserName$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/UserName/.ssh/id_rsa): (保存先に指定がなければEnterを押してください) 
Enter passphrase (empty for no passphrase):  (パスワード入力)
Enter same passphrase again: (パスワード入力)
Your identification has been saved in /Users/UserName/.ssh/id_rsa.
Your public key has been saved in /Users/UserName/.ssh/id_rsa.pub.
The key fingerprint is:
(省略)
The key's randomart image is:
(省略)
+----[SHA256]-----+
UserNames-MacBook-Pro:~ UserName$ ls -al ~/.ssh
total 24
drwx------   5 UserName  staff   160  2 27 12:03 .
drwxr-xr-x+ 35 UserName  staff  1120  2 23 11:23 ..
-rw-------   1 UserName  staff  1766  2 27 12:03 id_rsa
-rw-r--r--   1 UserName  staff   418  2 27 12:03 id_rsa.pub
-rw-r--r--   1 UserName  staff   209  2  7 11:42 known_hosts

「id_rsa.pub」も「id_rsa」のどちらも生成されてますね。

オプション設定しない場合の暗号度合い

ssh-keygen -l -f ~/.ssh/id_rsa.pubを実行します。

UserNames-MacBook-Pro:~ UserName$ ssh-keygen -l -f ~/.ssh/id_rsa.pub
2048 SHA256:(省略) (RSA)

通常、2048あれば十分と言われていますので、あくまでも確認程度です。(暗号方式も確認できますね。)

オプション設定した場合の鍵生成

ssh-keygen -b 4096 -t rsaを実行します。

UserNames-MacBook-Pro:~ UserName$ ssh-keygen -b 4096 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/UserName/.ssh/id_rsa): (保存先に指定がなければEnterを押してください) 
Enter passphrase (empty for no passphrase): (パスワード入力)
Enter same passphrase again: (パスワード入力)
Your identification has been saved in /Users/UserName/.ssh/id_rsa.
Your public key has been saved in /Users/UserName/.ssh/id_rsa.pub.
The key fingerprint is:
(省略)
The key's randomart image is:
(省略)
UserNames-MacBook-Pro:~ UserName$ ls -al ~/.ssh
total 24
drwx------   5 UserName  staff   160  2 27 12:14 .
drwxr-xr-x+ 35 UserName  staff  1120  2 23 11:23 ..
-rw-------   1 UserName  staff  3326  2 27 12:14 id_rsa
-rw-r--r--   1 UserName  staff   762  2 27 12:14 id_rsa.pub
-rw-r--r--   1 UserName  staff   209  2  7 11:42 known_hosts

「id_rsa.pub」も「id_rsa」のどちらも生成されてますね。

オプション設定した場合の暗号度合い

ssh-keygen -l -f ~/.ssh/id_rsa.pubを実行します。

UserNames-MacBook-Pro:~ UserName$ ssh-keygen -l -f ~/.ssh/id_rsa.pub
4096 SHA256:(省略)(RSA)

オプションで設定した通り、4096ビットで生成されました。
こちらの方が強度がありますね。

余談

クリップボードへのコピーはこのコマンドを使用すると便利です。

UserNames-MacBook-Pro:~ UserName$ pbcopy < ~/.ssh/id_rsa.pub

これで公開鍵の内容がコピーできたので、あとはペーストするだけ。

18
15
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
18
15