Configure SSH Key Authentication and Disable Password Login on a WordPress Server

SSH keys only: disable password login on a WordPress server

Every server with an open SSH port gets thousands of login attempts a day from bots that try common user names and passwords. With key authentication and passwords switched off, these attempts cannot succeed. The change takes five minutes; the only risk is locking yourself out, and the order of the steps below avoids that.

Problem: /var/log/auth.log is full of “Failed password for root” lines, and the server still accepts passwords for SSH.

Solution: Create an Ed25519 key on your computer, copy the public key to the server with ssh-copy-id, test the key login in a second session, then set PasswordAuthentication no and PermitRootLogin no in a drop-in file under /etc/ssh/sshd_config.d/, test the config and reload SSH.

On your computer

ssh-keygen -t ed25519 -C "laptop-2023"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10
ssh deploy@203.0.113.10

Give the key a passphrase; ssh-agent remembers it for the session (see add a private key to ssh-agent permanently). ssh-copy-id appends the key to ~/.ssh/authorized_keys of the user and sets the permissions. Use a normal user with sudo, not root.

On the server

Keep the current session open until the end. Then:

sudo tee /etc/ssh/sshd_config.d/10-ha-hardening.conf > /dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
AllowUsers deploy
EOF

sudo sshd -t
sudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin'
sudo systemctl reload ssh

Ubuntu 22.04 and Debian 12 read the files in sshd_config.d/, and the first value found for an option wins. Cloud images often ship 50-cloud-init.conf with PasswordAuthentication yes; the file name starting with 10- is read before it. sshd -T prints the effective configuration, so you see what really applies, not what you think you wrote. sshd -t checks the syntax; reload only if it prints nothing.

Test from a new terminal

ssh deploy@203.0.113.10                                    # works with the key
ssh -o PubkeyAuthentication=no deploy@203.0.113.10         # Permission denied (publickey)
ssh root@203.0.113.10                                      # Permission denied

Only when all three behave like this, close the old session.

Several people and deploy keys

Each person and each deploy system (a CI job, a Git hook) gets its own key in authorized_keys, with a comment that says whose it is. Removing access is then deleting one line. Deploy keys for Git hosting are covered in SSH for Git on a remote server.

NOTE: Without passwords, losing the key means losing access. Keep a second key in a safe place, and know how to reach the provider’s web console. Bots will keep knocking; Fail2ban removes them from the logs, see Fail2ban and unattended upgrades.