Skip to content

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

Terminal window
$ nano ~/.ssh/known_hosts

Checking for existing SSH keys

Enter ls -al ~/.ssh to see if existing SSH keys are present:

Terminal window
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

Check 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:

Terminal window
ssh-keygen -t ed25519 -C "email@example.com"
# to copy the public key
cat ~/.ssh/id_ed25519.pub | pbcopy .

Or, if you want to use RSA:

Terminal window
ssh-keygen -t rsa -b 4096 -C "email@example.com"
# or
ssh-keygen -b 4096 -C "email@example.com"
# to copy the public key
cat ~/.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:

Terminal window
ssh-keygen -p -o -f <keyname>

or

Terminal window
ssh-keygen -p
# Start the SSH key creation process
Enter 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:

Terminal window
pbcopy < ~/.ssh/id_ed25519.pub
# or
cat ~/.ssh/id_rsa.pub | pbcopy .

WSL / GNU/Linux (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

Note: If you opted to create an RSA key, the name might differ.

Test connecting via SSH

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

Example: To clone repository

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

Example: Reconfigure existing repositories to use SSH

List the existing remote repositories and their URLs with:

Terminal window
$ git remote -v

That command should output something like:

Terminal window
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:

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

Git 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

  1. Open your server → Console & Rescue tab.
  2. Enable Rescue System (Linux).
  3. Boot into Rescue mode and connect via SSH.
  4. Mount your main disk: (your disk might be /dev/sda2 — check with lsblk)
Terminal window
mount /dev/sda1 /mnt
  1. Edit authorized_keys:
Terminal window
nano /mnt/root/.ssh/authorized_keys
  1. Replace your old key with the new one.
  2. Unmount and reboot:
Terminal window
umount /mnt
reboot

Now you should be able to log in normally again with your new SSH key.

Related articles

1 min read

gnome

Making the GNOME login screen ask for a username instead of showing the user list.

  • Linux & Systems
2 min read

Jitsi

Self-hosting Jitsi Meet: installation, Let's Encrypt, firewall rules, secure meetings and monitoring.

  • Linux & Systems
1 min readTürkçe

Steam

Linux'ta Steam ve Counter-Strike 2: GPU ayarları ve başlatma seçenekleri.

  • Linux & Systems

Type to search the whole site.