Advent calendar From 1 December, a new deal every day at 8 am. To the calendar
Guides

How do I set up SSH keys and disable password login?

In short

You generate a key pair on your own machine, transfer the public part to the server and then disable password authentication in the SSH configuration. The private key stays on your machine. After that, login attempts with guessed passwords get nowhere.

Key facts

Time needed
about 10 minutes
Recommended type
ed25519
Private key
stays local, never copy it
Target file
~/.ssh/authorized_keys
Configuration
/etc/ssh/sshd_config
Important
keep a second session open

1. Generate the key pair

Generate the key on your own machine, not on the server. Ed25519 is considered secure by current standards and produces short keys. Set a passphrase when generating it. It protects the key if someone else gets access to your machine.

  • ssh-keygen -t ed25519 -C "note-about-this-key"

2. Transfer the public key

The public part ends in .pub and may be shared. The easiest way to transfer it is ssh-copy-id. Alternatively, append it by hand to the target user's authorized_keys file.

  • ssh-copy-id <user>@<ip-address>
  • or: cat ~/.ssh/id_ed25519.pub | ssh <user>@<ip> "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

3. Test the login before disabling anything

Open a second session and log in with the key. Only change the server configuration once that works. Keep the first session open until then so you still have access if something goes wrong.

4. Disable password login

Adjust the following lines in /etc/ssh/sshd_config and reload the SSH service. The server then only accepts keys.

  • PasswordAuthentication no
  • PermitRootLogin prohibit-password
  • KbdInteractiveAuthentication no
  • then: systemctl reload ssh

On some systems, files in /etc/ssh/sshd_config.d/ override these values. Check the result with sshd -T | grep -i passwordauthentication.

If you have locked yourself out

On a dedicated server you reach the console via IPMI and can revert the configuration. On virtual servers, use the console access in the customer panel or a rescue system.

Several people, several keys

Give each person their own key. When someone leaves, you remove one line from authorized_keys. A shared password would have to be changed everywhere.

Bottom line

Generate the key, transfer it, test it in a second session and only then disable password login. Keep to this order so you do not lock yourself out.

Questions about our services?

Write to us. Advice on the right configuration is free and without obligation.