How to connect to Github using SSH

How to connect to Github using SSH ?

This tutorial guides you on how to connect to Github using SSH. While setting up development environment or checking out code from git for the first time, you may need to setup Github with SSH. So that it simplifies your authentication and connection process each time you visit Github repository.

How to connect to Github using SSH ?

 

How to connect to Github using SSH

You can use SSH protocol for authentication and connecting to remote servers. And using SSH keys you can connect to Github servers without using your username and access token each time.

Let’s see step by step procedure on how to setup Github with SSH or how to connect to Github using SSH.

SSH Keys – Existing/ Generate new one

First, you need to check whether you can use existing SSH key before you generate a new key.

To check existing SSH keys, try the following.

  • Git bash
  • Run the following command ls -al ~/.ssh which will list public keys if you already have some.
ubuntu@my-ubuntu-virtual-machine:~$ ls -al ~/.ssh
total 20
drwx------  2 ubuntu ubuntu 4096 Jun  7 12:43 .
drwxr-xr-x 19 ubuntu ubuntu 4096 Jun 11 15:49 ..
-rw-------  1 ubuntu ubuntu  464 Jun  7 12:35 id_ed25519
-rw-r--r--  1 ubuntu ubuntu  109 Jun  7 12:35 id_ed25519.pub
-rw-r--r--  1 ubuntu ubuntu 1110 Jun  7 13:45 known_hosts

To generate a new SSH key:

  • And if you don’t have any keys listed or if you don’t wish to use any of them, then you can generate a new SSH key.
  • All you need to do is just open Git bash terminal and run the following command.
$ ssh-keygen -t ed25519 -C "[email protected]"

The above command will create a new SSH key using the email provided by you as label and you would see the following message.

> Generating public/private ed25519 key pair.

You will be prompted for typing secure passphrase. Hence type the passphrase and type again the same passphrase as prompted.

> Enter passphrase for key '/home/ubuntu/.ssh/id_ed25519': <Type passphrase>
> Enter same passphrase again: <Type passphrase again>

Once the SSH keys are generated successfully then you need to add SSH key to your Github account.

Add SSH Key to Github account

You need to perform the below steps to add SSH key to your Github account (i.e., the email address that you have used while generating SSH keys)

  • First, you need to run the following commands and copy your public key.
$ cd /home/ubuntu/.ssh/

~.ssh/ $ ls
id_ed25519  id_ed25519.pub  known_hosts

$ cat  id_ed25519.pub
ssh-ed25519 DDDGGHHHAGAGAHADASHGAH2dghdasjhgjaghakdhgkashdgdkjahkghkahgkhag [email protected]
  • After that, login to your GitHub account.
  • Then click your profile avatar and choose Settings.
  • And select SSH and GPG keys.
  • Click New SSH key.
  • Enter a title in the field.
  • Paste your public key into the Key field.
  • Click Add SSH key.

That’s all. You had successfully added the new SSH key to your Github account.

Add your new SSH key to ssh-agent

First, make sure that ssh-agent is running on your machine. To check that you can run the following command. This command will start the ssh-agent in the background.

$ eval "$(ssh-agent -s)"
> Agent pid 59568

Then you need to add your SSH private key ( id_ed25519) to the ssh-agent as shown below

$ ssh-add ~/.ssh/id_ed25519

That’s all. You are Done!

Testing Github with SSH

Finally you can try the small test to check if Github SSH setup works.

$ ssh -T [email protected]
Enter passphrase for key 'home/ubuntu/.ssh/id_ed25519':
Hi <username> You've successfully aunthenticated, but GitHub does not provide shell access.

Note, if you have Github Dektop installed, then you can clone git repositories without using or dealing with SSH Keys. This is just for your information.

Hope this article is helpful to you 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments