Ssh key

ssh key #

An ssh key allows you to connect to a remote machine via the ssh protocol. Among other uses, this is the preferred way to share data with a git hosting service (such as GitHub, Gitlab or Bitbucket).

You only need one ssh key per machine (laptop, desktop, phone, etc.). So from your laptop, you can use the same ssh key to connect to a server or interact with GitHub, Gitlab, etc.

By default, ssh keys are located in the directory <homeDir>/.ssh/ (or <homeDir>\.ssh\ on Windows) where <homeDir> is your home directory (i.e. the one with your user name).

Check whether there is already an ssh key on your machine #

There may already be an ssh key on your machine. To check whether this is the case:

  • verify whether the <homeDir>/.ssh/ already exists,
  • if it exists, then open it and check whether it contains two files with the same name, one without extension, and one with the extension .pub. For instance, id_rsa and id_rsa.pub, or id_ed25519 and id_ed25519.pub.

Warning. The .ssh directory is a hidden directory, so make sure that your file browser displays hidden files and directories. Alternatively, with a terminal you can list all files and directories in the current directory (including hidden ones) with:

  • Linux and macOS:
ls -a
  • Windows:
ls -force

If these two files are already present, then you already have an ssh key, and you can skip the next step.

Create an ssh key (if there was none) #

If the .ssh directory does not exist or does not contain an ssh key, then generate a key with:

ssh-keygen

and follow the instructions (note that if you choose to enter a password, then you will have to type it each time you upload or download data to/from Github, Gitlab, etc.).

Share your public key #

If you followed the instructions above, you should now have an ssh key one your machine (whether it was already there or you just created it)

Your ssh key consists of two files, with the same name (modulo the extension .pub):

  • the file with the .pub extension is your public key,
  • the file without the .pub extension is your private key.

In order to connect to/exchange with a remote machine via ssh, you need to share the public key: this is the content of the file with the pub extension.

with GitHub #

This is how to share your public ssh key with GitHub (the procedure is similar for GitLab, etc.):

  1. login to GitHub,
  2. click on your user icon (top right), and select “Settings”,
  3. in the left menu, select “SSH and GPG keys”,
  4. click on the button “new ssh key”,
  5. give a name to your key (e.g. my_laptop) in the field “Title”,
  6. paste the content of the .pub file in the field “Key”,
  7. click on the “Add SSH key” button.