A few years ago, when I first learned Git on Linux, I used the HTTPS method to push code to GitHub. I thought it was normal, but over time it became annoying. Every time I pushed, I had to enter my username and password. Doing that several times a day felt like a waste of time.
Then I learned about SSH keys. Once I started using them, my workflow became much smoother. With a one-time setup, I no longer had to log in for every push or pull. The process is also more secure because it uses encryption.
In this article, Iβll guide you through understanding, installing, creating, and testing an SSH key for GitHub. The explanation will be simple so you can follow along easily.
π What is an SSH Key and Why is it Important? #
SSH (Secure Shell) is a protocol for secure communication between your computer and a server. An SSH key is a pair of public and private keys used for passwordless authentication.
If you set up SSH keys for GitHub, you wonβt need to enter your username and password for every git push or git pull. Once configured, the connection is automatic.
Benefits of SSH keys:
- Secure, thanks to end-to-end encryption.
- Convenient, saves time.
- Works on Linux, Windows, and MacOS.
- Supports multiple GitHub accounts with proper configuration.
π οΈ Step 1: Check and Install OpenSSH #
Linux/MacOS
ssh -V
If OpenSSH is not installed:
# Debian/Ubuntu
sudo apt install openssh-client
# Fedora
sudo dnf install openssh-clients
Windows
ssh -V
If missing, enable it via Optional Features in Settings β Apps β Optional features β Add a feature β “OpenSSH Client”.
π Step 2: Generate a New SSH Key #
ssh-keygen -t ed25519 -C "your_github_email"
If ed25519 is not supported:
ssh-keygen -t rsa -b 4096 -C "your_github_email"
Press Enter to use the default location (~/.ssh/id_ed25519). Enter a passphrase for extra security, or press Enter for none.
π Step 3: Start SSH Agent and Add the Key #
Linux/MacOS:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Windows:
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
π Step 4: Copy the Public Key #
cat ~/.ssh/id_ed25519.pub
Copy everything in the output.
π Step 5: Add the SSH Key to GitHub #
- Log in to GitHub.
- Go to Settings β SSH and GPG keys.
- Click New SSH key.
- Enter a Title, paste your public key, click Add SSH key.
β Step 6: Test the SSH Connection to GitHub #
ssh -T git@github.com
If successful:
Hi username! You've successfully authenticated...
π¦ Step 7: Use SSH When Cloning Repositories #
HTTPS:
git clone https://github.com/username/repo.git
SSH:
git clone git@github.com:username/repo.git
βοΈ Step 8: Configure Multiple GitHub Accounts #
Edit ~/.ssh/config:
# Personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# Work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Clone with the specified host:
git clone git@github.com-personal:username/repo.git
π Troubleshooting SSH GitHub Errors #
β Permission denied (publickey)
#
-
Add the key to the agent:
ssh-add ~/.ssh/id_ed25519 -
Ensure the public key is added in GitHub.
-
Fix permissions:
chmod 600 ~/.ssh/id_ed25519 chmod 644 ~/.ssh/id_ed25519.pub
β οΈ Host key verification failed
#
-
Run:
ssh git@github.comType
yeswhen prompted.
π Could not resolve hostname github.com
#
- Check your internet connection.
- Switch to Cloudflare or Google DNS.
ποΈ Wrong SSH Key for a Specific GitHub Account #
- Use
~/.ssh/configto specify the right key.
π§Ή Remove Old Keys #
ssh-add -d ~/.ssh/id_ed25519_old
ssh-add -D
β SSH GitHub FAQ #
1. Can I still use HTTPS after setting up SSH? Yes, SSH and HTTPS can be used independently.
2. Can I use the same SSH key on multiple devices? Yes, but it’s safer to generate separate keys.
3. Do SSH keys expire? No, but itβs good practice to rotate them periodically.
4. Should I use a passphrase? Yes, for extra protection.
5. How do I remove an SSH key from GitHub? Settings β SSH and GPG keys β Delete.
6. Is the setup different on Linux, Windows, and MacOS? The core process is the same, only the SSH Agent activation commands differ.