GitHubにSSHで接続する方法

~/.sshに移動


cd ~/.ssh

鍵生成


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

Enter file in which to save the key (/Users/simmon/.ssh/id_rsa): # そのままEnter
Enter passphrase (empty for no passphrase): # そのままEnter
Enter same passphrase again: # そのままEnter

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/simmon/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/simmon/.ssh/id_rsa
Your public key has been saved in /Users/simmon/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:N6sOZ8siZ3HtfMHyFCs9nDIWy265sKG7R+NcdBCC7Ss smn.mori@urcareer.jp
The key's randomart image is:
+---[RSA 4096]----+
|       o. ..     |
|      . ...      |
|       .   .     |
|        . o o    |
|        S=oB +   |
|      E =.@oX    |
|      .B=B.O o   |
|    . +*+** o    |
|     ++**..o     |
+----[SHA256]-----+

id_rsa.pubid_rsaが作成できたことを確認


ls
id_rsa.pub     id_rsa

公開鍵をコピーし、GitHubに登録

cat id_rsa.pubで公開鍵を表示してコピー、GitHubの「Settings」→「SSH and GPG keys」→「New SSH key」に貼り付けて登録。


cat id_rsa.pub

~/.ssh/configに下記を追加


Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  User git

接続確認

下記コマンドを実行し、Hi ! You've successfully authenticated, but GitHub does not provide shell access.と表示されれば成功。


ssh -T github