Introduction
When working with Git repositories hosted on platforms like GitHub, you might encounter a "Permission denied (publickey)" error when attempting to push or pull changes. This usually indicates a problem with authentication. While you can use HTTPS for Git operations, utilizing SSH keys provides a more secure and convenient method, eliminating the need to repeatedly enter your username and password. This tutorial will guide you through the process of generating an SSH key, adding it to your GitHub account, and configuring your local Git environment.
Understanding SSH Keys
SSH (Secure Shell) keys are a pair of cryptographic keys: a private key (kept secret on your computer) and a public key (shared with the remote server, in this case, GitHub). When you attempt to connect to GitHub, it uses these keys to verify your identity without requiring a password.
Generating an SSH Key Pair
If you don’t already have an SSH key pair, you’ll need to generate one. Open your terminal and use the following command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-t rsa
: Specifies the type of key to generate (RSA is a common and secure choice).-b 4096
: Sets the key size to 4096 bits, providing a higher level of security.-C "[email protected]"
: Adds a comment to the key, typically your email address, for identification purposes.
The command will prompt you to enter a file in which to save the key. The default location (~/.ssh/id_rsa
) is recommended. You’ll also be prompted to enter a passphrase. While optional, a passphrase adds an extra layer of security; however, you’ll need to enter it each time you use the key (unless you use an SSH agent, explained later).
This command creates two files in the ~/.ssh/
directory:
id_rsa
: Your private key. Keep this file secret and never share it with anyone!id_rsa.pub
: Your public key. This is the key you will share with GitHub.
Adding Your Public Key to GitHub
-
Copy your public key: Open the
id_rsa.pub
file in a text editor or use the following command to display its contents:cat ~/.ssh/id_rsa.pub
Select and copy the entire key, starting with
ssh-rsa
orssh-ed25519
and ending with your email address. -
Add the key to your GitHub account:
- Log in to GitHub.
- Click on your profile picture in the top-right corner and select "Settings".
- In the left sidebar, click on "SSH and GPG keys".
- Click the "New SSH key" or "Add SSH key" button.
- Give the key a descriptive title (e.g., "My Laptop").
- Paste your public key into the "Key" field.
- Click "Add SSH key".
Configuring Your Git Remote URL
Ensure your Git remote URL is set to use the SSH protocol. You can verify this using:
git remote -v
The URL should look like this: [email protected]:YourUsername/YourRepository.git
.
If it uses HTTPS (e.g., https://github.com/YourUsername/YourRepository.git
), you can change it to use SSH with the following command:
git remote set-url origin [email protected]:YourUsername/YourRepository.git
Replace YourUsername
and YourRepository
with your actual GitHub username and repository name.
Using an SSH Agent (Optional but Recommended)
Typing your passphrase every time you use your SSH key can be cumbersome. An SSH agent manages your private keys in memory, allowing you to authenticate without re-entering your passphrase.
-
Start the SSH agent:
eval `ssh-agent -s`
-
Add your private key to the agent:
ssh-add ~/.ssh/id_rsa
If you used a different filename for your private key, replace
~/.ssh/id_rsa
with the correct path. You’ll be prompted for your passphrase if you set one.
Now, the SSH agent will handle authentication for you, and you won’t need to enter your passphrase again until you restart the agent or your computer.
Multiple SSH Keys
If you have multiple SSH keys (e.g., for different GitHub accounts or projects), you may need to create a configuration file in your ~/.ssh/
directory (e.g., ~/.ssh/config
) to specify which key to use for each host. For example:
Host github.com
IdentityFile ~/.ssh/github_rsa
This tells SSH to use the github_rsa
key when connecting to GitHub. Replace github_rsa
with the filename of your key.
Troubleshooting
- Permission denied (publickey): Double-check that you’ve added the correct public key to your GitHub account and that your remote URL is set to use SSH.
- Still prompted for a password: Ensure that the SSH agent is running and that you’ve added your private key to it. Also, verify that your remote URL is correct.
- Incorrect key being used: If you have multiple keys, review your
~/.ssh/config
file to ensure that the correct key is being used for the desired host.