My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Setting up SSH Keys using Github and Gitlab

Setting up SSH Keys using Github and Gitlab

Sujay Kundu's photo
Sujay Kundu
·Oct 15, 2020·

4 min read

Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.

Setting up SSH keys

For Github

Open Terminal

Generating a SSH Key

Paste the text below, substituting in your GitHub email address.

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This will create a public ssh key

When you're prompted to "Enter a file in which to save the key,".

Enter a file in which to save the key (/home/you/.ssh/id_rsa):

Enter the filename as yourusername-github (example: sujaykundu777-github)

At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".

Just press Enter twice"

Adding your SSH key to the ssh-agent

Start the ssh-agent in the background.

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

Add your SSH private key to the ssh-agent. If you created your key with a different name, in our case yourusername-github

$ ssh-add ~/.ssh/yourusername-github

The files - the private key and the public key should be inside the /home/yourusernme/.ssh folder.

Adding the ssh key to your Github Account

Copy the ssh key to your clipboard

$ sudo apt-get install xclip

Downloads and installs xclip. If you don't have apt-get, you might need to use another installer (like yum)

$ xclip -sel clip < ~/.ssh/yourusername-github.pub

Copies the contents of the yourusername-github.pub file to your clipboard

Now paste this ssh key in Github settings page under settings > SSH and CPG Keys > Add SSH Key > Save SSH Key.Use a suitable title for the key.If prompted, confirm your GitHub password.

After saving the ssh key, just set the remote url using the ssh url provided for the repo:

$ git remote set-url origin git@yourname-github:yourname/yourrepo.git

That's it, Now you don't have to enter password everytime for a push

For Gitlab

$ KEY=yourname-gitlab && ssh-keygen -t rsa -b 4096 -f ~/.ssh/$KEY -C "$KEY"

This will create a public ssh key,Now open config file:

$ nano ~/.ssh/config

and save the host details :

 Host yourname-gitlab
 HostName gitlab.com
 IdentityFile ~/.ssh/yourname-gitlab

Now copy the ssh signed rsa public key by:

$ cat ~/.ssh/yourname-gitlab.pub

Now paste this ssh key in Gitlab settings page

After saving the ssh key, just set the remote url using the ssh url provided for the repo:

$ git remote set-url origin git@yourname-gitlab:yourname/yourrepo.git

That's it, Now you don't have to enter password everytime for a push

ggpush and ggpull works too !!