SSH鍵の生成からGitHubに登録するまでの備忘録
鍵を生成しGitHubに登録する手順
まずは次に自分が見返してもできるように手順だけ書いておきます。
// ユーザー情報を設定する git config --global user.name "First-name Family-name" git config --global user.email "username@example.com" // 確認 $ git config --global --list
// EmailアドレスはGitHubに登録しているもの $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter] // パスワード入れる Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
上記で公開鍵と秘密鍵が生成されます。
// 表示されるすべてをコピーする $ cat id_rsa.pub
https://github.com Setting> SSH and GPG keys> New SSH keys でコピーした公開鍵を登録する。
$ ssh -T git@github.com The authenticity of host 'github.com (192.30.255.113)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no)?
RSAのfingerprintが以下のいずれかと一致するのを確認します。
https://help.github.com/articles/github-s-ssh-key-fingerprints
Hi GitHubアカウント名! You've successfully authenticated, but GitHub does not provide shell access.
$ eval "$(ssh-agent -s)" Agent pid 59566 $ ssh-add ~/.ssh/id_rsa
以上でGitHubに登録が完了です。
SSH鍵の生成にて
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
GiHubのヘルプにのっとり上記コマンドで鍵を生成しました。
$ ssh-keygen
だけで鍵を生成すると2048bitになる。
RSA鍵では2048bit以下だとあまりよろしくないようです。
https://hnw.hatenablog.com/entries/2014/07/05
上記のブログの著者は以下のように述べていますが、今回自分はGitHubに書かれている方を使いました。
2048bit RSA鍵で2030年まで戦えるらしいので当分それでいいと思っています。闇雲にSSHの鍵長を長くするとDH鍵交換やその他の暗号化プロセスも計算量が増えて、結果として処理が重くなってしまうんじゃないでしょうか。何事もトレードオフがあるよねってことで。
違う名前で鍵を生成する
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" // 鍵を違う名前で生成する Enter a file in which to save the key (/home/you/.ssh/id_rsa): example_rsa
ssh-keygenして名前を入力すると鍵を違う名前で生成することができます。(上記だとexample_rsaとexample_rsa.pub) デフォルト(なにも入力せずにエンター)ではid_rsaとid_rsa.pubが生成されます。
※名前を指定しない時は勝手に~/.ssh内に鍵を生成してくれますが、名前を指定するときは~/.ssh内にいないとこの中に生成してくれないです。
違う名前で鍵を生成した場合は接続がうまく行かない可能性があります。 ssh接続の際「~/.ssh/id_rsa」、「~/.ssh/id_dsa」、「~/.ssh/identity」しかデフォルトでは見に行かないからのようです。 ~/.ssh/configに以下を作成する必要があります。
Host github github.com HostName github.com IdentityFile ~/.ssh/id_git_rsa #ここに自分の鍵のファイル名 User git
参考: GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~ - Qiita
SSH鍵の公開鍵をクリップボードにコピーする
上記手順では$ cat id_rsa.pub
で表示されたものをコピーするというダサい感じでしたが、コマンドでももちろんできます。
// Mac $ pbcopy < ~/.ssh/id_rsa.pub // Windows $ clip < ~/.ssh/id_rsa.pub // Linux $ xclip -sel clip < ~/.ssh/id_rsa.pub // xclipがなければ $ sudo apt-get install xclip
https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/#platform-linux
SSH鍵のパスワードの変更
ssh-keygen -p # Start the SSH key creation process Enter file in which the key is (/Users/you/.ssh/id_rsa): [Hit enter] Key has comment '/Users/you/.ssh/id_rsa' Enter new passphrase (empty for no passphrase): [Type new passphrase] Enter same passphrase again: [One more time for luck] Your identification has been saved with the new passphrase.
https://help.github.com/articles/working-with-ssh-key-passphrases