Blog post image for Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux - Git connects to remotes by default via HTTPS, which requires you to enter your login and password every time you run a command like Git pull or git push, using the SSH protocol. You may connect to servers and authenticate to access their services. The three services listed allow Git to connect through SSH rather than HTTPS. Using public-key encryption eliminates the need to type a login and password for each Git command.
Blog

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Published: Updated: 08 Mins read

Introduction

By default, Git talks to remotes over HTTPS, so it asks for your username and password on every git pull or git push. SSH fixes that. GitHub, GitLab, and Bitbucket all let Git authenticate over SSH with public-key encryption instead set it up once and you stop typing credentials for every Git command.

Info

An SSH key is a pair of files: a private key that never leaves your machine, and a public key you upload to each service. Authentication happens by proving you hold the private key no password sent over the wire.

Make sure a Git and SSH client is installed

A Git and SSH client must be installed on your system to connect via the SSH protocol. It should be installed by default if you use Arch Linux-based distributions like Manjaro or Garuda Linux.

Check if Git and SSH are installed
git --version
ssh -V

That command should return the Git version and SSH clientโ€™s version number:

Output
git version 2.34.1
OpenSSH_8.8p1, OpenSSL 1.1.1l 24 Aug 2021

If the system tells you that the ssh or git commands are missing, install them with the command set for your distribution:

Arch-based (Manjaro, Garuda)
sudo pacman -Syu
sudo pacman -Syyu
sudo pacman -S git
sudo pacman -S openssh

Donโ€™t forget to specify global Git settings using the following command after installing Git:

Set global Git settings
git config --global user.name 'USERNAME'
git config --global user.email '[email protected]'

Look for any SSH keys that have already been created

Check for existing SSH keys
ls -lah ~/.ssh

That command lists the contents of the ~/.ssh folder, where the SSH client stores its configuration files. A typical populated directory looks like this:

  • Directory~/.ssh/
    • id_ed25519 Ed25519 private key never share this
    • id_ed25519.pub Ed25519 public key safe to upload
    • id_rsa RSA private key (legacy) never share this
    • id_rsa.pub RSA public key (legacy) safe to upload
    • known_hosts
    • config

Note

Donโ€™t worry if you get an error saying there is no ~/.ssh directory or no files in there it just indicates you havenโ€™t established an SSH key pair yet. Proceed to the next section if this is the case.

Tip

Itโ€™s worth regenerating your SSH key pair about once a year. If your current pair is older than that, generate a new one below; if itโ€™s recent and you want to keep it, skip the next section.

Make a fresh set of SSH keys

Generate a new SSH key pair, replacing [email protected] with your email address. Use Ed25519 itโ€™s what GitHub, GitLab, and Bitbucket recommend today. Reach for RSA only on an older system or server that doesnโ€™t support Ed25519.

Create a new Ed25519 SSH key pair
ssh-keygen -t ed25519 -C '[email protected]'

This creates ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public). Ed25519 keys are small and fast, with security on par with a 4096-bit RSA key.

After running the command, complete the prompts:

  1. Choose where to save the private key. Press Enter to accept the default location (~/.ssh/id_ed25519, or ~/.ssh/id_rsa for an RSA key):
Output
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/your_user_name/.ssh/id_ed25519):
  1. If a private key already exists, youโ€™ll be asked whether to overwrite it. Type y and press Enter:
Output
/home/your_user_name/.ssh/id_ed25519 already exists.
Overwrite (y/n)?
  1. Enter and re-enter a passphrase (think of it as a password for the key):
Output
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Tip

A passphrase encrypts your private key on disk, so a stolen key file is useless without it. Combined with the ssh-agent (next section), you only type it once per session.

The SSH key pair is created in ~/.ssh, and the whole interaction should look like this:

Output
your_user_name@your_host_name:~> ssh-keygen -t ed25519 -C '[email protected]'
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/YOUR_USER_NAME/.ssh/id_ed25519):
/home/YOUR_USER_NAME/.ssh/id_ed25519 already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/YOUR_USER_NAME/.ssh/id_ed25519.
Your public key has been saved in /home/YOUR_USER_NAME/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:Qx3yXenY8FOQmvIsjKVp6oAlITe3k1aMKRdViOFePP6 [email protected]
The key's randomart image is:
+--[ED25519 256]--+
| .o+. |
| .oo=o |
| . o*+.o |
| . ..oB.+ |
| o.S=.* . |
| . +o.E o |
| o.o+.= . |
| =o.++o |
| ..o**+. |
+----[SHA256]-----+
YOUR_USER_NAME@YOUR_HOST_NAME:~>

To the ssh-agent, add your private SSH key

If youโ€™d rather not retype your passphrase every time you use the key, add it to the ssh-agent a background process that keeps your keys in memory while youโ€™re logged in.

  1. Start the ssh-agent in the background:
Start the ssh-agent
eval "$(ssh-agent -s)"

The command returns the ssh-agent process identification:

Output
Agent pid 2887
  1. Add your SSH private key to the ssh-agent pick the tab for your key type:
Add the Ed25519 private key to the ssh-agent
ssh-add ~/.ssh/id_ed25519
  1. Type your passphrase and press Enter:
Output
Enter passphrase for /home/YOUR_USER_NAME/.ssh/id_ed25519:

The ssh-agent confirms the private SSH key has been added:

Output
Identity added: /home/YOUR_USER_NAME/.ssh/id_ed25519 ([email protected])

To your account, add the public SSH key

You can connect through SSH once you have an SSH key and have added it to the ssh-agent. The procedure is the same for all three services: copy your public key to the clipboard, then paste it into the serviceโ€™s SSH-keys settings.

xclip is a command-line tool that gives you access to the clipboard from the terminal. If it isnโ€™t already installed, install it for your distribution:

Arch-based (Manjaro, Garuda)
sudo pacman -Syu
sudo pacman -Syyu
sudo pacman -S xclip

Using the xclip command, copy the contents of your public SSH key to the clipboard pick the tab that matches the key type you created:

Copy the Ed25519 public key to the clipboard
xclip -sel clip < ~/.ssh/id_ed25519.pub

Warning

Only ever copy and paste the public key (the .pub file). The private key (id_ed25519 or id_rsa, with no extension) must never be uploaded or shared with anyone.

Now add that public key to your account. Pick your service below:

Sign in to your GitHub account by going to github.com and entering your username and password. Click your profile photo in the upper-right corner of the page, then Settings:

GitHub Settings

Select SSH and GPG keys from the user settings sidebar, then select New SSH key. Put a descriptive label for the new key in the Title area (for example, your computerโ€™s name) and paste your public key into the Key field. Finally, click Add SSH key:

GitHub Settings

The key is now visible in the list of SSH keys linked to your account:

GitHub Settings

Test connecting via SSH

Before you start using SSH with Git, all three services let you check that the connection works.

Once youโ€™ve added your SSH key to your GitHub account, open the terminal and type:

Test connecting via SSH

If youโ€™re connecting to GitHub over SSH for the first time, the SSH client will ask if you trust the GitHub serverโ€™s public key:

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

Type yes and press Enter. GitHub is added to the list of trustworthy hosts in the SSH client, and you wonโ€™t be asked about its public key again:

Output
Warning: Permanently added 'github.com,140.82.113.4' (RSA) to the list of known hosts.

GitHub only allows this SSH connection for testing, not shell access, so it confirms youโ€™re authenticated and then closes the connection:

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

The entire interaction should look something like this:

Output
The authenticity of host 'github.com (140.82.113.4)' can't be established.
RSA key fingerprint is SHA256:a5d6c20b1790b4c144b9d26c9b201bbee3797aa010f2701c09c1b3a6262d2c02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,140.82.113.4' (RSA) to the list of known hosts.
Hi your_user_name! You've successfully authenticated, but GitHub does not provide shell access.
YOUR_USER_NAME@YOUR_HOST_NAME:~>

Test passed youโ€™re ready to use SSH with GitHub.

Frequently Asked Questions

Yes. A passphrase encrypts the private key on disk, so if the file is ever stolen itโ€™s useless without the passphrase. Pair it with the ssh-agent so you only type it once per login session rather than on every Git command.

Use ed25519. The keys are smaller and faster than RSA with comparable security, and itโ€™s what GitHub, GitLab, and Bitbucket recommend. Generate one with ssh-keygen -t ed25519 -C '[email protected]'. Reach for rsa -b 4096 only when you need to connect to an older server that doesnโ€™t speak Ed25519.

Yes. The same public key can be added to as many accounts and services as you like thereโ€™s no need for a separate key per provider. Just paste ~/.ssh/id_ed25519.pub (or id_rsa.pub) into each serviceโ€™s SSH-keys settings.

Usually one of: the key wasnโ€™t added to the ssh-agent (ssh-add ~/.ssh/id_ed25519), the public key wasnโ€™t added to the service, or the wrong key path is being used. Run ssh -vT [email protected] to see which key the client offers, and confirm ~/.ssh permissions are 700 and the private key is 600.

The ssh-agent isnโ€™t running or isnโ€™t persisting between sessions. Start it with eval "$(ssh-agent -s)" and add the key with ssh-add. To make it stick automatically, add AddKeysToAgent yes (and optionally UseKeychain yes on macOS) under your host in ~/.ssh/config.


References

Related Posts

You might also enjoy

Check out some of our other posts on similar topics

Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Introduction By default, Git talks to remotes over HTTPS, so it asks for your username and password on every git pull or git push. SSH fixes that. GitHub, GitLab, and Bitbucket all let Git aut

Dotfiles: A Git-Based Strategy for Configuration Management

Dotfiles: A Git-Based Strategy for Configuration Management

Introduction Your dotfiles those hidden .-prefixed configuration files scattered across your home directory are the muscle memory of your environment. They hold your shell aliases, your editor s

10+ Secret Git Commands That Will Save Hours Every Week

10+ Secret Git Commands That Will Save Hours Every Week

Introduction As a Software Engineer, DevOps Engineer, or GitHub user, you probably use Git daily. But are you making the most of it? Git is packed with powerful commands that can save

Introduction to Linux CLI

Introduction to Linux CLI

Introduction The Linux operating system family is a group of free and open-source Unix systems. They consist of Red Hat, Arch Linux, Ubuntu, Debian, openSUSE, and Fedora. You must utilize a shell

VIM Cheat Sheet

VIM Cheat Sheet

What Is VIM? VIM (Vi Improved) is a versatile text editor pre-installed on most Linux systems, known for its efficiency in command-line file editing. Its modal nature switching between modes like

How to Install and Configure Node.js on EC2 Instance Amazon Linux 2

How to Install and Configure Node.js on EC2 Instance Amazon Linux 2

Introduction Node.js does not exist in the default Amazon Linux 2 repository. So, we need to add the Node.js repository to the system. In this post, we will learn how to install and configure Node

6 related posts