Encountering the ‘Permission denied (publickey)’ error in Git can be frustrating, especially when you’re trying to push or pull code to/from a remote repository. This error typically indicates an issue with your SSH key configuration. In this guide, we’ll take you through various steps to troubleshoot and resolve this error.
Check for Existing SSH Keys
First, you need to check if you already have an SSH key pair (public and private key) on your local machine. Open your terminal and type:
ls -al ~/.ssh
This command will list the files in your .ssh
directory. Look for files named id_rsa
and id_rsa.pub
or id_ed25519
and id_ed25519.pub
. If these files exist, you already have an SSH key pair.
Generate a New SSH Key Pair (if needed)
If no SSH key pair exists, you need to generate one. Use the following command to generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
or, for enhanced security, you might want to use:
ssh-keygen -t ed25519 -C "your_email@example.com"
Follow the prompts and save the key in the default location (typically ~/.ssh
). You can also add a passphrase for extra security.