Docs · Security

Set up SSH keys

Replace password logins with an SSH key pair, then turn password authentication off.

Linux · 5 min read

An SSH key pair is far harder to guess than a password. You keep the private key on your own computer and put the public key on the server.

1. Create a key pair on your computer

Run this on your own machine, not on the server:

ssh-keygen -t ed25519 -C "my-laptop"

Press Enter to accept the default location (~/.ssh/id_ed25519), and set a passphrase when asked. The passphrase protects the key if your laptop is ever lost.

2. Copy the public key to the server

On macOS and Linux:

ssh-copy-id [email protected]

On Windows PowerShell, which has no ssh-copy-id:

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Now log out and back in. You should be asked for the key’s passphrase, not the server password:

ssh [email protected]

3. Turn off password logins

Only do this after confirming that key login works, or you will lock yourself out.

Open the SSH server configuration:

nano /etc/ssh/sshd_config

Find these lines (remove a leading # if present) and set them to:

PasswordAuthentication no
KbdInteractiveAuthentication no

Some distributions also set PasswordAuthentication in a file under /etc/ssh/sshd_config.d/. Check there too:

grep -ri passwordauthentication /etc/ssh/sshd_config.d/

Then reload SSH. Keep your current session open and test from a second terminal before closing it:

# Ubuntu and Debian
systemctl reload ssh

# CentOS
systemctl reload sshd

Keep the private key safe

  • Never copy id_ed25519 (the file without .pub) to a server or share it.
  • Back it up somewhere secure. If you lose it and password login is off, you will need a console or a reinstall to get back in.