Post

Generating an SSH-Key

What is an SSH key?

An SSH key is an access credential in the SSH protocol.

  • Its function is similar to that of user names and passwords, but the keys are primarily used for automated processes and for implementing single sign-on by system administrators and power users.
  • With an SSH key, you can connect to GitHub without supplying your username or password at each visit.
  • Whenever you get a new computer or a new job, you can add your SSH key to your GitHub account, so you can authenticate to GitHub.

Here is how you can generate an SSH key and add it to your GitHub account.

Checking for existing SSH keys

Before you generate an SSH key, you can check to see if you have any existing SSH keys as follows:

  • Open Terminal and enter the following:
1
ls -al ~/.ssh

Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following;

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • Tip: If you receive an error that ~/.ssh doesn’t exist, you do not have an existing SSH key pair in the default location. You can create a new SSH key pair in the next step.

Generate a new SSH key

You can generate a new SSH key on your local machine. After you generate the key, you can add the public key to your account on GitHub.com to enable authentication for Git operations over SSH.

  • Open Terminal and enter the following:
1
ssh-keygen -t ed25519 -C "Your email address"

This creates a new ssh key, using the provided email as a label.

  • When you’re prompted to “Enter a file in which to save the key”, you can press Enter to accept the default file location.
    • Please note that if you created SSH keys previously, ssh-keygen may ask you to rewrite another key, in which case we recommend creating a custom-named SSH key.
    • To do so, type the default file location and replace id_ssh_keyname with your custom key name.
  • At the prompt to enter a passphrase, press Enter twice to create an SSH key pair with no passphrase.
    • However, I recommend using a passphrase because it provides an extra layer of security in case your computer is stolen or compromised.

Add your SSH key to the ssh-agent

  • Start the ssh-agent in the background.
1
eval "$(ssh-agent -s)"

You should see a response similar to the following:

1
Agent pid 59566
  • Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.
1
ssh-add ~/.ssh/id_ed25519

Add the new SSH key to your GitHub account

  • Enter the following to display your SSH key:
1
cat ~/.ssh/id_ed25519.pub
  • Copy the SSH key to your clipboard.

On GitHub.com

  • In the upper-right corner of any page, click your profile photo, then click Settings.
  • In the “Access” section of the sidebar, click 🔑 SSH and GPG keys.
  • Click New SSH key or Add SSH key.
  • In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
  • Select the type of key (authentication)
  • Paste your key into the “Key” field.
  • Click Add SSH key.
  • If prompted, confirm your GitHub password.

Test your SSH connection

  • Open Terminal and enter the following:
1
ssh -T git@github.com
  • If you receive a message similar to the following, then you’re good to go:
1
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

References

Learn more about generating SSH keys on GitHub.

This post is licensed under CC BY 4.0 by the author.