エラーメッセージ
Permission denied (publickey).
別の表記
- git@github.com: Permission denied (publickey).
- Permission denied (publickey,password).
先頭に `user@host:` が付きます。括弧内のリストは、サーバーがまだ許可している方式を示します。たとえば `(publickey,password)` や `(publickey,gssapi-keyex,gssapi-with-mic)` です。
このエラーの意味
SSH の接続とホストの検証は成功しましたが、サーバーはクライアントが提示した公開鍵をどれも受け入れず、しかもサーバーが許可する方式は公開鍵認証だけです(括弧内のリスト)。鍵が拒否された理由がクライアントに伝わることはなく、その理由はサーバーのログにしか記録されません。GitHub、GitLab、Bitbucket では、同じメッセージは、そのリポジトリに対するアカウント(またはデプロイキー)に手元の鍵がどれも登録されていないことを意味します。
発生する理由
正しい鍵が提示されていない
よくある鍵がエージェントになく、デフォルトのパス(`~/.ssh/id_ed25519`、`id_ecdsa`、`id_rsa`)にもなく、`-i` や `IdentityFile` でも指定されていません。エージェントに多数の鍵がある場合、正しい鍵が試される前にサーバーが接続を切断することがあります(MaxAuthTries)。
鍵がそのユーザーの authorized_keys にない
よくある別のユーザーやホストにインストールされている、再インストールの際に削除された、または(Git ホスティングの場合)リポジトリへのアクセス権を持つアカウントとは別のアカウントに追加されている、といったケースです。
パーミッションが原因で sshd が authorized_keys を無視している
ときどき`StrictModes yes`(デフォルト)の場合、ホームディレクトリ、`~/.ssh`、`authorized_keys` のいずれかがグループまたは全員に書き込み可能だと、sshd はファイルを拒否します。サーバーのログには「Authentication refused: bad ownership or modes」と記録されます。
鍵の署名アルゴリズムが受け入れられない
ときどき古いクライアントやライブラリが SHA-1 の `ssh-rsa` アルゴリズムで使う RSA 鍵は、OpenSSH 8.8 以降のサーバーで拒否されます。DSA 鍵は、OpenSSH 10 で完全に廃止されました。サーバーのログには「not in PubkeyAcceptedAlgorithms」と記録されます。
リモートのユーザー名が間違っている
ときどき`ssh server` はローカルのユーザー名でログインします。クラウドのイメージでは `ubuntu`、`ec2-user`、`admin` が想定されており、Git ホスティングでは常に `git@` が想定されています。
修正方法
- 1.
どの鍵が提示されているかを確認する
`ssh -v` は、クライアントが提示するすべての鍵と、サーバーがそれを受け入れたかどうかを表示します。`ssh -G` は、すべての設定を適用した後の実際のユーザーと identity ファイル(秘密鍵ファイル)を表示します。
bash ssh -v deploy@server.example.com 2>&1 | grep -E "Offering|Server accepts|Authentications that can continue|send_pubkey_test" ssh-add -l # keys the agent offers ssh -G deploy@server.example.com | grep -E "^(user|identityfile|identitiesonly) " - 2.
ホストごとに identity ファイルを固定する
`IdentityFile` と `IdentitiesOnly yes` を組み合わせると、提示される鍵が 1 つだけになり、多数の鍵を持つエージェントで起こる「Too many authentication failures」を避けられます。
text # ~/.ssh/config Host server.example.com User deploy IdentityFile ~/.ssh/deploy_ed25519 IdentitiesOnly yes - 3.
サーバー側を修正し、ログを確認する
正しいユーザーに対して、厳格なパーミッションで公開鍵をインストールし、sshd のログで具体的な拒否理由を確認してください。
bash # On the server: install the key and fix modes (sshd ignores keys in group/world-writable paths) install -d -m 700 ~/.ssh && cat deploy_ed25519.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys sudo journalctl -u ssh --since "10 min ago" | grep -E "Authentication refused|not in PubkeyAccepted|Failed publickey"
関連するエラー
userauth_pubkey: signature algorithm ssh-rsa not in PubkeyAcceptedAlgorithms [p…OpenSSH server (sshd) logsign_and_send_pubkey: signing failed for RSA "/path/to/key" from agent: agent r…OpenSSH client with an SSH agent (ssh-agent, gpg-agent, 1Password, Secretive, Windows OpenSSH agent)ssh: handshake failed: ssh: unable to authenticate, attempted methods [none pub…golang.org/x/crypto/ssh (Go)Load key "/path/to/key": invalid formatOpenSSH