Log inSign up

SSH keys and basic hardening, without a lockout

Generate an Ed25519 key on your own computer, put the public half on the server, confirm that key login works, and only then switch off password authentication. Since there is no browser console to fall back on, every step below includes a test before the next change.

Why move from passwords to keys at all?

A key cannot be guessed, and a public IPv4 address starts receiving automated login attempts within minutes of going live. Watch it happen on your own instance:

sudo journalctl -u ssh --since "1 hour ago" | grep -c "Failed password"

On the RHEL family the unit is called sshd instead of ssh. Each line is a bot trying common passwords. With keys only, those attempts fail before they reach the password stage, the log stays quieter, and a leaked or reused password is no longer a way in. The cost is one small file you must keep safe on your laptop.

How do you create a key pair on your computer?

Run ssh-keygen locally, never on the server. Ed25519 keys are short, fast and accepted by every image we offer.

ssh-keygen -t ed25519 -C "laptop-2026" -f ~/.ssh/bvps_ed25519

Pick a passphrase when asked. It encrypts the private key on disk, so a stolen laptop does not hand over your servers. The command writes two files: ~/.ssh/bvps_ed25519 (private, stays with you) and ~/.ssh/bvps_ed25519.pub (public, goes to servers). Print the public half with cat ~/.ssh/bvps_ed25519.pub; it is a single line starting with ssh-ed25519.

How does the key get onto the instance?

There are two routes, and you can use both.

  • Before deploy: paste the public key on the SSH keys page of the console and tick it on the Deploy screen. New instances boot with it in /root/.ssh/authorized_keys.
  • After deploy: copy it to an existing server with one command from your computer.
ssh-copy-id -i ~/.ssh/bvps_ed25519.pub deploy@198.51.100.24

Keys saved in the console are applied at deploy time only. Adding a key there later does not push it into servers that are already running, so use ssh-copy-id or edit ~/.ssh/authorized_keys by hand for those.

To make logins short, add an entry to ~/.ssh/config on your computer:

Host node1
    HostName 198.51.100.24
    User deploy
    IdentityFile ~/.ssh/bvps_ed25519

Now ssh node1 is enough.

How do you prove the key works before changing anything?

Force the client to use the key and refuse passwords for one test login:

ssh -o PasswordAuthentication=no -i ~/.ssh/bvps_ed25519 deploy@198.51.100.24 'sudo -n true && echo ok'

If it prints ok, key login and passwordless sudo work. If sudo asks for a password, that is fine too, it just means your user has a password for sudo, which is the safer default. What matters is that the SSH step succeeded with no password prompt. Do not continue until this test passes.

Which sshd settings should change?

Put your changes in a drop-in file so package upgrades never overwrite them. Ubuntu 22.04 and newer, Debian 12 and 13, and the RHEL family all read /etc/ssh/sshd_config.d/.

sudo tee /etc/ssh/sshd_config.d/10-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
MaxAuthTries 3
LoginGraceTime 20
AllowUsers deploy root
EOF

prohibit-password keeps root reachable with a key for emergencies while stopping root password logins. If you prefer no root SSH at all, use PermitRootLogin no, but only after your sudo user has been tested. Some images ship a cloud-init file in the same folder that sets PasswordAuthentication yes; list the folder with ls /etc/ssh/sshd_config.d/ and remove or edit that file, because the first value read wins.

How do you reload SSH safely?

Check the syntax, reload, and keep your current session open while you test from a new one.

sudo sshd -t && sudo systemctl reload ssh    # Ubuntu, Debian
# sudo sshd -t && sudo systemctl reload sshd # RHEL family
sudo sshd -T | grep -Ei 'passwordauth|permitroot|maxauth'

A reload does not drop existing sessions. From a second terminal, log in again with the key. Then try a password login on purpose and confirm it is refused:

ssh -o PubkeyAuthentication=no deploy@198.51.100.24
# expected: Permission denied (publickey).

Only close the original session once both checks look right.

Is fail2ban still worth adding?

Yes, as a noise filter. With passwords off, bots cannot get in, but fail2ban bans their addresses for a while and keeps the logs readable.

sudo apt-get install -y fail2ban
sudo tee /etc/fail2ban/jail.local >/dev/null <<'EOF'
[sshd]
enabled  = true
backend  = systemd
maxretry = 4
findtime = 10m
bantime  = 1h
EOF
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

The status command lists how many addresses are banned right now. If you ever ban yourself by mistake, wait for the ban to expire or unban from another network with sudo fail2ban-client set sshd unbanip 203.0.113.7.

What else reduces risk on a public server?

A few habits do more than any single setting:

  • Turn on unattended security updates: sudo apt-get install -y unattended-upgrades && sudo dpkg-reconfigure -plow unattended-upgrades.
  • Keep a second key, stored offline, in authorized_keys so a lost laptop is not a lost server.
  • Remove keys of people who no longer need access, and review the file now and then.
  • Moving SSH to another port only cuts log noise; it is not a security control. If you do it, open the new port in your firewall first.

Pair this tutorial with firewall rules so only the services you mean to expose are reachable.

Questions about this tutorial

Can I add an SSH key to a running server from the console?

Keys from the console are written at deploy time. For a running instance, use ssh-copy-id or append the key to ~/.ssh/authorized_keys yourself.

RSA or Ed25519?

Ed25519 is the better default: shorter keys, fast signatures and support in every current OpenSSH. Use RSA 4096 only for an old client that cannot handle Ed25519.

What if a bad SSH change shuts me out?

Without a web console the only self-service fix is a reinstall, which erases the disk. Test every change from a second session and keep your backups current.

Does disabling password login affect sudo?

No. sshd settings only control how you reach the server. sudo still asks for your user password unless you configured it otherwise.

Should FreeBSD users follow the same steps?

Mostly. The file is /etc/ssh/sshd_config, the reload command is service sshd reload, and fail2ban is replaced by blacklistd or a pf table if you want one.