新しいリポジトリを作成し、git pushしようとしたら以下のエラーが出てきて、pushがうまくいかなかった。
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.
前にも見たことがあったけど、どうやって対処しようかググってたが、結構対処法が散らばっていたので、備忘録としてまとめておく。
よく見かけたのは
http://tusukuru.hatenablog.com/entry/2018/08/29/021651
のような感じで、ssh-keygenを行ってid_rsaを作成するものだが、今回は他のアカウントも持っていたので、別名で公開鍵・秘密鍵を作成したかった。
1. .ssh/configの修正
vim ~/.ssh/config
# 元々あったやつ
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
User git
# 今追記したやつ
host github-hogehoge
hostname github.com
IdentityFile ~/.ssh/id_rsa_github_private
User hogehoge
ここにも書いてありますが、このhogehoge
がgithubのアカウント名となります。
id_rsa_github_private
は作成する秘密鍵のファイル名となります.名前はなんでも大丈夫です。
2. 鍵の作成
ssh-keygen -t rsa -C hoge@example.com -f id_rsa_github_private
hoge@example.com
にはgithubで使用しているメールアドレスを入力します。
パスワードを入れるかenterを押すなりして進めます。
id_rsa_github_private
がカレントディレクトリにあると思うので、~/.ssh/の方に移動しておきます。
mv id_rsa_github_private* ~/.ssh/
これで~/.ssh/配下に必要なものが揃いました。
% ls ~/.ssh/
config id_rsa.pub id_rsa_github_private.pub
id_rsa id_rsa_github_private known_hosts
3. githubの方に公開鍵を設定
https://github.com/settings/profile
のSSH keys に new ssh keyから新しい鍵を登録します。
keyの中に~/.ssh/id_rsa_github_private.pub
の内容をコピペします。
以下のコマンドを実行すると接続確認が取れます。
% ssh -T git@github-hogehoge
Hi hogehoge! You've successfully authenticated, but GitHub does not provide shell access.
4. git pushをする
今回はgit pushをしようとしたときにエラーが出ていました。cloneの場合はここ を参考にすると良いかと思います。
pushする際にremote addをする必要がありますが、そのときに
git remote set-url origin git://git@github.com:hogehoge/repository.git
となっていますが、これではなく、
git remote set-url origin git://git@github-hogehoge:hogehoge/repository.git
となります。(repositoryのところは作成したリポジトリ名となります)
これで、git push origin master
をするとうまくpushされました