Security & OpsEducation3 min read

SSH keys without locking yourself out

Host identity, user authentication and a safe sequence for hardening remote SSH access.

There are two kinds of identity

An SSH server has a host key, used to establish its identity to the connecting client. Users also have authentication key pairs. These are different keys solving different problems. A host-key warning may indicate a legitimate server rebuild, but it can also indicate a man-in-the-middle attack: verify the fingerprint through a trusted channel before accepting a changed key.

For user authentication, the private key remains on the client. The server receives the corresponding public key in the user’s ~/.ssh/authorized_keys.

Create and protect a user key

Current OpenSSH supports Ed25519 keys for ordinary user authentication. A passphrase protects the private key if the file is copied from your workstation.

ssh-keygen -t ed25519 -a 64 -C "my-admin-key"
ssh -i ~/.ssh/id_ed25519 admin@server.example

The exact path syntax differs between shells, but the principle is the same on Windows and Linux. Set sensible access permissions for the private key and the server-side .ssh directory.

Change server policy in the safe order

  1. Install the public key for a non-root administrative account.
  2. Open a second SSH session and verify key authentication works.
  3. Keep the original working session available while changing configuration.
  4. Validate the configuration with sudo sshd -t.
  5. Only then consider disabling password authentication and direct root login.
PasswordAuthentication no
PermitRootLogin no

These directives are examples for sshd_config, not commands for a shell. Cloud-init snippets or included files may override settings, so inspect the effective server configuration.

Defense in depth

Keys reduce risks associated with password guessing, but they do not replace host updates, least privilege, access logs, firewalls or rate limiting. Keep a documented emergency recovery path through your VPS provider’s console whenever possible.

← All articles
SELECTED READING

Keep exploring

View all articles