Home / Blog / Runners and limits
How do I fix "Permission denied (publickey)" in GitHub Actions?
"Permission denied (publickey)" means the SSH server accepted no key the job offered. Five causes, the commands that show which, and the fix for each.
“Permission denied (publickey).” means the SSH server accepts only public-key logins and the job offered no key it accepts, so ssh gave up and exited with code 255 (when git runs ssh, the step shows git’s exit code, 128). In GitHub Actions the 3 usual causes are that no private key was loaded in the job, the secret does not hold the private key, or the matching public key is not registered where the job connects.
- The server takes public keys only, and ssh had no key it accepts; ssh exits with code 255, and a git step shows 128.
- A job starts with no SSH key of yours: it has to arrive as a secret and be loaded in that job.
- The secret must hold the whole private key, from its BEGIN line to its END line, with no passphrase; a key file needs chmod 600.
- The public half must be registered where the job connects; a deploy key covers one repository and is read-only by default.
- ssh -vT git@github.com shows whether a key was offered and refused, or none was found.
The full line is usually git@github.com: Permission denied (publickey)., with the user and host in front, and git adds “fatal: Could not read from remote repository.” after it. This post covers what the message means, the causes in the order to check them, how to see which key was offered, and the fix for each.
What does “Permission denied (publickey)” mean?
The words in brackets are the server’s list of login methods it would still accept. In OpenSSH’s client the message is printed when that list has no method left to try, with the user and host in front. So “(publickey)” says the server takes keys only, and the denial says every key the client offered was refused, or that it had none to offer. GitHub’s servers are of this kind, and so is any server with password logins turned off.
A runner starts each job with no SSH key of yours. Your laptop has one in ~/.ssh, which is why the same command works there. In a job the key has to arrive as a secret and be handed to ssh, and each cause below is one way that fails.
What causes it?
1. No key was loaded in this job. A key set up in one job is not there in the next. The README of webfactory/ssh-agent, an action that loads a key into an SSH agent, says the key is available only in the job that references the action, because each job runs on a fresh machine. A workflow that loads the key in a build job and connects in a deploy job fails this way.
2. The secret does not hold the private key. The same README is specific about what the secret must contain: the contents of the private key file, which starts with -----BEGIN ... PRIVATE KEY-----, runs to many lines and ends with the matching END line, with no passphrase set. A secret holding the .pub file, one line of the key, or a key with a passphrase does not give ssh a key it can use.
A key file written in a step must be chmod 600. With wider permissions ssh warns “UNPROTECTED PRIVATE KEY FILE”, ignores the key, and the server answers with this denial.
3. The public key is not registered where the job connects. The server has to know the public half. For a GitHub repository that means a deploy key on that repository, or a key on an account that can read it. GitHub’s documentation says a deploy key grants access to a single repository and is read-only unless you tick “Allow write access”. For your own server it means a line in the login user’s ~/.ssh/authorized_keys, the file the ssh manual gives as the list of keys permitted to log in.
4. The wrong user. GitHub’s troubleshooting page for this error says every connection must be made as the user git, and shows this exact message for a connection made with a GitHub username instead. On your own server the equivalent is logging in as a user whose authorized_keys does not have the key. The same page warns about sudo: a command run with it does not use the keys set up without it.
5. Several deploy keys, and the wrong one first. With two private repositories you need two deploy keys, and the README of webfactory/ssh-agent explains the trap: GitHub accepts the first key it knows, which may belong to the other repository. That case ends in “Could not read from remote repository” without the permission line. The action solves it by reading each key’s comment, so create each key with its repository’s address as the comment.
How do I tell which one I have?
Add a step before the failing one that asks ssh what it has. GitHub’s troubleshooting page uses two commands: ssh-add -l -E sha256 lists the keys the agent holds, and the page says that if it prints no key you have none loaded, and ssh -vT git@github.com shows the attempt.
steps:
- name: Which keys does this job have?
run: |
ssh-add -l -E sha256 || true
ssh -vT git@github.com 2>&1 | grep -E 'Offering|Trying|No more|denied|authenticated' || true
Read the output against GitHub’s example. No key listed, and lines that say “Trying private key” followed by “No more authentication methods to try”, mean ssh found no key: cause 1 or 2. A line that says “Offering public key” followed by the denial means a key was offered and refused: cause 3, 4 or 5.
How do I fix it?
To clone or push with a deploy key, give the private key to checkout. Its ssh-key input configures that repository’s local git config to use the key, and removes it after the job. github.com’s host key is added for you.
steps:
- uses: actions/checkout@v4
with:
repository: octo-org/private-tools
ssh-key: ${{ secrets.TOOLS_DEPLOY_KEY }} # the private key; its public half is a deploy key on that repository
For anything else that uses ssh, a package manager fetching a private dependency or a deploy to your own server, load the key into an agent in the same job. With webfactory/ssh-agent that is one step, and the secret can hold several keys.
steps:
- uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- run: npm ci # can now fetch git+ssh dependencies
If that step itself fails with Error loading key "(stdin)": invalid format, the README’s answer is that the key is not in PEM format, and it gives the ssh-keygen -p -m pem command to convert it. Then check the other end: the public key as a deploy key with the access the job needs, the user git for GitHub, and for your own server the right user and its authorized_keys.
What if the address is HTTPS, not SSH?
Then this is not your error. An HTTPS address fails with a different message, covered in the post on “could not read Username”, and is fixed with a token, not a key. The ssh exit code 255 is in the exit code post, the checkout reference lists the ssh-key input with the others, and the security hardening post covers what a deploy key should be allowed to do.
The key and the refusal belong to you and the server you connect to, so this error is the same on any runner, ours included. Every size we run, with its memory, disk and rate a minute, is on our managed GitHub Actions runners page.
Sources
- sshconnect2.c (OpenSSH source) (github.com)
- ssh(1) manual page (OpenSSH source) (github.com)
- Error: Permission denied (publickey) (docs.github.com)
- webfactory/ssh-agent (github.com)
- Managing deploy keys (docs.github.com)
- actions/checkout (github.com)
- authfile.c (OpenSSH source) (github.com)
- usage.c (git source) (github.com)
Frequently asked questions
How do I fix "Permission denied (publickey)" in GitHub Actions?
Give the job a private key the server accepts. The 3 usual causes are that no key was loaded in the job, the secret does not hold the private key, or the matching public key is not registered where the job connects. ssh exits with code 255; through git, the step shows 128.
What causes it?
No key was loaded in this job; the secret holds the public key, part of the key or a key with a passphrase; the public key is not registered on the repository or server; the connection uses the wrong user; or the first of several deploy keys belongs to another repository.
How do I tell which one I have?
Run ssh-add -l and ssh -vT git@github.com in a step before the failing one. Trying private key followed by No more authentication methods means ssh found no key. Offering public key followed by the denial means a key was offered and refused.
What if the address is HTTPS, not SSH?
Then this is not your error. A git address that starts with https fails with a message about not being able to read a username, and is fixed with a token, not a key.