How to generate Git SSH Keys (Windows, Mac & Linux)

In this tutorial, you will learn step-by-step how to generate, set up and store a Git SSH key for repository cloning.

For any operations with your repository (cloning, pushing, building, …) you need an authentication method between your local computer and a Git repository (e.g. GitHub or Bitbucket) – and this is where SSH keys (also called SSH key pairs) come into play. I’ll show you how to store a Git SSH key – on Mac and Linux as well as Windows.

What is a Git SSH Key?

A Git SSH key is a secure access mechanism that enables authentication to Git repositories. It is a combination of a public and private key that ensures secure communication between your computer and a Git server. This key plays a critical role in ensuring the integrity and security of your Git transactions. Read this article for a technical explanation of how Public-Private-Keypairs work.

Generate a Git SSH Key on Mac and Linux

On Linux, you can create the SSH key directly from the terminal. This also works if you are using WSL2 (Windows Subsystem for Linux) with a Linux distribution. To generate a new SSH key, run the following command:

ssh-keygen -t rsa -b 4096 -C "your-mail@example.com"

Where your-mail@example.com is the email address you want to use in your Git repository.

Optionally, you can set a password (passphrase) that you will be asked to enter each time you want to use the key:

Generate SSH keypair under Linux
Generate SSH keypair under Linux

The private and public keys are saved under ~/.ssh/. You can find the files id_rsa and id_rsa.pub in that directory. The file named id_rsa.pub is your public key, whereas id_rsa is your private key.

Attention: It is imperative that you retain this private key and refrain from sharing it with any third parties.

Execute the command below to modify the permissions of the SSH key. Failing to do so will result in your key being ignored by the system.

chmod 400 ~/.ssh/id_rsa

The key pair is created. Now you have to deposit the public key in your Git repository provider, so jump straight to it:

➜ Setup in Git Repository

Generate a Git SSH Key on Windows

Under Windows, you require the program PuTTY Key Generator (PuTTYgen). If you have already installed PuTTY on your computer, it is probable that PuTTYgen is also installed.

Open the program and select “Generate”. Hover over the white space to create a random key pair. Leave the remaining settings unchanged.

PuTTY Key Generator
PuTTY Key Generator

Once the process has finished, select “Conversions” from the menu and choose “Export OpenSSH key.” Save the file as id_rsa under C:\Users\.ssh.

Attention: It is imperative that you retain this private key and refrain from sharing it with any third parties.

The private key file (id_rsa) requires particular permissions; otherwise, it will be disregarded due to security concerns.

Permissions for “/Users/<YOUR_USER>/.ssh/id_rsa” are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored.

If you come across this error message, follow the steps below.

  • In Windows Explorer, navigate to your private key: C:\Users\.ssh\id_rsa
  • Right click > Properties > Security
  • Select “Advanced” at the bottom
  • At the top you can change the owner of the file to your user (admin rights required).
  • Then remove all entries from the list of permissions and add full access for your user.

At the end of the process, the visual output should resemble this.

Windows: Advanced security settings for id_rsa
Windows: Advanced security settings for id_rsa

In the final step, it is necessary to include the public key into our Git account, such as at GitLab, GitHub, or Bitbucket. This can be achieved by copying the entire content from the designated blue text box and preserving it for the ensuing procedure. The public key corresponds to your private key found in the id_rsa file.

PuTTY Key Generator
PuTTY Key Generator

Deposit SSH key with the Git repository provider

Depositing your generated key into your Git repository provider is straightforward. I will use GitHub as an example to demonstrate the process, but other providers follow similar steps.

Log in to GitHub and go to the settings.

GitHub Settings
GitHub Settings

Im linken Menü findest du den Punkt “SSH and GPG keys”.

GitHub SSH Keys Settings
GitHub SSH Keys Settings

Click on “New SSH key” there.

GitHub Add SSH Key
GitHub Add SSH Key

Choose a suitable title, such as <PC-NAME>-<DATE>, and add the public key. When saving, you will be prompted for your GitHub password again and will receive an email notification for security purposes.

Test Git SSH Key

That’s it! Now you can take a private repository, click on “Code”, select “SSH” and clone the repository via git clone <REPO_SSH_URL>.

GitHub: Clone Repository
GitHub: Clone Repository
Related Posts
Join the Conversation

1 Comment

Your email address will not be published. Required fields are marked *

bold italic underline strikeThrough
insertOrderedList insertUnorderedList outdent indent
removeFormat
createLink unlink
code

This can also interest you