How to Generate Git SSH Keys
SSH Keys are a secure and easy way of connecting to the remote repository. They help the remote repository identify the user who is making a push and are convenient for the users as they don't have to manually enter their username and password every time. Let's learn more about SSH keys and how to use them.
SSH Keys
- SSH stands for Secure Shell and it is a network protocol that enables us to communicate with the remote servers.
- SSH keys are used for authentication purposes and they are a lot more secure than usernames and passwords that we have to use every time we need to push something on the remote repository.
- SSH uses two keys - a Private Key and a Public Key. The public key is like a Lock and it is stored on the remote server and the private key is the Key to that lock and stored on our local machine.
- Both of these keys are created by a one-way cryptographic function which means that we can get the public key from the private key but not the other way round.
How does User Authentication work?
When a user wants to communicate with the server, the server first creates a random message and encrypts(or locks) it using the Public key and sends it to the user.
The user can then unlock or decrypt this message by using the private key and sends the message back to the server.
If this message matches the original message generated by the server then a connection is established.
Generating SSH Keys
We know what SSH is and how SSH keys make our lives easier. Let's learn how to generate and use these keys.
Creating New SSH Keys
SSH keys are by default saved in a .ssh file in the Home Directory. Run the following command to check whether we already have any keys stored in this file.
$ ls -al ~/.ssh
If you get an output that says that the directory was not found then you first need to create a .ssh directory. Use the following command to do that.
$ mkdir $HOME/.ssh
To create a new SSH key open the Git Bash and run the following command.
$ ssh-keygen -t rsa -b 4096 -C "email"
Next, you will be asked to enter the file path where you want to save the key. If you want the save it in the default location(.ssh file in your Home directory).
You will also be asked to set a passphrase. It is like an additional layer of security because every time you have to use the key you will be asked to enter this passphrase.
You should get the following output after completing the above steps.
Adding SSH Keys to GitHub
Next, we need to add this SSH key to the ssh-agent. Ssh-agent is a program that stores and manages the SSH keys. Run the following code to check whether the ssh-agent is running or not.
$ eval "$(ssh-agent -s)"
Add the SSH key to the ssh-agent using the following command.
$ ssh-add ~/.ssh/id_rsa
Now, we need to add the SSH key to the site where we are hosting our remote repository. In our case, it would be GitHub. We need to inform GitHub about this SSH key. We will need the public key to do this. To get the public key run the following command.
$ cat ~/.ssh/id_rsa.pub
Copy the output and go to your GitHub account settings. Go to SSH and GPG Keys and click on the New SSH Key button. Give a suitable title and paste the public key in the space provided.
Finally, we can test whether our SSH key was successfully added by running the following command.
$ ssh -T git@github.com
Summary
SSH Keys are a safe and secure way of accessing the remote repository. It provides a robust user authentication process which is a lot more secure than usernames and passwords. They also the developers as they no longer need to enter their username and password every time they want to connect to the remote repository. In this tutorial, we learned to how to create SSH keys and also learned how to add them to our GitHub account.