Clone GitHub repository over SSH in Ubuntu 22.04

GitHub recommends to clone its repositories over SSH rather than HTTP for performance and security reasons. This toturial will show you how to clone a GitHub repository into your Ubuntu box step by step.

Installation to clone GitHub over SSH protocol

Cloning a GitHub over SSH into a directory in Ubuntu 22.04 requires four steps:

  1. Create an SSH keypair (incl. private key and public key)
  2. Copy the value of the public SSH key to your GitHub account
  3. Obtain the GitHub SSH URL for the repository to be cloned
  4. Using git command to clone from GitHub with the SSH URL

Create an SSH keypair

Open your command line terminal (press Ctrl+Alt+T) and execute the following command:

ssh-keygen -o -t rsa -C "your_name@your_company.com"

Then, you will be asked for:

  • Where to store the key. The default value will be ~/.ssh (.ssh inside your home directory. You must enter to not change it, otherwise your GitHub SSH clone attempt will fail, or you have to specify the ssh key location in your clone command.
  • Enter passphrase, which is a password to protect your key in case you lost the key. You just leave it empty and hit Enter for no passphrase so that you don’t need to input the passphrase when cloning your GitHub repository.

Now, you could get output as below to indicate where your keys are stored:

Your identification has been saved in /your/home/.ssh/id_rsa
Your public key has been saved in /your/home/.ssh/id_rsa.pub

Where /your/home/ is your home directory which is equivalent to ~/.

Copy the public SSH key content to your Github account

To clone a GitHub repository over SSH, you must store a copy of your public key on GitHub.

When you perform a clone of a GitHub repository over SSH, the public key held by the remote server is compared against the private key stored locally in your .ssh folder. If there’s a match, the GitHub clone over SSH proceeds. If not, the connection fails. So, for any of this to work, GitHub needs a copy of your public SSH key.

Open your public key, likely named id_rsa.pub, with a plain text editor and copy the contents. Or, you could simply issue a cat command and copy the output from the terminal window:

cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC0YgUumiYFYML0AxpVEVUpnTSxEphuRNICnxL3sIv6HB7drUPlTZ19R1s1z7eBPiIqDXrH8eCE3vBxf6EWvQVypp4xYimgOKF/F0PACqT2KK9dIImioBn3aicrKJ4TnksOWvWNgiZqlpAbrVtfrtdWGEnHOJrp86BJsf7

Login GitHub with your account then open your account setting page to find SSH and GPG keys menu entry then hit New SSH Key button to add the public key by pasting the value of the public key into the appropriate field, and give your key a creative name.

Add SSH key to GitHub

Obtain the GitHub SSH URL

Each GitHub repo has a green Code button you can click to get either an HTTP, CLI or SSH URL that allows the repo to be cloned.

Copy your repository’s SSH URL and you’re ready to perform the GitHub SSH clone operation. The SSH URL for my Odoo’s fork repository is as follows:

git@github.com:Viindoo/odoo.git

Clone GitHub over SSH

You may need to install Git by issuing the following commands if it is not installed on your Ubuntu box yet

sudo apt update
sudo apt install git

Then issue the Git clone command and specify the SSH URL copied from GitHub. Git will ensure that the GitHub clone operation uses an SSH connection.

git clone git@github.com:Viindoo/odoo.git /path/to/destination

If you need to clone and check out a branch that is not the default one at the same time, the command could be:

git clone -b branch_name git@github.com:Viindoo/odoo.git /path/to/destination

You may encounter a warning about Git’s inability to validate the SSH key against a third party authentication service pauses the clone operation. This is expected. Simply type ‘yes‘ and the GitHub SSH clone operation proceeds.

You may also be asked for passphrase if your key requires passphrase when you created it in the first step.

That’s all!

Conclusions

No matter your repository is public or private, performing a clone over SSH is much faster and more securely. Once the clone operation is complete, all subsequent interactions with GitHub take place over a secure SSH connection.

This tutorial is for Ubuntu 22.04. However, it would work for earlier and later versions.

Leave a Reply