SSH keys in git

We can communicate with Git repository using 2 main protocols.
  • HTTP
  • SSH
The disadvantage of using HTTP protocol is that every time you access the remote repository, you will have to enter user id and password for authentication purpose. To avoid typing the password, you can use SSH protocol. You need to follow below steps to set up SSH keys for authentication purpose.
  • Create pair of public and private keys using ssh-keygen command
  • Add private key to the SSH agent running on local machine
  • Add public key on the Git server like Stash or GitHub.

Generating SSH keys

Below command is used to create SSH keys.
 
ssh-keygen -t rsa -b 4096 -C “[email protected]
Above command creates pair of keys – public (id_rsa.pub) and private (id_rsa) in .ssh directory under your home directory.

Registering private key with SSH agent on local machine

Below command will start the SSH agent.
 
eval “$(ssh-agent -s)”
Below command will register the private ssh key with agent.
 
ssh-add ~/.ssh/id_rsa

Adding the public key on Git server

You have to copy the public key from the id_rsa.pub file and paste it on the server SSH key section. Below image shows sample key added on GitHub.
Complex problems, Simple Solutions