How to set up SSH keys on Ubuntu 22.04

Photo by Dan Nelson on Unsplash

How to set up SSH keys on Ubuntu 22.04

What is SSH?

The Secure Shell protocol (SSH) is used to create secure connections between your device and GitHub Cloud. SSH uses two keys, a public key and a private key.

  • The public key can be distributed.

  • The private key should be protected.

The connection is authenticated using public SSH keys, which are derived from a private SSH key (also known as a private/public key pair). The secure (encrypted) connection is used to securely transmit your source code between your local device and GitHub Cloud.

Use SSH keys to communicate with GitHub

Git is a distributed version control system, which means you can work locally, and then share or push your changes to a server. In this case, the server you push to is GitHub.

GitHub uses the SSH protocol to securely communicate with Git. When you use SSH keys to authenticate to the GitHub remote server, you don’t need to supply your username and password each time.

Prerequisites

To use SSH to communicate with GitHub, you need:

  • The OpenSSH client, which comes pre-installed on GNU/Linux, macOS, and Windows 10.

  • SSH version 6.5 or later. Earlier versions used an MD5 signature, which is not secure.

Hands-on Lab Overview

In this hands-on lab, I will create an AWS EC2 instance using AWS SDK. I will manually configure the default Security Group by adding a new inbound rule to allow incoming SSH traffic from my custom IP. Then, I will generate a new key pair on this EC2 instance and push the public key onto my GitHub account. Finally, I will connect to EC2 to verify the connection.

Hands-on Lab

To set up your device for connecting to GitHub Cloud using SSH, you need to:

  1. View the version of SSH installed on your system, run ssh -V:

  2. See if you have an existing SSH key pair:

     ls -al ~/.ssh
     # Lists the files in your .ssh directory, if they exist
    
  3. 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.

💡
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.
  1. Generate a new SSH key. Paste the text below, replacing the email used in the example with your GitHub email address:

     ssh-keygen -t ed25519 -C "your_email@example.com"
    
    💡
    If you are using a legacy system that doesn't support the Ed25519 algorithm, use: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    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_ALGORITHM with your custom key name.

  2. At the prompt, type a secure passphrase:

     > Enter passphrase (empty for no passphrase): [Type a passphrase]
     > Enter same passphrase again: [Type passphrase again]
    

  3. Start the ssh-agent in the background:

     $ eval "$(ssh-agent -s)"
    

  4. 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.

     ssh-add ~/.ssh/id_ed25519
    

  5. Add the SSH public key to your account on GitHub. Copy the contents of your public key file. You can do this manually or use a script:

     cat ~/.ssh/id_ed25519.pub
    

    💡
    You can use xclip to copy an ED25519 key to the clipboard: sudo apt update and sudo apt-get -y install xclip
  6. Verify that you can connect by running this command:

     ssh -T git@github.com
    

References

  1. ec2-with-sdk

  2. Use SSH keys to communicate with GitLab

  3. Checking for existing SSH keys

  4. About SSH

  5. Adding a new SSH key to your GitHub account

  6. Set up personal SSH keys on Linux

  7. How To Install xclip on Ubuntu 22.04