githubのssh接続設定

今更ながらgithubを登録して使用してみたところ、ssh接続設定で「Permission denied (publickey).」と怒られてしまったのでメモ。

細かいところまでは必要ないだろうけど、使用しているPCはMacです。

簡潔に手順だけ書くと以下のようになる。

1. 鍵の作成
下記のように作成。

$ cd ~/.ssh/
$ ssh-keygen -ssh

$ ssh-keygen -t rsa -b 4096 -C "コメント"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/hoge/.ssh/id_rsa): id_rsa_github
----
省略
----

$ pbcopy < ./id_rsa_github.pub

2. sshのconfigを設定
下記のファイルに書き込む。

■~/.ssh/config

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

3. githubに公開鍵を登録
貼り付けるだけなので省略。

4. githubレポジトリの作成
ポチポチするだけなので省略(笑)

すると、下記のような画面が出てくるはず。

f:id:tabucchi33b:20190919191023p:plain
quick_setup画面

ようは「git@github.com:tabucchi33b/test.git」でssh接続できるようになるよ〜というので、テストがてら試しに上記の画像の通りに試してみる。

$ echo "# test" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin git@github.com:tabucchi33b/test.git
$ git push -u origin master
  git@github.com: Permission denied (publickey).
  fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

うん...さーせん。
というわけでsshの書き方に習って変更(ユーザ名の書き方あってる?)。

$ git remote set-url origin "github:tabucchi33b/test.git"

これで動くことは動くけど、例と違う書き方でしっくりこないし、他人に毎回説明するのもめんどいので、「~/.ssh/config」の「Host」を設定を変更する。

■~/.ssh/config

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

これで、githubに記載の「git@github.com:tabucchi33b/test.git」でアクセスできる。