The Permission denied (publickey)
error occurs when GitHub cannot authenticate your connection using SSH keys. This prevents you from cloning, pulling, or pushing to repositories using SSH. The root causes typically involve misconfigured SSH keys, incorrect permissions, or issues with the SSH agent.
First, check if you already have an SSH key pair on your local machine.
ls -al ~/.ssh
Look for files named id_rsa
and id_rsa.pub
or id_ed25519
and id_ed25519.pub
. If these files exist, you have an SSH key pair set up.
If you do not have an SSH key pair, generate one using the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
If your system does not support ed25519
, use rsa
with a 4096-bit key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Follow the prompts to save the key in the default location (~/.ssh/id_ed25519
or ~/.ssh/id_rsa
).
Start the SSH agent and add your private key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
If you used a different key filename, replace id_ed25519
with your key's name.
Copy your public key to the clipboard:
cat ~/.ssh/id_ed25519.pub
Follow these steps to add the key to GitHub:
Verify that your SSH key is correctly configured by testing the connection to GitHub:
ssh -T git@github.com
You should receive a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Ensure that the repository keli/ctp-python
exists and that your GitHub account has the necessary permissions to access it. If the repository is private, you must be added as a collaborator or have the appropriate access rights.
Double-check that you're using the correct SSH URL for cloning:
git clone git@github.com:keli/ctp-python.git
Ensure there are no typos and that the repository path matches exactly.
Ensure that your private key file has the correct permissions to prevent unauthorized access:
chmod 600 ~/.ssh/id_ed25519
This command restricts the file permissions so that only the owner can read and write the file.
Make sure the email associated with your SSH key matches the email used in your GitHub profile:
git config --global user.email
If it doesn't match, update it:
git config --global user.email "your_email@example.com"
If you're behind a company firewall or proxy, SSH traffic on port 22 might be blocked. To resolve this:
git remote set-url origin https://github.com/keli/ctp-python.git
ssh -T -p 443 git@github.com
After performing the above steps, attempt to clone the repository again:
git clone git@github.com:keli/ctp-python.git
If the issue persists, consider debugging the SSH connection:
ssh -vvv git@github.com
This verbose mode provides detailed information about the SSH connection process, helping identify where the authentication is failing.
To prevent future authentication issues, adhere to the following best practices:
ed25519
over rsa
for better security and performance.chmod 600
).~/.ssh/config
file to manage multiple SSH keys and settings efficiently.Ensure that the correct public key is added to your GitHub account. If multiple keys exist, verify that the one you intend to use is the one linked to GitHub.
If you have multiple SSH keys, specify which key to use for GitHub by configuring the SSH config file:
# ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Ensure that the SSH agent is running and has your key added. You can start the agent and add your key using:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
For added security, you can protect your SSH keys with a passphrase. When generating the key, you will be prompted to enter one. This ensures that even if someone gains access to your private key, they cannot use it without the passphrase.
If you use Git GUI clients like GitKraken or SourceTree, ensure they are configured to use your SSH keys correctly. Refer to the respective documentation for integration steps.
Encountering the Permission denied (publickey)
error when attempting to clone a GitHub repository via SSH is a common issue that can be resolved by carefully following a series of troubleshooting steps. By verifying the existence and correctness of your SSH keys, ensuring they are properly added to your GitHub account, and confirming your repository permissions, you can effectively overcome this authentication hurdle. Additionally, adhering to SSH best practices and addressing common configuration issues will help maintain a smooth and secure workflow with Git and GitHub.