Skip to main content
How to Install and Set Up SSH for GitHub on Linux, Windows, and MacOS

How to Install and Set Up SSH for GitHub on Linux, Windows, and MacOS

·670 words·4 mins
Zarvelion Zynji
Author
Zarvelion Zynji
Tech enthusiasts (self-proclaimed). Gaming addict (diagnosed). Anime simp (no regrets). I turn my hyperfixations into contentβ€”welcome to the chaos.
Table of Contents

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
#

  1. Log in to GitHub.
  2. Go to Settings β†’ SSH and GPG keys.
  3. Click New SSH key.
  4. 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.com
    

    Type yes when 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/config to 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.


Related