SSH
SSH keys: generating key pairs, passphrases, copying keys, known hosts and testing connections.
2 min read
Remove domain from ssh known hosts if you already have
$ nano ~/.ssh/known_hostsChecking for existing SSH keys
Enter ls -al ~/.ssh to see if existing SSH keys are present:
ls -al ~/.ssh# Lists the files in your .ssh directory, if they existCheck the directory listing to see if you already have a public SSH key.
By default, the filenames of the public keys are one of the following:
- id_dsa.pub
- id_ecdsa.pub
- id_ed25519.pub
- id_rsa.pub
Generating a new SSH key pair
To create a new SSH key pair:
Generate a new ED25519 SSH key pair:
ssh-keygen -t ed25519 -C "email@example.com"# to copy the public keycat ~/.ssh/id_ed25519.pub | pbcopy .Or, if you want to use RSA:
ssh-keygen -t rsa -b 4096 -C "email@example.com"# orssh-keygen -b 4096 -C "email@example.com"# to copy the public keycat ~/.ssh/id_rsa.pub | pbcopy .The -C flag adds a comment in the key in case you have multiple of them and want to tell which is which. It is optional.
Adding or changing a passphrase
If, in any case, you want to add or change the password of your SSH key pair, you can use the -p flag:
ssh-keygen -p -o -f <keyname>or
ssh-keygen -p# Start the SSH key creation processEnter file in which the key is (/Users/you/.ssh/id_rsa): [Hit enter]Key has comment '/Users/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.Copying SSH key
Copy your public SSH key to the clipboard by using one of the commands below depending on your Operating System:
MacOS:
pbcopy < ~/.ssh/id_ed25519.pub# orcat ~/.ssh/id_rsa.pub | pbcopy .WSL / GNU/Linux (requires the xclip package):
xclip -sel clip < ~/.ssh/id_ed25519.pubGit Bash on Windows:
cat ~/.ssh/id_ed25519.pub | clipNote: If you opted to create an RSA key, the name might differ.
Test connecting via SSH
$ ssh -T git@github.com$ ssh -T git@gitlab.com$ ssh -T git@bitbucket.orgExample: To clone repository
$ 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.gitExample: Reconfigure existing repositories to use SSH
List the existing remote repositories and their URLs with:
$ git remote -vThat command should output 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)Change your remote repository’s URL with:
$ git remote set-url origin git@your_server:your_user_name/your_project_name.gitGit will use SSH, instead of HTTPS, to synchronize that local repository with its remote equivalent.
SSH key have expired
If you can’t SSH in at all
- Open your server → Console & Rescue tab.
- Enable Rescue System (Linux).
- Boot into Rescue mode and connect via SSH.
- Mount your main disk: (your disk might be /dev/sda2 — check with lsblk)
mount /dev/sda1 /mnt- Edit authorized_keys:
nano /mnt/root/.ssh/authorized_keys- Replace your old key with the new one.
- Unmount and reboot:
umount /mntrebootNow you should be able to log in normally again with your new SSH key.