Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux
- Mohammad Abu Mattar
- Linux
- 18 Dec, 2021
- 06 Mins read
Introduction
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.
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.
That command should return the Git version adn SSH client’s version number:
If the system tells you that the ssh or git commands are missing, use the following command to install them:
Arch-based Linux systems:
Debian-based Linux systems:
Red Hat-based Linux systems:
Suse-based Linux systems:
Fedora-based Linux systems:
Don’t forget to specify global Git settings using the following command after installing git:
Look for any SSH keys that have already been created
The contents of the ~/.ssh folder
, where the SSH client stores its configuration files, should be listed by that command:
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 subject if this is the case.
It is advised that you produce a fresh SSH key pair at least once a year for security reasons. It is advised that you go on to the following topic if you already have an SSH key pair that was established more than a year ago. You can skip the following subject if you already have an SSH key pair and wish to reuse it.
Make a fresh set of SSH keys
Run the following command (change your YOUR_EMAIL@EXAMPLE.COM
with your email address) to establish a new SSH key pair:
It will ask you where you want to save the private key (id rsa), and you may accept the default location by pressing Enter.
Whether you already have a private key, it will ask if you want to override it:
If this happens, hit Enter and type y. Then, enter and re-enter the following passcode (think of it as a password):
The SSH key pair is created in ~/.ssh
, and the whole interaction should look like this:
To the ssh-agent, add your private SSH key
If you don’t want to input your password every time you use your SSH keys, add it to the ssh-agent, which is a background application that saves your keys in memory while you’re signed in to the system.
Run the following commands to start the ssh-agent in the background:
The ssh-agent process identification is returned by this command:
Then, in the ssh-agent, add your SSH private key:
Press Enter once you’ve typed your password:
The ssh-agent has been updated with the private SSH key, according to this command:
To your account, add the public SSH key
You can connect through SSH after you have an SSH key and have added it to the ssh-agent. Let’s take a look at how to implement it with each of the three servers: GitHub, GitLab, and Bitbucket.
The procedure is the same in all three circumstances, xclip is a command-line tool that gives you access to the clipboard in the graphical user interface from the terminal. If it isn’t already installed, perform the following commands to do so:
Arch-based Linux systems:
Debian-based Linux systems:
Red Hat-based Linux systems:
Suse-based Linux systems:
Fedora-based Linux systems:
Using the xclip command, copy the contents of your public SSH key (/.ssh/id rsa.pub) file to the clipboard:
GitHub
Sign in to your GitHub account using a browser 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:
Select SSH and GPG keys from the user settings sidebar. Then select New SSH key from the drop-down menu. 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. Last but not least, click Add SSH key:
The key is now visible in the list of SSH keys linked to your account:
GitLab
Sign in to your GitLab account using a browser by going to gitlab.com and entering your username and password. Click your profile photo in the upper-right corner of the page, then Settings:
Click SSH Keys in the User Settings sidebar. In the Key area, paste your public key. Fill in the Title area for the new key with a descriptive term (for example, the name of your computer). Finally, click the Add key:
The key is now visible in the list of SSH keys linked to your account:
Bitbucket
Log in to your Bitbucket account using a browser by going to bitbucket.org and entering your username and password. Click your profile photo in the lower-left corner of the website, then Bitbucket settings:
SSH keys may be found in the Settings sidebar’s Security section. After that, select Add key from the drop-down menu. Fill up the Description box with a descriptive label for the new key (such as your computer’s name), and then paste your public key into the Key field. Last but not least, choose to Add key:
The key has now been added to your account’s list of SSH keys:
Test connecting via SSH
Before utilizing SSH with Git, GitHub, GitLab, and Bitbucket allow you to verify whether the connection has been set up successfully.
GitHub Test Connecting via SSH
Open the terminal once you’ve added your SSH key to your GitHub account and type:
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:
Press Enter after typing yes. GitHub has been added to the list of trustworthy hosts in the SSH client:
You won’t be asked about GitHub’s public key again once you’ve added it to the list of known hosts.
The server notifies you that you have successfully authenticated and ends the connection: Because this remote access through SSH is offered by GitHub only for testing purposes and not for practical usage, the server informs you that you have successfully authenticated and terminates the connection:
If you passed the test, you may now utilize SSH with GitHub.
The entire interaction should look something like this:
GitLab Test Connecting via SSH
The test is pretty similar if you’ve added your SSH key to your GitLab account:
If you passed the test, you may now utilize SSH with GitLab.
Bitbucket Test Connecting via SSH
The test is pretty similar if you’ve added your SSH key to your Bitbucket account:
If you passed the test, you may now utilize SSH with Bitbucket.