Skip to content

SSH keys

Create and manage SSH keys: generate a key pair, set a passphrase, copy the public key, test Git hosts and recover a server you are locked out of.

4 min readUpdated

SSH authenticates with a key pair: the private key stays on your machine and the public key goes to the servers and Git hosts you connect to. This note covers checking for existing keys, generating a new pair, changing the passphrase, copying the public key, testing the connection to GitHub, GitLab and Bitbucket, switching a Git remote from HTTPS to SSH, and getting back into a server when your key no longer works.

Check for existing keys

List the contents of your .ssh directory to see whether you already have a key pair:

Terminal window
ls -al ~/.ssh

Public keys use one of these default file names:

  • id_ed25519.pub
  • id_ecdsa.pub
  • id_rsa.pub
  • id_dsa.pub

DSA keys (id_dsa) have been disabled by default since OpenSSH 7.0; replace them with a new key.

Generate a key pair

Ed25519 keys are short and fast, and they are the default type since OpenSSH 9.5. Generate one:

Terminal window
ssh-keygen -t ed25519 -C "email@example.com"

If a server does not accept Ed25519, use RSA with 4096 bits:

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

-C adds a comment to the key, which helps tell keys apart when you have several. It is optional. ssh-keygen asks where to save the key and for a passphrase; a passphrase protects the private key if the file is ever copied.

Add or change a passphrase

To add, change or remove the passphrase of an existing key, use -p with the key file:

Terminal window
ssh-keygen -p -f ~/.ssh/id_ed25519

Without -f, ssh-keygen asks for the file:

Terminal window
$ ssh-keygen -p
Enter file in which the key is (/home/you/.ssh/id_rsa): [Hit enter]
Key has comment '/home/you/.ssh/id_rsa'
Enter new passphrase (empty for no passphrase): [Type new passphrase]
Enter same passphrase again: [One more time for luck]
Your identification has been saved with the new passphrase.

Older guides add -o to save the key in the new OpenSSH format; that format has been the default since OpenSSH 7.8, so the option is no longer needed.

Copy the public key

To paste the public key into a Git host or a control panel, copy it to the clipboard. Use the command for your operating system (replace id_ed25519 with id_rsa for an RSA key).

macOS:

Terminal window
pbcopy < ~/.ssh/id_ed25519.pub

Linux or WSL (requires the xclip package):

Terminal window
xclip -sel clip < ~/.ssh/id_ed25519.pub

Git Bash on Windows:

Terminal window
cat ~/.ssh/id_ed25519.pub | clip

To install the key on a server you can already log in to with a password, ssh-copy-id appends it to ~/.ssh/authorized_keys on the server:

Terminal window
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

Remove a host from known_hosts

When a server is reinstalled, its host key changes and SSH refuses to connect with a warning. Remove the old entry for that host:

Terminal window
ssh-keygen -R server.example.com

You can also edit ~/.ssh/known_hosts by hand and delete the line for the host.

Test the connection to Git hosts

-T disables the terminal; the host replies with a greeting that confirms which account the key belongs to:

Terminal window
ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@bitbucket.org

Clone a repository over SSH:

Terminal window
git clone git@github.com:your_user_name/your_project_name.git
git clone git@gitlab.com:your_user_name/your_project_name.git
git clone git@bitbucket.org:your_user_name/your_project_name.git

Switch an existing repository to SSH

List the remotes of a repository and their URLs:

Terminal window
git remote -v

A repository cloned over HTTPS shows something like:

origin https://your_server/your_user_name/your_project_name.git (fetch)
origin https://your_server/your_user_name/your_project_name.git (push)

Point the remote to the SSH URL:

Terminal window
git remote set-url origin git@your_server:your_user_name/your_project_name.git

Git now uses SSH instead of HTTPS to sync with that remote.

Recover access when your key no longer works

SSH keys do not expire, but you can lose the private key or replace it on your machine while the server still has only the old public key. If you cannot log in at all, use the hosting provider’s rescue system to put the new public key on the server:

  1. In the provider’s control panel, open the server’s console or rescue section (for example a Console & Rescue tab).

  2. Enable the Linux rescue system.

  3. Boot into rescue mode and connect to it over SSH.

  4. Find the root partition with lsblk (it may be /dev/sda1, /dev/sda2 or similar) and mount it:

    Terminal window
    mount /dev/sda1 /mnt
  5. Edit the root user’s authorized_keys (for another user, /mnt/home/<user>/.ssh/authorized_keys):

    Terminal window
    nano /mnt/root/.ssh/authorized_keys
  6. Replace the old public key with the new one.

  7. Unmount the disk and reboot:

    Terminal window
    umount /mnt
    reboot

After the reboot you can log in with the new key.

References

Related notes

1 min read

GNOME

Make the GNOME login screen (GDM) ask for a username instead of showing the list of users.

  • Linux & Systems
4 min read

Jitsi Meet

Self-host Jitsi Meet on Debian: install from the Jitsi repository, add a Let's Encrypt certificate, open the firewall, require login and check the services.

  • Linux & Systems
2 min read

Trivy

Install Trivy and scan project files, Git repositories, container images and Kubernetes workloads for vulnerabilities and misconfigurations.

  • Security
  • DevOps & Containers

Type to search the whole site.