DEV Community

Cover image for How To Generate Git SSH Keys
devconnected
devconnected

Posted on • Originally published at devconnected.com

How To Generate Git SSH Keys

If you have been working with Git for quite some time, you know how important it is to setup SSH authentication.

SSH authentication is one of two ways of logging to your Git remote repository and pushing modifications to it.

Using SSH keys with Git, you don’t have to use your password anymore.

You simply have to create your SSH key, store it on your local machine and configure it for your Git client to use it to connect to your remote.

In this tutorial, we are going to learn how you can generate SSH keys for Git, whether you are on Linux or on Windows.

Note : if you are using Github, follow this tutorial to setup SSH keys on Github

In order to generate SSH keys for your Git repository, use the “ssh-keygen” command and specify the encryption algorithm that you want to use.

$ ssh-keygen -t rsa -b 4096 -C "email@example.com"

Note that it is recommended to generate your SSH keys in the “.ssh” directory of your home directory.

When using the ssh-keygen utility, you will be prompted with multiple questions.

First, you will be asked to provide the filename for your key as well as the passphrase.

For the filename, you usually want to leave the default options and simply press Enter.

For the passphrase, we won’t bother choosing a passphrase for this tutorial.

Even if it includes an extra layer of security for your Git repository, you will be prompted with the passphrase every time that you want to perform an operation on the repository.

The ssh-keygen utility created two files for you :

  1. id_rsa : this is the private key of your SSH key pair, you should not share this key with anybody.
  2. id_rsa.pub : this is the public key of your SSH key pair, this is the key that you will copy to your server in order to connect to it seamlessly.
    Generate SSH keys for Git on Windows
    In order to generate SSH keys for Git on Windows, you have to enable the OpenSSH commands using the “Add-WindowsCapability” command.

    $ Add-WindowsCapability -Online -Name OpenSSH.Client*

    Path :
    Online : True
    RestartNeeded : False

Note : you need to be administrator in order to enable OpenSSH on your computer.

Now that OpenSSH is enabled and configured on Windows, simply use the “ssh-keygen” command in order to generate your SSH keys.

$ ssh-keygen -t rsa -b 4096 -C "email@example.com"

By default, you will be prompted to provide a public key filename as well as a passphrase.

You can leave those options empty : by default, your key-pair will be named “id_rsa” (or “id_rsa.pub” for the public key).

When it comes to the passphrase, it provides more security to add one but you will be asked to provide it on every SSH authentication.

generate git ssh keys on windows
As you can see, your SSH keys for Git are located in the “.ssh” directory of your user home.

The ssh-keygen utility created two files for you :

  1. id_rsa : this is the private key of your SSH key pair, you should not share this key with anybody.
  2. id_rsa.pub : this is the public key of your SSH key pair, this is the key that you will copy to your server in order to connect to it seamlessly. Setup SSH authentication on Git Now that your SSH keys are generated for Git, it is time to add them to your remote Git repository in order to push changes.

In order to setup SSH authentication on the server, you will have to copy the content of the public key to the “authorized_keys” file of the server.

Copy SSH keys to your Git server

On the client, use the “cat” command in order to see the content of the public key.

$ cat ~/.ssh/id_rsa.pub

Copy the output of your cat command, and paste it into the “authorized_keys” file of your server (or remote repository).

$ sudo vi /home/git/.ssh/authorized_keys

Alternatively, if you don’t want to copy and paste the content of your public key, you can use the “ssh-copy-id” command.

This command will simply take the content of your “.ssh” directory and add the relevant public keys to the “authorized_keys” file of the server.

$ ssh-copy-id <user>@<remote>

Now that your keys are correctly configured for Git, you should be able to pull and push modifications to your Git repository.

Head over to the directory where your project files are stored.

$ cd /home/myproject

If you don’t have a Git repository, initialize one using the “git init” command.

$ git init

If there is already a “.git” file in your directory, it means that there is already a repository initialized.

Note : in order to get the content from your existing Git repository, use the “git clone” command.

Now that your Git repository is correctly configured, add the remote repository using the “git remote add” command.

$ git remote add origin git@<server_ip>:<path_to_git_repository>

On the server, if the “project.git” file (i.e the repository root) is located directly on the git user home directory, you would write

$ git remote add origin git@8.8.8.8:project.git

After adding your remote, make sure that your modifications are effective by running the “git remote -v” command.

$ git remote -v

origin   git@8.8.8.8:project.git (fetch)
origin   git@8.8.8.8:project.git (push)

Great!

Let’s now try to add some files in order to test if files can be pushed to your remote repository.

Create a README file using the “touch” command.

$ touch README

Add the files using the “git add” command and commit them using the “git commit” command.

$ git add .

$ git commit -m "This is the first commit of this repository"

Finally, push your modifications : the push operation should not require any password using the SSH method.

$ git push

Awesome!

You have correctly configured SSH key-based authentication for your Git repository.

Conclusion

In this tutorial, you learnt how you can generate SSH keys for Git easily using the ssh-keygen utility.

You learnt that the same utility is used on Linux and on Windows.

The directories used in order to store the keys are also the same.

If you are interested about Git or about software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!

Top comments (0)