GeekDas
••11 min read•
GeekdasGeekdas

How to Add GitHub SSH in Linux and Git Clone Using That SSH

How to Add GitHub SSH in Linux and Git Clone Using That SSH

Before we dive into setting up SSH for GitHub, make sure you've covered the basics. If you're just getting started, check out the complete guide on how to install Git on Ubuntu, where I explain the installation step by step. You can also read a detailed article on Git stash commands, which covers how to temporarily save changes without committing them. And if you want a solid foundation, don't miss the post on 10 basic Git commands every beginner should know, which is perfect for beginners and intermediate developers who want to strengthen their Git workflow. Once you're comfortable with these, setting up GitHub SSH will be much easier and more practical.

If you're a developer using Linux, setting up SSH for GitHub is one of the most important steps for secure and password-free repository access.

Instead of entering your GitHub credentials every time you push or clone a repository, SSH allows secure authentication using cryptographic keys. It is a one-time setup that takes about five minutes.

In this complete guide, you'll learn:

  • How to add GitHub SSH in Linux

  • How to generate SSH keys

  • How to add SSH keys to GitHub

  • How to clone repositories using SSH

  • How to switch an existing repository from HTTPS to SSH

  • How to use multiple GitHub accounts

  • Common mistakes and how to fix them

This guide works for Ubuntu, Debian, Fedora, Arch, and most Linux distributions.

Quick Summary (TL;DR)

If you already know your way around a terminal, here is the whole process:

ssh-keygen -t ed25519 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub        # copy the output into GitHub > Settings > SSH and GPG keys
ssh -T git@github.com            # test the connection
git clone git@github.com:username/repository.git

Read on for the explanation of every step.

Why Use SSH with GitHub?

Using SSH to connect with GitHub provides:

  • Secure authentication: keys are far harder to steal or guess than a password.

  • No repeated password typing: configure once and forget about it.

  • Easy repository access: clone, push and pull without prompts.

  • Ideal for automation and CI/CD: scripts and servers can authenticate without a human.

When you configure SSH correctly, you can clone, push, and pull without entering credentials every time.

SSH vs HTTPS: Which Should You Use?

SSH

HTTPS

Authentication

Key pair

Personal access token (passwords are no longer accepted for Git operations)

Prompts

None once set up

Token needed unless a credential helper is configured

Setup effort

One-time key setup

Token creation and storage

Best for

Daily development, servers, automation

Quick one-off clones, restricted networks

URL format

git@github.com:user/repo.git

https://github.com/user/repo.git

For regular development, SSH is the more convenient and secure option.

What is SSH?

SSH (Secure Shell) is a cryptographic protocol used to securely connect and authenticate between systems.

In this case:

  1. Your Linux machine generates a key pair.

  2. You upload the public key to GitHub.

  3. GitHub verifies you using the private key stored locally.

There are two parts of SSH keys:

  • Private key: stored securely on your system. Never share it, email it or commit it to a repository.

  • Public key: uploaded to GitHub. It is safe to share, because it can only verify, not impersonate.

Prerequisites

  • A Linux machine with a terminal

  • Git installed (check with git --version)

  • OpenSSH client installed (check with ssh -V). If it is missing, install it with sudo apt install openssh-client on Ubuntu/Debian or sudo dnf install openssh-clients on Fedora.

  • A GitHub account

Check for Existing SSH Keys

Before generating a new key, check if you already have one:

ls -al ~/.ssh

Look for files like:

  • id_rsa

  • id_rsa.pub

  • id_ed25519

  • id_ed25519.pub

If they exist, you can reuse them. If not, generate a new one. If the ~/.ssh folder does not exist yet, that is fine, ssh-keygen will create it.

Generate a New SSH Key in Linux

Step 1: Generate SSH Key

Use the modern and recommended ED25519 algorithm:

ssh-keygen -t ed25519 -C "your_email@example.com"

The -C option adds a comment (usually your email) so you can identify the key later. Use the email associated with your GitHub account.

If ED25519 is not supported on a very old system, use:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Why ED25519? It produces shorter keys, is faster, and is considered more modern and secure than RSA at typical key sizes.

Step 2: Choose File Location

After running the command, you'll see:

Enter file in which to save the key (/home/yourusername/.ssh/id_ed25519):

Option 1: Use Default Location (Recommended)

Press Enter to save it in the default location:

/home/yourusername/.ssh/id_ed25519

This is the standard and recommended location for most users.

Option 2: Use a Custom Path and File Name

You can also specify a custom path and file name, for example:

/home/yourusername/.ssh/work_geekdas

Important notes:

  • The folder must already exist (for example ~/.ssh/).

  • Avoid creating nested folders like ~/.ssh/work/geekdas unless the work/ directory exists.

  • If you use a custom filename, you must specify it later when adding to the SSH agent:

ssh-add ~/.ssh/work_geekdas

If a key with the same name already exists, ssh-keygen asks whether to overwrite it. Answer n unless you are sure, because overwriting destroys the old key.

Step 3: Set Passphrase (Optional)

You can:

  • Set a passphrase (more secure)

  • Leave it empty for convenience

A passphrase protects your private key if your laptop is lost or someone copies the file. For personal machines, a passphrase combined with the SSH agent (next section) gives you security without constant typing. For automated servers and CI jobs, an empty passphrase is common, but then protect the key file carefully.

Your SSH key is now generated. You will have two files: the private key (id_ed25519) and the public key (id_ed25519.pub).

Add SSH Key to SSH Agent

The SSH agent keeps your decrypted key in memory so you do not have to type your passphrase on every connection.

Start the SSH agent:

eval "$(ssh-agent -s)"

You should see something like Agent pid 12345.

Add your private key:

ssh-add ~/.ssh/id_ed25519

This loads your SSH key into memory. To confirm, list loaded keys:

ssh-add -l

Add SSH Key to GitHub

Step 1: Copy Public Key

cat ~/.ssh/id_ed25519.pub

Copy the entire output. It starts with ssh-ed25519 and ends with your email comment. Make sure you copy the .pub file, never the private key.

Optional: copy straight to the clipboard if you have a clipboard tool installed:

xclip -selection clipboard < ~/.ssh/id_ed25519.pub

On Wayland, use wl-copy < ~/.ssh/id_ed25519.pub instead.

Step 2: Add to GitHub

  1. Go to GitHub and sign in.

  2. Click your profile picture, then Settings.

  3. Click SSH and GPG keys.

  4. Click New SSH key.

  5. Give it a clear title, such as "Ubuntu Laptop", and leave the key type as Authentication Key.

  6. Paste your public key.

  7. Click Add SSH key and confirm your password if prompted.

Now GitHub recognizes your Linux machine.

Test GitHub SSH Connection

ssh -T git@github.com

The first time you connect, SSH asks whether you trust the host:

The authenticity of host 'github.com (...)' can't be established.
ED25519 key fingerprint is SHA256:...
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Compare the fingerprint with the ones GitHub publishes in its documentation, then type yes.

If successful, you'll see:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

That message is exactly what you want. GitHub does not give you a shell, so the connection closing right after is normal.

If you see:

Permission denied (publickey)

Check your SSH key configuration. See the troubleshooting section below.

For extra detail while debugging, run ssh -vT git@github.com.

Git Clone Using SSH

Now you're ready to clone repositories.

Step 1: Get SSH URL

  1. Go to your repository on GitHub.

  2. Click Code.

  3. Select the SSH tab.

The URL looks like:

git@github.com:username/repository.git

Step 2: Clone

git clone git@github.com:username/repository.git

That's it! No password required.

Practical Example: Clone Your Own Repository

git clone git@github.com:geekdas/my-project.git

Switch an Existing Repository from HTTPS to SSH

If you already cloned a repository with HTTPS, you do not need to clone it again. Check the current remote:

git remote -v

Then switch it to SSH:

git remote set-url origin git@github.com:username/repository.git

Run git remote -v again to confirm. From now on, git push and git pull use SSH.

Configure Git With Your Identity

SSH handles authentication, but Git still needs your name and email for commits:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Use the same email as your GitHub account so your commits link to your profile.

Use Multiple SSH Keys (Multiple GitHub Accounts)

If you manage multiple GitHub accounts, for example personal and work, create a separate key for each one:

ssh-keygen -t ed25519 -C "work_email@example.com" -f ~/.ssh/id_ed25519_work

Then edit your SSH config:

nano ~/.ssh/config

Add:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

IdentitiesOnly yes tells SSH to use only the key you specify, instead of trying every key loaded in your agent. This prevents GitHub from authenticating you as the wrong account.

Then clone using the host alias:

git clone git@github-work:company/project.git

For an existing repository, point its remote at the alias:

git remote set-url origin git@github-work:company/project.git

Remember to set the right commit email per repository with git config user.email "work_email@example.com" (without --global).

Common Mistakes and Fixes

1. Permission Denied (publickey)

Cause:

  • SSH key not added to GitHub

  • SSH agent not running

  • The wrong key is being offered

Fix:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh -vT git@github.com

In the verbose output, check that the right key file is being tried and that you pasted the public key into the correct GitHub account.

2. Cloning Using HTTPS Instead of SSH

Wrong:

https://github.com/user/repo.git

Correct:

git@github.com:user/repo.git

Already cloned with HTTPS? Use git remote set-url as shown earlier.

3. Wrong File Permissions

SSH refuses to use keys that other users can read. You may see "WARNING: UNPROTECTED PRIVATE KEY FILE!"

Fix:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

4. SSH Key Not Loaded After Reboot

The agent forgets your keys when you restart. A simple fix is to let SSH add keys automatically the first time they are used. Add this to ~/.ssh/config:

Host *
    AddKeysToAgent yes

Alternatively, add this to your .bashrc or .zshrc:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Note that the .bashrc approach starts a new agent for each terminal. The AddKeysToAgent option, or a tool such as keychain, is cleaner. Many desktop environments (GNOME, KDE) also run their own agent that handles this for you.

5. Host Key Verification Failed

This appears when the saved host entry in ~/.ssh/known_hosts no longer matches. Verify the situation is legitimate (GitHub does occasionally rotate keys and announces it), then remove the old entry:

ssh-keygen -R github.com

and reconnect, checking the fingerprint against GitHub's published list.

6. Connection Timed Out or Port 22 Blocked

Some corporate and public networks block port 22. GitHub supports SSH over port 443. Add this to ~/.ssh/config:

Host github.com
    Hostname ssh.github.com
    Port 443
    User git

Then test again with ssh -T git@github.com.

7. "Bad passphrase" or Forgotten Passphrase

A forgotten passphrase cannot be recovered. Generate a new key pair, add the new public key to GitHub, and delete the old key from your GitHub settings.

Security Best Practices

  • Never share or upload your private key (the file without .pub).

  • Use a passphrase on personal machines.

  • Use a separate key for each computer, so you can revoke one without affecting the others.

  • Review the keys listed under GitHub Settings > SSH and GPG keys now and then, and remove ones you no longer use.

  • Keep correct file permissions on ~/.ssh.

  • For CI/CD, consider GitHub deploy keys, which are limited to a single repository.

Benefits of Using SSH for GitHub

1. Enhanced Security

SSH uses public-key cryptography, making it more secure than password authentication.

2. No Repeated Authentication

No need to enter credentials every time.

3. Automation Friendly

Perfect for:

  • CI/CD pipelines

  • VPS deployments

  • Automated scripts

4. Works Seamlessly with Git

SSH integrates smoothly with Git workflows.

Conclusion

Setting up GitHub SSH in Linux is a one-time process that saves you time and improves security.

Quick summary:

  1. Generate SSH key

  2. Add it to SSH agent

  3. Upload public key to GitHub

  4. Test connection

  5. Clone using SSH

Once configured, your Git workflow becomes faster, more secure, and more professional.

If you work regularly with GitHub, SSH setup is absolutely essential. Next, practice the daily workflow with our guide on the 10 basic Git commands every beginner should know, and if you run into any problem not covered here, leave a comment and we will help you out.

Was this article helpful?

Comments

Leave a comment

Loading comments...

Frequently Asked Questions

How do I add GitHub SSH key in Linux?
Generate a key using ssh-keygen, add it to SSH agent, copy the public key, and paste it into GitHub -> Settings -> SSH Keys.
How do I clone a Git repository using SSH?
Copy the SSH URL from GitHub and run: git clone git@github.com:username/repo.git
Why is SSH better than HTTPS for GitHub?
SSH avoids password prompts, is more secure, and works better for automation and CI/CD.
Where is SSH key stored in Linux?
SSH keys are stored in: ~/.ssh/ Private key: id_ed25519 Public key: id_ed25519.pub
Can I use multiple SSH keys for GitHub?
Yes, configure multiple keys using the '~/.ssh/config' file and assign different Host aliases.
How do I test if my SSH key works with GitHub?
"ssh -T git@github.com" If successful, GitHub will confirm authentication.
Can I use the same SSH key for multiple GitHub accounts?
No. GitHub requires each key to belong to only one account. Create a separate key per account and use the ~/.ssh/config setup above.
How do I remove an SSH key from GitHub?
Go to Settings > SSH and GPG keys, find the key, and click Delete.

Related Articles