How can I fix the 'Permission denied (publickey)' error when accessing GitHub via SSH?
This error usually means your SSH key is not correctly set up or linked to your GitHub account. Verify and add your SSH key in GitHub settings under SSH and GPG keys.
The 'Permission denied (publickey)' error on GitHub occurs when Git can’t authenticate your identity with the SSH key provided. Start by confirming that an SSH key exists on your local machine. To check, open your terminal and run ls -al ~/.ssh
to list any existing SSH keys, typically named id_rsa
or id_ed25519
. If no key is present, generate one by using the command ssh-keygen -t rsa -b 4096 -C '[email protected]'
. Once generated, add the SSH key to your SSH agent with ssh-add ~/.ssh/id_rsa
. Now, log in to GitHub, navigate to Settings > SSH and GPG keys, and click New SSH key. Paste the copied SSH key (found in ~/.ssh/id_rsa.pub
) into the provided field. Once added, try accessing GitHub again. If issues persist, verify that you’re connecting using SSH by running git remote -v
. Ensure the remote origin URL uses the SSH format ([email protected]:username/repo.git
) instead of HTTPS. These steps should resolve the error by correctly linking your SSH key with GitHub.