Want to avoid git's annoying password prompts for every push? Configure SSH keys for Github
Tags: beginners, github, codenewbie, tutorial
Published: 2021-10-02 21:52:47

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:
```javascript
ssh-keygen -t rsa -b 4096 -C your@email.com
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ditb5a9v96jctn7ghla6.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbdk3q7gy2d2zk4m6brt.png)

>***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.

```javascript
// 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 
```
If it is running you will see and output like this:
![SSH Agent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1h1fx15blucyiev750jf.png)
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:

```javascript
ssh-add ~/.ssh/id_rsa
```
>***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:
```javascript
// For linux/mac terminal use this:
cat ~/.ssh/id_rsa.pub # Linux

//For Windows use this:
clip < ~/.ssh/id_rsa.pub
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw88811bkm850anpp5co.png)

### Testing Github Authentication
Try this in the terminal
```javascript
ssh -T git@github.com
```
If everything goes well, it will return:
 
```javascript
Hi GithubName! You've successfully authenticated, but GitHub does not provide shell access.
```

>***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 !!!