For GitHub users, there are several ways to connect and manage the GitHub repository from a local computer. I am currently learning how to use GitHub and connect my repository from a laptop. This way, I can use my laptop and then I can push any updates I made to the repository.
There are several steps to connect to GitHub with SSH. The very first thing is to make sure that we have a SSH Key. Let’s check the existing SSH Keys before we create a new one.
ls -al ~/.ssh
The command will list all the files inside the .ssh directory. In my case, I don’t have any ssh keys yet. The command returns the following:
ubuntu@ubuntu:/$ ls -al ~/.ssh
drwx------ 2 ubuntu ubuntu 40 Jan 3 23:17 .
drwxr-xr-x 21 ubuntu ubuntu 580 Jan 4 00:45 ..
This means, I don’t have any ssh keys yet. So, let’s create a new one:
Generate a new SSH Key and Adding to SSH Agent
So now we are going to create a new SSH key. Open Terminal and then paste the following command. Change the email address with your GitHub email address.
ssh-keygen -t rsa -b 4096 -C “email@example.com
You will need to supply a new passphrase but you can also skip it by pressing Enter twice if you don’t want to put any passphrase.
ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
| E |
| . o. . |
| o . .. o |
| o +……. |
|o + . oS+o.. |
|.o * ++=+.++.. . |
|. + . *.o. .o o |
| o o +.. .o |
| .o.o.. oo |
At this point, we have successfully created a new SSH Key. Now we are going to add this new key to ssh-agent
Start the ssh-agent in the background with this command
eval “$(ssh-agent -s)”
Agent pid 20427
Now let’s add the SSH private key to the ssh-agent with this command
Add the SSH Key to GitHub Account
On the previous step we’ve created a new Private SSH Key and add it to our ssh agent. Now, we are going to copy the key to our GitHub Account. To do this, we need to install xclip. This program will help us to easily copy the key to clipborad.
sudo apt install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub
Now, open GitHub Account and go to Settings. Click SSH and GPG Keys and then click New SSH Key. Give a name for this new key for example: Ubuntu-laptop or anything you like. Then, paste your key.
Finally, click Add SSH Key to complete the step. At this point, we have successfully add a new SSH Key to our GitHub account.
Switching Remote URL from HTTPS to SSH
The following step is used to change the remote URL from HTTPS to SSH. If you want to connect to your GitHub repository via Terminal using SSH, do the following:
Make sure to change the ~/Documents/zenity-script with your local GitHub repository.
git remote -v
origin https://github.com/dhaniekonugroho/zenity-script (fetch)
origin https://github.com/dhaniekonugroho/zenity-script (push)
As you can see, currently I am connecting to my GitHub using HTTPS mode. Now let’s switch to SSH mode. The syntax is:
git remote set-url origin email@example.com:USERNAME/REPOSITORY.git
In my case, it would be
git remote set-url origin firstname.lastname@example.org:dhaniekonugroho/zenity-script.git
Now let’s check again
git remote -v
origin email@example.com:dhaniekonugroho/zenity-script.git (fetch)
origin firstname.lastname@example.org:dhaniekonugroho/zenity-script.git (push)
As you can see the connection is now start with git@github, not HTTPS. So now, I am connecting to my GitHub using SSH.