How to Generate An SSH Key in Linux to Access A GitHub Repo

In this tutorial, first we will see what SSH is and what problems it solves; then we will get into generating an SSH key in GNU/Linux (Ubuntu) in order to access a GitHub repo.

What Is SSH?

SSH stands for Secure Shell which is basically a protocol to communicate with other computers (Other communication protocols are HTTP, HTTPS, and FTP to just name a few.) What is so cool about SSH is its security due to the fact that all the traffic is encrypted. There are GUI programs which make it possible to use SSH through an interface but it is mostly used in the terminal and in this tutorial we will use the latter.

How to Generate SSH Keys?

Most Linux distros, like Ubuntu, come with SSH tools out of the box and there is no need to set it up. To make sure the required tools are available, we can easily run ssh command in the terminal which results in something as follows:

$ ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
           [-S ctl_path] [-W host:por

To generate both public and private keys, we just need to type ssh-keygen in the terminal. By default, this command will create a private key called id_rsa and a public key called in ~/.ssh path (No need to say that ~ or Tilde is the home directory of Unix-based OSes.) So the quick and dirty way to generate an SSH key is to just type the following command:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):

By typing the above command and hitting enter, we are asked to define the location for private key to be saved and it is normal to keep the defaults and press enter. Then we are asked to choose a password to make that key more secure:

Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

If you do not want to have a passphrase on it, you can simply press enter twice to pass this step; then the process is as follows:

Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/
The key fingerprint is:
SHA256:PeF2EA69aNOWWylHbGcGWUOJAGQaCK/jcnlZK0EQjc4 ubuntu@ip-172-31-47-51
The key s randomart image is:
+---[RSA 2048]----+
|  += ...=ooo.*+. |
|  .oo  + o..* =. |
| o  o .  o+= =   |
|  Eo    +o*o+    |
|  o . ..So==.    |
| . o + . ..o     |
|. + + .          |
| o . .           |
|                 |

To activate the SSH agent, we need to run the following command:

$ eval "$(ssh-agent -s)"

In addition, we have to add our SSH private key to SSH agent:

$ ssh-add ~/.ssh/id_rsa
Identity added: /home/ubuntu/.ssh/id_rsa (/home/ubuntu/.ssh/id_rsa)

Now, we need to get the public key and add it to our GitHub (or GitLab) account. To do this, we can run the following command:

$ cat ~/.ssh/ 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMBNBmEpLj5EwS1MoqAih7XDqh7oX1hQingazq0lFBS8CF5VxKuj73eqfohSJHOFsrKWn0RkfmZ3xDAR91/9YGdBcVX3TIPgWosktINfk09gTZblbLi5170KKJEEPQgAy8MbpBbxVzBi8ITKVu4ZZIcKph13/pc+ot9VUWyb0k49j28P+xkc3ihQdz4uWrf5Qk7jJU9xXr4bIfVmJrvq0dx3avtrFVdn7FtyC7dYuiO1C1M0j/j8N8QbnvOvcxRDTWgf6RvzJEwkslU7YvOjWWzejmU1wD4KGvk0uexpcWSMjcirjr3z/JJQ7aMmRO8I7Ad8lUE14oPUuBHt5cI6xT ubuntu@ip-172-31-47-51

We have to copy the public key then go to SSH keys page in GitHub website and click "New SSH Key" and after choosing whatever title we want, paste the key and hit "Add SSH Key". 

That is all there is to it. From now on, we can simply clone any repo without entering the username and password of our GitHub account:

$ git clone

In the above command, username and repo.git should be replaced with your real names.

SSH Key Does Not Work for Connecting to GitHub

To tackle the above issue, most of the time the problem is that you are using wrong remote URL; in other words, you may use https instead of for the remote. First, double-check that your Git SSH connection is working:

$ ssh -T

If it works, go to the project folder and check your remote:

$ git remote -v

It will display something like this:


If it indicates https at the very beginning, then you need to change this URL with the following command:

$ git remote set-url origin

From now on, in 99% of situations, you have to be able to connect to your GitHub repo via SSH without any problem.

by Behzad Moradi on 2019-10-09