How to Add Github Ssh Key to Linux

How to Add Github Ssh Key to Linux

To how to add github ssh key to linux, generate an SSH key pair on your Linux machine, add the private key to your SSH agent, copy the public key to your GitHub account, and test the connection. Once it is set up, you can use GitHub over SSH without typing your username and password every time.

What you need before you start

Before you begin, make sure you have access to a Linux terminal and a GitHub account. Most modern Linux distributions already include OpenSSH tools such as ssh-keygen and ssh. If they are missing, install the openssh-client package through your distribution’s package manager.

You also need to know one important security rule: the public key is safe to upload to GitHub, but the private key must stay on your Linux machine. Never paste the private key into GitHub or share it with anyone.

Generate a GitHub SSH key on Linux

Generate a GitHub SSH key on Linux

The most common and recommended approach is to create an ed25519 key pair. It is modern, secure, and supported by GitHub on current systems. If you need compatibility with an older environment, RSA can still work, but ed25519 is usually the better starting point.

  1. Open a terminal and run:

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

  2. When prompted for a file location, press Enter to accept the default path. This usually saves the key in ~/.ssh/id_ed25519.

  3. Choose a strong passphrase when prompted. A passphrase is optional, but it adds another layer of protection if someone ever gains access to your private key file.

If your Linux system does not support ed25519 for some reason, use RSA instead:

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

After this step, you should have two files: the private key and the public key. The public key usually ends in .pub.

Add the key to the SSH agent

Add the key to the SSH agent

The SSH agent stores your unlocked private key in memory so you do not need to type the passphrase every time you connect.

  1. Start the SSH agent:

    eval "$(ssh-agent -s)"

  2. Add your private key to the agent:

    ssh-add ~/.ssh/id_ed25519

If you created an RSA key, use:

ssh-add ~/.ssh/id_rsa

If you see an error saying the agent is not running, start it again with the first command and retry ssh-add.

Copy the public key to GitHub

Now copy the public key and add it to your GitHub account settings.

  1. Display the public key in your terminal:

    cat ~/.ssh/id_ed25519.pub

  2. Copy the full output, including the email comment at the end.

  3. Sign in to GitHub, open Settings, then go to SSH and GPG keys.

  4. Select New SSH key.

  5. Give the key a descriptive title, such as your laptop name or Linux distribution.

  6. Paste the public key into the key field and save it.

If your desktop environment supports a clipboard utility, you can copy the key more quickly. Common examples include xclip or xsel, but the exact command depends on your system. If those tools are not installed, using cat and manually copying the output is fine.

Test the GitHub SSH connection

Testing confirms that GitHub recognizes your key and that your Linux machine is using it correctly.

ssh -T git@github.com

The first time you connect, you may be asked to verify GitHub’s host fingerprint. If prompted, confirm that you are connecting to GitHub before continuing.

When it works, GitHub usually responds with a message similar to a successful authentication notice. You may still see a message saying that GitHub does not provide shell access, and that is normal. It means the SSH login succeeded.

Check that your Git remote uses SSH

Check that your Git remote uses SSH

Adding the key is only part of the setup. Your repository also needs to use the SSH remote URL, not the HTTPS version.

Check the remote with:

git remote -v

If you see an HTTPS URL such as https://github.com/..., switch it to SSH with a command like this:

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

Replace username/repository.git with your actual GitHub path. After that, Git will use SSH when you push or pull.

Fix common SSH key problems

Most SSH setup problems are caused by one of a few common issues. The fixes below cover the cases people run into most often.

ProblemLikely causeWhat to do
Permission denied (publickey)GitHub does not have the right public key, or your machine is using the wrong private key.Confirm that the public key was added to GitHub, then run ssh-add -l to check whether the correct key is loaded.
Could not open a connection to your authentication agentThe SSH agent is not running in your current shell session.Run eval "$(ssh-agent -s)" and then add the key again with ssh-add.
GitHub asks for a password over SSHThe remote URL is still HTTPS, or SSH is not being used for that repository.Check git remote -v and change the URL to the SSH format.
Wrong key is used on a machine with multiple accountsMore than one SSH key exists, and SSH is choosing the wrong one.Create or update ~/.ssh/config so GitHub uses the correct key explicitly.

Using multiple GitHub accounts on one Linux machine

Using multiple GitHub accounts on one Linux machine

If you manage more than one GitHub account, you may need separate keys and a custom SSH configuration. This is common on workstations used for both personal and work repositories.

A typical approach is to create separate key files and assign each one to a host alias in ~/.ssh/config. For example, you might use one key for personal GitHub access and another for a work account. The important part is making sure each GitHub identity maps to the correct private key.

Without a custom configuration, SSH may choose the wrong key, which often leads to authentication failures even when both keys are valid.

ed25519 vs RSA

ed25519 is usually the best choice for new GitHub SSH keys on Linux because it is modern and compact. RSA is still useful when you need broader compatibility with older systems or older tooling.

If you are unsure, start with ed25519. If your environment does not support it, RSA with a strong key length such as 4096 bits is the common fallback.

Permission denied errors

If GitHub returns Permission denied (publickey), work through these checks in order:

  • Confirm the public key was added to the correct GitHub account.
  • Check that the private key is loaded with ssh-add -l.
  • Make sure the repository remote uses the SSH URL.
  • Verify that your ~/.ssh/config file is not pointing to the wrong key.

Quick verification checklist

Use this checklist if you want to confirm the setup is complete:

  • The SSH key pair exists on your Linux machine.
  • The private key has been added to the SSH agent.
  • The public key has been pasted into GitHub under SSH and GPG keys.
  • ssh -T git@github.com returns a successful authentication message.
  • Your Git remote URL uses the SSH format instead of HTTPS.

Frequently Asked Questions About how to add github ssh key to linux

How do I add an SSH key to GitHub on Linux?

Generate a key pair with ssh-keygen, add the private key to ssh-agent, copy the public key, and paste it into GitHub under Settings then SSH and GPG keys.

How do I generate an SSH key in Linux for GitHub?

Use ssh-keygen -t ed25519 -C "your_email@example.com". If ed25519 is not available, use RSA with -t rsa -b 4096.

How do I test whether my GitHub SSH key works?

Run ssh -T git@github.com. If the setup is correct, GitHub should recognize your key and confirm authentication.

Why does GitHub say permission denied with my SSH key?

That usually means the wrong key is being used, the public key was not added to the right account, or the repository is still configured with the wrong remote URL.

Should I use ed25519 or RSA for GitHub SSH keys?

Use ed25519 for most modern Linux systems. Choose RSA only when you need compatibility with older software or older systems.

Can I use one SSH key for multiple GitHub accounts?

Technically yes, but separate keys are usually easier to manage. If you use multiple accounts, configure ~/.ssh/config so each account uses the correct key.

Conclusion

The simplest way to how to add github ssh key to linux is to generate a key pair, load the private key into the SSH agent, add the public key to GitHub, and test the connection with ssh -T git@github.com. If Git still prompts for a password or shows Permission denied (publickey), the issue is usually a missing key, the wrong remote URL, or a mismatch between multiple SSH keys on the same machine.

Once everything is configured correctly, SSH makes GitHub access on Linux faster and more secure, especially if you work with Git regularly.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *