Git - Generating SSH Key



Users authenticate with Git servers by utilizing SSH public keys, which they need to generate if they do not already have them.

  • SSH key generation is done in the same way on all operating systems.

  • Users need to make sure they have a key before generating new ones.

  • The user's ~/.ssh directory contains SSH keys by default.

Use these commands to list the contents of the ~/.ssh directory and check if you already have a SSH key:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub

To generate a SSH key with Git services for secure authentication, you need to follow the following steps:

1. Open a Terminal

Open up the terminal on your respective operating system, such as Git Bash in Windows.

2. Generate the SSH Key

To generate a new SSH key pair using RSA (recommended) with a 4096-bit key size, run the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • -t rsa − Signifies the type of key to be created (RSA).

  • -b 4096 − Signifies the bit-size, where 4096 is for strong encryption.

  • -C "your_email@example.com" − Signifies the email id with which the SSH key gets associated and used for identification.

3. Specify the Location

In order to save the key, you will be asked to specify a location. You can specify a desired location or press enter to accept the default location provided, which is ~/.ssh/id_rsa.

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

4. Set a Passphrase (optional)

A passphrase can also be added, though it is optional, it adds an extra layer of security. This step can be skipped even.

Enter passphrase (empty for no passphrase):

5. View the Generated SSH Key

After following the steps mentioned above, your SSH key pair will be created. You will have two files, one for a private key and other for public key.

  • Private Key − You need to keep this file safe and do not share it ~/.ssh/id_rsa

  • Public Key − You need to share this file with services ~/.ssh/id_rsa.pub

6. Add the SSH Key to the SSH Agent

In order to ensure the SSH Key is available for Git services, you need to add it to the SSH agent.

  • Start the agent.

    eval "$(ssh-agent -s)"
  • Add your key.

    ssh-add ~/.ssh/id_rsa

7. Copy the Public Key

Add the content of the public key file id_rsa.pub to your Git service (GitHub, GitLab, BitBucket, etc.). You can use the following command to display the public key:

cat ~/.ssh/id_rsa.pub

Select and copy the output to be added to the Git Service. The actual public key content is a large string of characters (AAAAB3NzaC1yc2EAAAABIwAAAQEA2kLx2K5DwsUgrA8WqA+zm4e5+JY1ZqD0Gi?.......).

8. Add SSH Key to Git Service

GitHub

To add the SSH Key to GitHub, go to Settings > SSH and GPG keys and click New SSH key.

GitLab

To add the SSH Key to GitLab, go to Profile Settings > SSH keys and paste the public key.

BitBucket

To add the SSH Key to BitBucket, go to Personal Settings > SSH keys and add a new key.

Once the key has been added to your Git service, you can clone, push, and pull the repositories using SSH.

Advertisements