When connecting to an existing GitHub repository, you can use SSH keys (public and private) for authentication. For instructions on how to create an SSH key and add it to GitHub, refer to the official GitHub documentation: https://docs.github.com/cn/authentication/connecting-to-github-with-ssh.
Key steps to note:
- You need to create a
.ssh
folder in your home directory and copy the SSH private key into it. You do not need to copy the public key into this folder. Instead, paste the contents of your public key into the Settings section of your GitHub account by following the steps here: Adding a new SSH key to your GitHub account. - The file name of your SSH private key should be
id_rsa
, as this is one of the default names git looks for when locating the private key. If the file is named differently, you will encounter an error such as “[email protected]: Permission denied (publickey).” If this happens, rename your private key file toid_rsa
. - If you’re using a Linux-based OS like Ubuntu, ensure that no other users can read or write to the private key file. Run the following commands:
chmod o-rwx ./id_rsa
chmod g-rwx ./id_rsa
After completing the setup, you can use the SSH private key in your .ssh
folder for authentication and communication with GitHub. To verify the setup, open Git Bash and run the following command:
ssh -T [email protected]
If you see a message like this:
Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.
It means you’ve successfully set up SSH key authentication with GitHub.
References: