DEV Community

Cover image for Want to avoid git's annoying password prompts for every push? Configure SSH keys for Github
Shubham Kumar
Shubham Kumar

Posted on

Want to avoid git's annoying password prompts for every push? Configure SSH keys for Github

Are you using HTTPS protocol to communicate between your local repos and Github? If yes, then just think–all of that time you spent entering your email address and password into the terminal every time you push a commit could have been spent coding. Guess what? you could have saved all that time by simply enabling SSH for your GitHub account. In this post, we will try to create an SSH key and add it to your Github account for "better" way of GitHub authentication.

What the heck is SSH anyway?

SSH or Secure Socket Shell, is a network protocol that gives users a secure way to access a computer over an unsecured network. It allows the remotely login to a server using the connection established between your system's shell and the server's shell.

Let's Setup SSH Keys for your GitHub

Navigate to your $HOME and change into .ssh directory.

Inside the .ssh folder, you can generate a key using the following command:

ssh-keygen -t rsa -b 4096 -C your@email.com
Enter fullscreen mode Exit fullscreen mode

Replacing your@email.com with your Github email. You can either keep the default filename id_rsa or you can provide a custom file name.

Create ssh key

Dissecting the command

The ssh-keygen command creates RSA and DSA keys for public key authentication. It can also used to edit properties of existing keys, and to convert file formats. The command takes the following options

ssh-keygen [-b bits] -t alog-type [-N [passphrase]] [-C comment] [-f output_keyfile]

When no options are specified,ssh-keygengenerates a 2048-bit RSA key pair and queries you for a key name and a passphrase to protect the private key. Public keys are created using the same base name as the private key, with an added .pub extension. The key location is displayed when key generation is complete.

Keys Generated

Important Note: You should never reveal your private key, and only use your public key for things like GitHub authentication.

Add your SSH key to ssh-agent

ssh-agent is a program that starts when you log in to your system. It has all your private keys. To add the key to the ssh-agent first we need to check if it is up and running.

// If you are on Mac/Linux:
eval "$(ssh-agent -s)" # for Mac and Linux

// If you are using Windows use:
eval `ssh-agent -s`
ssh-agent -s 
Enter fullscreen mode Exit fullscreen mode

If it is running you will see and output like this:
SSH Agent
This tells us that the agent and up and running on the specified port number.

You can then move ahead to add your key to the agent by using the following command:

ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Note: If you used a custom file name for your keys, use that instead of id_rsa

Now we have to copy our public key:

// For linux/mac terminal use this:
cat ~/.ssh/id_rsa.pub # Linux

//For Windows use this:
clip < ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Adding the public keys to your Github account:

Go to your GitHub Account -> Settings -> SSH & GPG Keys.
Click on add new key.
Provide a title and paste in your public (id_rsa.pub) key:

Add key

Testing Github Authentication

Try this in the terminal

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If everything goes well, it will return:

Hi GithubName! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Note: If you provided a passphrase during generating the key, you might have to use that as the password.

That's it you have successfully generated and configured an SSH key for your Github authentication. Happy Coding !!!

Discussion (0)