経緯
ObsidianGitでリモートリポジトリにプッシュしようとしたらこんなエラーがでてプッシュできなくなった。
ssh_askpass: exec(/usr/X11R6/bin/ssh-askpass): No such file or directory
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 key(公開鍵)を登録するとpushできるようになるらしい。
ん?
ObsidianでHugo用ノートを更新したら自動で公開する方法でもエラーでてSSH keyも登録したと思うのだけれどな。
その時とエラー表示違うし、とりあえず再設定する。
参考ページは下記だが、そのまましてもエラーがあったので少し違うこともした。
また、今後同じことがあるかもしれないので、記しておく。
SSH keysの作成と登録
鍵の作成
ssh-keygen -t rsa -b 4096 -C "hoge@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/<UserName>/.ssh/id_rsa): /Users/<UserName>/.ssh/id_rsa_github # 秘密鍵の名前を指定
Enter passphrase (empty for no passphrase): #パスワードを入力
Enter same passphrase again: # パスワードを入力
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
鍵の確認
ls -l /Users/<UserName>/.ssh
-rw------- 1 <UserName> staff 3434 1 9 16:25 id_rsa_github # 秘密鍵
-rw-r--r--@ 1 <UserName> staff 748 1 9 16:25 id_rsa_github.pub # 公開鍵
GitHubに公開鍵を登録する
下記コマンドで、id_rsa_github.pubの中身がクリップボードに小コピーされる。
pbcopy < ~/.ssh/id_rsa_github.pub
GitHubにログインしてユーザーページのsettingにある「SSH and GPG keys」ページに移動
「New SSH keys」ボタンから登録
Titleはわかりやすいものにし、keyのところにコピーした公開鍵をペーストして「Add SSH key」ボタンで登録
この際に、ペーストしたものにメールアドレスとその前にスペースが含まれているので、それを削除しておく。
configファイルの設定
vim ~/.ssh/config
と打ってconfigファイルを作成する。
すでにある場合は、中身が表示される。
iでインサートモードになるので編集し、escキーで戻り、:wqで終了
中身はこんなかんじ。
Host github github.com
User git
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github #秘密鍵を指定
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent yes
Macの場合、「AddKeysToAgent yes」と「UseKeychain yes」を記述しておかないと思ったように動いてくれなかった。
VSCodeとかObsidianGitで動かなかった。
ssh-agentに鍵を登録
ssh-add -K /Users/<UserName>/.ssh/id_rsa_github
ログイン確認
ssh -T git@github.com
Hi <UserName>! You've successfully authenticated, but GitHub does not provide shell access.
GitHubが出迎えてくれたらひとまずOK
各種Git関連アプリからリモートリポジトリにアクセスできるかも確認しておきましょう。
おつかれさまでした。