Resolve Git 'Permission Denied (publickey)' errors on Ubuntu 22.04 LTS by troubleshooting SSH key agent issues, ensuring correct key setup, and proper Git host authentication.
When working with Git repositories over SSH, encountering a "Permission Denied (publickey)" error can halt your development or deployment workflows. This issue often signals a problem with how your SSH client (on Ubuntu 22.04 LTS) is presenting its authentication credentials (your SSH private key) to the remote Git server (e.g., GitHub, GitLab, Bitbucket). The error message "SSH key agent missing" further points to the ssh-agent utility, which is responsible for securely managing your private keys in memory.
Symptom & Error Signature
You will typically encounter this error when attempting to perform Git operations that require authentication with a remote server via SSH, such as git clone, git push, or git pull.
The output in your terminal might look similar to one of these:
[email protected]: Permission denied (publickey).
Please make sure you have the correct access rights
and the repository exists.
`
Or, with a more explicit "agent missing" hint (though this part is often inferred rather than explicitly stated by Git itself, ssh might report it with verbose output):
Cloning into 'my-repository'...
[email protected]: Permission denied (publickey).
The authenticity of host 'gitlab.com (X.X.X.X)' can't be established.
ED25519 key fingerprint is SHA256:....
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com' (ED25519) to the list of known hosts.
[email protected]: Permission denied (publickey).
Please make sure you have the correct access rights
and the repository exists.
`
In some verbose scenarios, you might see Agent admitted failure to sign using the key. or no mutual signature algorithm if the agent is running but lacks the correct key.
Root Cause Analysis
The "Permission Denied (publickey)" error, especially when combined with issues related to the ssh-agent, stems from one or more of the following underlying problems:
- Missing or Unloaded Private Key: Your SSH private key (
idrsa,ided25519, etc.) is either not present on your system, or it exists but has not been added to yourssh-agentfor use. Thessh-agentacts as a key manager, allowingsshandgitto use your keys without repeatedly asking for passphrases. -
ssh-agentNot Running or Misconfigured: Thessh-agentprocess, which holds your keys in memory, might not be running in your current shell session, or it may not have been correctly configured to start automatically with your desktop environment or terminal session. - Incorrect Private Key Permissions: SSH keys require strict file permissions (typically
600for the private key and700for the.sshdirectory). If permissions are too liberal,sshwill refuse to use the key for security reasons. - Public Key Not Registered with Git Hosting Service: Even if your private key is correctly set up locally, the corresponding public key (
idrsa.pub,ided25519.pub) must be uploaded and registered with your Git hosting provider (GitHub, GitLab, Bitbucket) for your user account. The remote server uses this public key to verify your identity. - Incorrect Git Remote URL: You might be attempting to use an HTTPS remote URL (e.g.,
https://github.com/user/repo.git) instead of an SSH URL (e.g.,[email protected]:user/repo.git). SSH authentication only works with SSH-formatted remote URLs. -
~/.ssh/configIssues: If you have a custom SSH configuration file (~/.ssh/config), it might be misconfigured, pointing to the wrong key, or overriding necessary default behaviors. - Key Passphrase Not Provided: If your private key is encrypted with a passphrase, and the
ssh-agentis not running or the passphrase wasn't provided when adding the key to the agent, authentication will fail.
Step-by-Step Resolution
Follow these steps sequentially to diagnose and resolve the "Permission Denied (publickey)" error.
1. Verify SSH Key Existence and Permissions
First, ensure you have an SSH key pair and that its permissions are correctly set.
- Check for existing keys:
-
`bash - ls -al ~/.ssh/
-
` - Look for files like
idrsa,idrsa.pub,ided25519,ided25519.pub. If you see only.pubfiles or noid_*files, you might not have a key.
- If no key exists, generate a new one:
-
`bash - ssh-keygen -t ed25519 -C "[email protected]"
-
` - > [!NOTE]
- >
ed25519is the recommended modern key type. You can usersawith-b 4096for older compatibility if needed. - When prompted for a file to save the key, press
Enterto accept the default (~/.ssh/id_ed25519). - Always set a strong passphrase for your private key. This encrypts your private key at rest, adding a crucial layer of security.
- Set correct permissions for your
.sshdirectory and keys: -
`bash - chmod 700 ~/.ssh
- chmod 600 ~/.ssh/ided25519 # Or idrsa if you use RSA
- chmod 644 ~/.ssh/ided25519.pub # Or idrsa.pub
-
` - > [!IMPORTANT]
- > Incorrect permissions are a very common cause of SSH authentication failures. The private key must only be readable by the owner.
2. Start ssh-agent and Add Your Private Key
The ssh-agent process is key to managing your SSH identities.
- Check if
ssh-agentis running: -
`bash - eval "$(ssh-agent -s)"
-
` - If it's already running, it will output something like
SSHAUTHSOCK=/tmp/ssh-XXXXXX/agent.YYYY; export SSHAUTHSOCK; SSHAGENTPID=ZZZZZ; export SSHAGENTPID; echo Agent pid ZZZZZ;. If it wasn't running, this command will start it and set the necessary environment variables.
- Add your private key to
ssh-agent: -
`bash - ssh-add ~/.ssh/id_ed25519 # Use the path to your private key
-
` - If your key has a passphrase, you will be prompted to enter it.
- If you have multiple keys, add them all.
- You can check which keys are loaded with
ssh-add -l.
[!NOTE] > On Ubuntu,
ssh-agentis often started automatically with your desktop environment. However, it might not be running in a specific terminal session, or it might not have your key loaded. Theeval "$(ssh-agent -s)"andssh-addcommands are typically needed once per session or set up for persistence.
3. Verify Public Key on Git Hosting Service
Your remote Git server needs to know your public key to authenticate you.
- Copy your public key to clipboard:
-
`bash - cat ~/.ssh/id_ed25519.pub
-
` - Copy the entire output, starting with
ssh-ed25519(orssh-rsa) and ending with your email address.
- Add the public key to your Git hosting provider:
- * GitHub: Go to
Settings->SSH and GPG keys->New SSH key. Paste your public key. - * GitLab: Go to
User Settings->SSH Keys. Paste your public key. - * Bitbucket: Go to
Profile settings->SSH keys. Add key. - * Self-hosted Git (e.g., Gitea, plain Git): You'll typically add it to the
~/.ssh/authorized_keysfile of thegituser on the server.
4. Validate Git Remote URL
Ensure your local Git repository is configured to use the SSH remote URL, not HTTPS.
- Check current remote URLs:
- Navigate to your repository directory and run:
-
`bash - git remote -v
-
` - Look for URLs starting with
git@(SSH) rather thanhttps://.
Example of an SSH URL:
`
origin [email protected]:yourusername/yourrepository.git (fetch)
origin [email protected]:yourusername/yourrepository.git (push)
`
Example of an HTTPS URL (which will cause issues):
`
origin https://github.com/yourusername/yourrepository.git (fetch)
origin https://github.com/yourusername/yourrepository.git (push)
`
- Change remote URL to SSH if necessary:
- If you're using an HTTPS URL, update it to SSH:
-
`bash - git remote set-url origin [email protected]:yourusername/yourrepository.git
-
` - Replace
yourusername/yourrepository.gitwith your actual repository path.
5. Test SSH Connection
Test your SSH connection to the Git hosting service. This verifies that your local SSH setup can authenticate.
ssh -T [email protected]
# Or for GitLab: ssh -T [email protected]
# Or for Bitbucket: ssh -T [email protected]
```
[!TIP] Use the
-vflag for verbose output (ssh -vT [email protected]) if you're still facing issues. This can provide valuable debugging information about which keys are being tried and why they're failing.
6. Troubleshoot ~/.ssh/config (Advanced)
If you have a ~/.ssh/config file, it might be interfering.
- Inspect
~/.ssh/config: -
`bash - cat ~/.ssh/config
-
` - Look for
Hostentries related to your Git provider (e.g.,Host github.com). Ensure anyIdentityFiledirectives point to the correct private key and that there are no conflicting options.
A minimal working configuration might look like this:
`
Host github.com
Hostname ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
UseKeychain yes # macOS specific, ignore on Ubuntu
`
The Port 443 and Hostname ssh.github.com lines are useful if you're behind a firewall that blocks standard SSH port 22.
- Temporarily disable
~/.ssh/config: - Rename it (
mv ~/.ssh/config ~/.ssh/config_backup) and try connecting again. If it works, the issue is in your config file.
7. Persist ssh-agent Across Sessions (Optional but Recommended)
To avoid re-running eval "$(ssh-agent -s)" and ssh-add every time you open a new terminal or reboot, configure your shell to handle this automatically.
- For Bash users:
- Edit your
~/.bashrcfile: -
`bash - nano ~/.bashrc
-
` - Add the following lines at the end:
-
`bash - # Start ssh-agent if not running
- if [ -z "$SSHAUTHSOCK" ]; then
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_ed25519 # Add your private key here
- fi
-
` - Save the file and exit (
Ctrl+X,Y,Enter). - Then, apply the changes:
source ~/.bashrc.
- For Zsh users:
- Edit your
~/.zshrcfile: -
`bash - nano ~/.zshrc
-
` - Add the same lines as for Bash. Save, exit, and then
source ~/.zshrc.
[!WARNING] > While convenient,
ssh-addin.bashrc/.zshrcwill prompt for your passphrase every time a new shell is opened if the agent doesn't already have the key. A more robust solution might involve usingkeychain(if available on Ubuntu) or relying on desktop environment integration. For most server environments where you login via SSH directly, the aboveifblock is a practical solution.
By systematically following these steps, you should be able to resolve the "Permission Denied (publickey)" error caused by SSH key agent issues on your Ubuntu 22.04 LTS system and resume your Git operations.