How to Set Up SSH Key for GitHub: Step-by-Step Guide
Generate an SSH key pair using
ssh-keygen, then add the public key to your GitHub account under Settings > SSH and GPG keys. This allows secure, password-free access to your GitHub repositories via SSH.Syntax
Use the ssh-keygen command to create a new SSH key pair. The basic syntax is:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"-t rsa: Specifies the RSA algorithm for the key.-b 4096: Sets the key size to 4096 bits for strong security.-C "your_email@example.com": Adds a label (usually your email) to identify the key.
After generating, you add the public key to GitHub to enable SSH authentication.
bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:... your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
| ... |
+----[SHA256]-----+
Example
This example shows how to generate an SSH key, start the SSH agent, add your private key, and then copy the public key to add it to GitHub.
bash
ssh-keygen -t rsa -b 4096 -C "user@example.com" eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa cat ~/.ssh/id_rsa.pub
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
Agent pid 12345
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC... user@example.com
Common Pitfalls
- Not adding the public key (
id_rsa.pub) to GitHub causes authentication failures. - Using a passphrase but not running
ssh-agentto cache it can cause repeated password prompts. - Copying the private key (
id_rsa) instead of the public key is a security risk and will not work. - Incorrect file permissions on
~/.sshor key files can block SSH from working.
bash
Wrong (copy private key): cat ~/.ssh/id_rsa Right (copy public key): cat ~/.ssh/id_rsa.pub
Quick Reference
| Step | Command / Action | Description |
|---|---|---|
| 1 | ssh-keygen -t rsa -b 4096 -C "your_email@example.com" | Generate a new SSH key pair |
| 2 | eval "$(ssh-agent -s)" | Start the SSH agent in the background |
| 3 | ssh-add ~/.ssh/id_rsa | Add your private key to the SSH agent |
| 4 | cat ~/.ssh/id_rsa.pub | Display your public key to copy |
| 5 | Add public key to GitHub | Paste key in GitHub under Settings > SSH and GPG keys |
Key Takeaways
Generate an SSH key pair with ssh-keygen using RSA and 4096 bits for strong security.
Add the public key (~/.ssh/id_rsa.pub) to your GitHub account to enable SSH access.
Use ssh-agent and ssh-add to manage your private key passphrase smoothly.
Never share your private key; only the public key should be uploaded to GitHub.
Check file permissions and ensure the SSH agent is running to avoid connection issues.