GitのPermission denied (publickey)を直す方法
Permission denied (publickey) は、GitHub などのリポジトリに SSH で git clone や git push をしたとき、サーバーが公開鍵での認証を断ると出るエラーです。手元で SSH の鍵を作り、公開鍵をサーバーに登録すると消えます。鍵を作ったのに消えないときは、ssh -v で、どの鍵が送られているかを見ると原因が分かります。
検証環境は Ubuntu 24.04、Git 2.43.0、OpenSSH 9.6p1 です。同じ VM の中に、公開鍵の認証だけを受け付ける SSH の Git サーバーを立て、git.example.com という名前でつなぎました。サーバーのユーザーは git、手元のユーザーは neko です(どちらも検証用に作ったものです)。サーバーのホスト鍵は、あらかじめ known_hosts に登録してあります。コマンドの出力に出る手元のホームフォルダーのパスは ~ に置き換えています。GitHub への接続、Windows・macOS、ssh-agent に鍵を読み込ませる方法は試していません。
エラーの全文
鍵を1つも持っていない状態で、SSH の URL を使って clone すると、次のように止まりました。
$ whoami; ls -A ~/.ssh
neko
known_hosts
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"
Cloning into 'app'...
git@git.example.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
exit=128
Cloning into 'app'... の次の行の git@git.example.com: Permission denied (publickey). は、Git ではなく ssh が出しているメッセージです。git@git.example.com というユーザーとサーバーに、公開鍵(publickey)の認証で入ろうとして断られた、という意味です。そのあとの fatal: Could not read from remote repository. からの3行は、Git が「リモートのリポジトリを読めなかった。アクセス権があるか、リポジトリがあるかを確かめてください」と案内しているだけです。この例では、リポジトリの名前を間違えたわけではありません。
Git を通さずに ssh -T でつなぐと、ssh のメッセージだけが出るので、SSH の認証だけを確かめられます。
$ ssh -T git@git.example.com; echo "exit=$?"
git@git.example.com: Permission denied (publickey).
exit=255
ssh -v で、どの鍵を試したかを見る
-v を付けると、ssh が認証のときに何をしたかが表示されます。出力は長いので、鍵に関わる行だけを抜き出しました。
$ ssh -v -T git@git.example.com 2>&1 | grep -E "Authentications that can continue|Trying private key|Will attempt key|No more authentication|Offering|Permission denied"
debug1: Authentications that can continue: publickey
debug1: Will attempt key: ~/.ssh/id_rsa
debug1: Will attempt key: ~/.ssh/id_ecdsa
(中略)
debug1: Trying private key: ~/.ssh/id_rsa
debug1: Trying private key: ~/.ssh/id_ecdsa
debug1: Trying private key: ~/.ssh/id_ecdsa_sk
debug1: Trying private key: ~/.ssh/id_ed25519
debug1: Trying private key: ~/.ssh/id_ed25519_sk
debug1: Trying private key: ~/.ssh/id_xmss
debug1: Trying private key: ~/.ssh/id_dsa
debug1: No more authentication methods to try.
git@git.example.com: Permission denied (publickey).
ssh は、~/.ssh/id_rsa や ~/.ssh/id_ed25519 といった決まった名前のファイルを順に探します。主な既定のファイル名は、ssh_config のマニュアルにある IdentityFile の説明に並んでいます。Trying private key だけが並んで Offering public key が1行もないときは、送れる鍵が見つからなかったということです。GitHub の Permission denied (publickey) の解説にも、同じ読み方が書かれています。
鍵を作っただけでは消えない
鍵は ssh-keygen で作ります。ここでは検証のため、パスフレーズを空(-N "")にしました。
$ ssh-keygen -t ed25519 -C "neko@example.com" -N "" -f ~/.ssh/id_ed25519 -q; ls -l ~/.ssh | awk "NR>1{print \$1, \$NF}"
-rw------- id_ed25519
-rw-r--r-- id_ed25519.pub
-rw-rw-r-- known_hosts
id_ed25519 が秘密鍵、id_ed25519.pub が公開鍵です。この状態でもう一度 clone すると、同じエラーが出ました。
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"
Cloning into 'app'...
git@git.example.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
exit=128
ssh -v で見ると、今度は鍵を送っています。それでも、サーバーがその鍵を知らないので断られます。
$ ssh -v -T git@git.example.com 2>&1 | grep -E "Offering|Server accepts|No more authentication|Permission denied"
debug1: Offering public key: ~/.ssh/id_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8
debug1: No more authentication methods to try.
git@git.example.com: Permission denied (publickey).
Offering public key の行があるのに Server accepts key の行がなければ、鍵は手元にあり、サーバーに登録されていない状態です。
公開鍵をサーバーに登録すると消える
サーバーに渡すのは公開鍵(.pub の付いたほう)です。検証でも、渡したのは公開鍵だけです。
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...(省略) neko@example.com
検証環境では、サーバー側でこの1行を git ユーザーの ~/.ssh/authorized_keys に書き足しました。sshd のマニュアルにあるとおり、ここが、公開鍵の認証に使う公開鍵を並べておく既定のファイルです。
GitHub では、このファイルを自分で書く代わりに、Web の画面から登録します。GitHub に SSH 鍵を追加する手順によると、右上のプロフィール画像から「Settings」を開き、サイドバーの「SSH and GPG keys」で「New SSH key」を押して、「Key」の欄に公開鍵を貼り付けます。
登録したあとに ssh -v を見ると、Server accepts key と Authenticated to の行が出て、認証が通りました。
$ ssh -v -T git@git.example.com 2>&1 | grep -E "Offering|Server accepts|Authenticated to"
debug1: Offering public key: ~/.ssh/id_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8
debug1: Server accepts key: ~/.ssh/id_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8
Authenticated to git.example.com ([127.0.0.1]:22) using "publickey".
同じ clone をやり直すと、エラーは出ずに終わりました。
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"; git -C app log --oneline
Cloning into 'app'...
exit=0
c51c326 最初のコミット
検証用のサーバーは、Git の操作しか受け付けない設定(git-shell)にしてあります。そのため、ssh -T だけを実行すると fatal: Interactive git shell is not enabled. と表示されますが、認証そのものは通っています。GitHub の場合は、SSH 接続の確かめ方にあるとおり、ssh -T で GitHub(github.com)に git ユーザーでつないで、Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. と出れば、認証が通っています。
鍵を登録したのに消えないとき
鍵をサーバーに登録しても、次の3つのどれかに当たると、同じ Permission denied (publickey) が出ました。
ユーザー名が git ではない
URL の @ の前を、自分のユーザー名にしてしまった場合です。
$ git clone neko@git.example.com:repos/app.git app2; echo "exit=$?"
Cloning into 'app2'...
neko@git.example.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
exit=128
メッセージの先頭が neko@git.example.com になっているので、どのユーザーで入ろうとしたかが分かります。ssh -v で見ると、鍵は送っていますが、サーバーの neko ユーザーにはその鍵が登録されていないので受け入れられません。
$ ssh -v -T neko@git.example.com 2>&1 | grep -E "Will attempt key|Offering|Server accepts|No more authentication|Permission denied"
debug1: Will attempt key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: Offering public key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: No more authentication methods to try.
neko@git.example.com: Permission denied (publickey).
この出力は、~/.ssh/config に鍵を指定したあと(次の節の設定)に取ったものです。GitHub の解説にも、接続はすべて git ユーザーで行う必要があり、GitHub のユーザー名でつなぐと失敗すると書かれています。リモートの URL は、@ の前を git にします。
鍵のファイル名が既定と違う
ssh-keygen で -f を使い、~/.ssh/example_ed25519 のような名前で鍵を作った場合です。サーバーにはこの鍵を登録してあっても、ssh は既定の名前のファイルしか探さないので、鍵を送りません。
検証では、さきほどの鍵の名前を変えて、この状態を作りました(サーバーに登録した公開鍵はそのままです)。
$ mv ~/.ssh/id_ed25519 ~/.ssh/example_ed25519; mv ~/.ssh/id_ed25519.pub ~/.ssh/example_ed25519.pub; rm -rf app; ls ~/.ssh
example_ed25519
example_ed25519.pub
known_hosts
known_hosts.old
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"
Cloning into 'app'...
git@git.example.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
exit=128
$ ssh -v -T git@git.example.com 2>&1 | grep -E "Will attempt key|Offering|No more authentication|Permission denied"
debug1: Will attempt key: ~/.ssh/id_rsa
(中略)
debug1: Will attempt key: ~/.ssh/id_dsa
debug1: No more authentication methods to try.
git@git.example.com: Permission denied (publickey).
~/.ssh/config に、このサーバーにはこの鍵を使う、と書くと直りました(User git は接続するユーザー名です)。IdentitiesOnly yes は、ssh_config のマニュアルにあるとおり、ssh-agent が別の鍵を持っていても、ここに書いた鍵だけを使わせる設定です。
$ printf "Host git.example.com\n User git\n IdentityFile ~/.ssh/example_ed25519\n IdentitiesOnly yes\n" > ~/.ssh/config; cat ~/.ssh/config
Host git.example.com
User git
IdentityFile ~/.ssh/example_ed25519
IdentitiesOnly yes
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"; git -C app log --oneline
Cloning into 'app'...
exit=0
c51c326 最初のコミット
ssh -v では、指定した鍵に explicit が付いて送られ、サーバーに受け入れられていました。
$ ssh -v -T git@git.example.com 2>&1 | grep -E "Will attempt key|Offering|Server accepts|Authenticated to"
debug1: Will attempt key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: Offering public key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: Server accepts key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
Authenticated to git.example.com ([127.0.0.1]:22) using "publickey".
秘密鍵をほかのユーザーも読める
秘密鍵のファイルの権限を 644(ほかのユーザーも読める)にすると、ssh は警告を出して秘密鍵を読み込まず、結果として Permission denied (publickey) になりました。
$ rm -rf app; chmod 644 ~/.ssh/example_ed25519; ls -l ~/.ssh/example_ed25519 | awk "{print \$1, \$NF}"
-rw-r--r-- ~/.ssh/example_ed25519
$ git clone git@git.example.com:repos/app.git; echo "exit=$?"
Cloning into 'app'...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '~/.ssh/example_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "~/.ssh/example_ed25519": bad permissions
git@git.example.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
exit=128
ssh のマニュアルにも、ほかのユーザーが読み書きできる秘密鍵のファイルは無視すると書かれています。ssh -v で見ると、公開鍵はサーバーに受け入れられているのに、秘密鍵を読み込めずに断られていました。
$ chmod 644 ~/.ssh/example_ed25519; ssh -v -T git@git.example.com 2>&1 | grep -E "Will attempt key|Offering|Server accepts|bad permissions|No more authentication|Permission denied"; chmod 600 ~/.ssh/example_ed25519
debug1: Will attempt key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: Offering public key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
debug1: Server accepts key: ~/.ssh/example_ed25519 ED25519 SHA256:nL9Hq7jSvAUQNjByjL3jboY/PuuKBd1lZCNrEjphjo8 explicit
Load key "~/.ssh/example_ed25519": bad permissions
debug1: No more authentication methods to try.
git@git.example.com: Permission denied (publickey).
chmod 600 で自分だけが読み書きできる権限に戻すと、clone は通りました。
$ chmod 600 ~/.ssh/example_ed25519; git clone git@git.example.com:repos/app.git; echo "exit=$?"
Cloning into 'app'...
exit=0
ssh -v -T git@<サーバー> の出力で、Offering public key の行がなければ、送れる鍵が見つかっていません(鍵を作っていないか、ファイル名が既定と違う)。Offering public key があって Server accepts key がなければ、その鍵が、@ の前のユーザーに登録されていません。Server accepts key のあとに bad permissions が出ていれば、秘密鍵の権限が広すぎます。




