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:
ls -al ~/.sshPublic keys use one of these default file names:
id_ed25519.pubid_ecdsa.pubid_rsa.pubid_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:
ssh-keygen -t ed25519 -C "email@example.com"If a server does not accept Ed25519, use RSA with 4096 bits:
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:
ssh-keygen -p -f ~/.ssh/id_ed25519Without -f, ssh-keygen asks for the file:
$ ssh-keygen -pEnter 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:
pbcopy < ~/.ssh/id_ed25519.pubLinux or WSL (requires the xclip package):
xclip -sel clip < ~/.ssh/id_ed25519.pubGit Bash on Windows:
cat ~/.ssh/id_ed25519.pub | clipTo 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:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@serverRemove 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:
ssh-keygen -R server.example.comYou 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:
ssh -T git@github.comssh -T git@gitlab.comssh -T git@bitbucket.orgClone a repository over SSH:
git clone git@github.com:your_user_name/your_project_name.gitgit clone git@gitlab.com:your_user_name/your_project_name.gitgit clone git@bitbucket.org:your_user_name/your_project_name.gitSwitch an existing repository to SSH
List the remotes of a repository and their URLs:
git remote -vA 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:
git remote set-url origin git@your_server:your_user_name/your_project_name.gitGit 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:
-
In the provider’s control panel, open the server’s console or rescue section (for example a Console & Rescue tab).
-
Enable the Linux rescue system.
-
Boot into rescue mode and connect to it over SSH.
-
Find the root partition with
lsblk(it may be/dev/sda1,/dev/sda2or similar) and mount it:Terminal window mount /dev/sda1 /mnt -
Edit the root user’s
authorized_keys(for another user,/mnt/home/<user>/.ssh/authorized_keys):Terminal window nano /mnt/root/.ssh/authorized_keys -
Replace the old public key with the new one.
-
Unmount the disk and reboot:
Terminal window umount /mntreboot
After the reboot you can log in with the new key.