SSH Hardening & Key-Based Authentication
Before you begin
- Applies to:Linux hosts running OpenSSH Server.
- Where to start:Keep a working administrative session or console to the server. Use a separate client session to test sign-in before closing it.
- What you need:Have approved administrative access and a verified working key login before changing password or root login settings.
Guide tools
Securing remote SSH access is the first and most critical defense for any internet-facing Linux VPS. Follow these modular configuration steps to lock down port 22:
Generate an Ed25519 Cryptographic Key Pair
Local MachineAvoid legacy RSA keys. Modern Ed25519 keys offer superior cryptographic resilience with compact 256-bit key length and faster handshake speeds.
ssh-keygen -t ed25519 -C "admin@configcorner"❯ Illustrative Output (Results Vary)
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase): [PROTECTED]
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
Deploy Public Key to Target VPS
TransferAppend your local public key to the remote server’s ~/.ssh/authorized_keys file using ssh-copy-id. Before changing server policy, open a second terminal and verify that a new session succeeds with the key. Keep the current administrative session open as a recovery path.
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@vps-ip-address
# In a second terminal, require public-key authentication for this test:ssh -o PreferredAuthentications=publickey \ -o PasswordAuthentication=no user@vps-ip-address❯ Illustrative Output (Results Vary)
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “id_ed25519.pub”
Number of key(s) added: 1
Now try logging into the machine with: “ssh user@vps-ip-address”
Disable Password Authentication & Enforce Key-Only
HardeningFirst confirm that the main SSH configuration includes /etc/ssh/sshd_config.d/*.conf and check existing files for earlier active values. OpenSSH generally keeps the first value it reads, so a file named 99-… may not override an earlier drop-in. This example uses an early filename; still verify the effective settings before reloading.
sudo mkdir -p /etc/ssh/sshd_config.dsudo test ! -e /etc/ssh/sshd_config.d/00-local-hardening.conf || { echo 'Drop-in already exists; inspect and merge it instead of overwriting.' >&2; exit 1; }sudo test ! -L /etc/ssh/sshd_config.d/00-local-hardening.conf || { echo 'Drop-in is a symlink; inspect it before making changes.' >&2; exit 1; }sudo tee /etc/ssh/sshd_config.d/00-local-hardening.conf >/dev/null <<'EOF'PasswordAuthentication noPermitRootLogin noPubkeyAuthentication yesAuthenticationMethods publickeyMaxAuthTries 3EOF
sudo sshd -tsudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|pubkeyauthentication|authenticationmethods|maxauthtries) 'Validate Configuration and Reload SSH
VerificationValidate syntax and effective values. If the server uses Match blocks, also check the effective configuration for the actual user, client address, and host. Reload whichever unit is active on this distribution; reload keeps the current SSH session available while new connections use the updated configuration.
sudo sshd -tsudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|pubkeyauthentication|authenticationmethods|maxauthtries) '
if systemctl is-active --quiet ssh.service; then sudo systemctl reload ssh.serviceelif systemctl is-active --quiet sshd.service; then sudo systemctl reload sshd.serviceelse echo 'No active ssh.service or sshd.service found; identify the service name before applying changes.' exit 1fi❯ Illustrative Output (Results Vary)
passwordauthentication no
permitrootlogin no
pubkeyauthentication yes
authenticationmethods publickey
maxauthtries 3
● sshd.service - OpenSSH server
Active: active (running)
Your feedback keeps these production procedures accurate and battle-tested.