Introduction
To harden SSH on Ubuntu 22.04, install your public key on the server, confirm you can log in with that key from a separate terminal session, then disable password-based authentication in the SSH daemon configuration and reload the service. This removes exposure to brute-force password guessing while restricting remote access to holders of the corresponding private key.
Prerequisites and Safety Checks
Before changing authentication settings, confirm you have a reliable way to recover access if the new configuration fails. A second active SSH session, out-of-band console access (such as a cloud provider serial or web console), or physical access to the machine prevents a lockout.
Verify the operating system and OpenSSH server version:
lsb_release -asshd -V 2>&1 | head -n 1systemctl status ssh --no-pagerOn Ubuntu 22.04, the SSH service is managed as ssh.service. Keep your current SSH session open until you have successfully tested key-only login from another terminal.
Generate and Install an SSH Key Pair
If you do not already have a dedicated key pair for this server, generate one on your local machine. Ed25519 keys provide strong security with good performance and are supported by the default OpenSSH version shipped with Ubuntu 22.04.
ssh-keygen -t ed25519 -a 64 -C "admin@workstation" -f ~/.ssh/id_ed25519_ubuntu2204Copy the public key to the server. The simplest method uses ssh-copy-id, which also sets safe permissions on the remote side:
ssh-copy-id -i ~/.ssh/id_ed25519_ubuntu2204.pub user@203.0.113.10If ssh-copy-id is unavailable, install the key manually:
# On the server, as the target usermkdir -p ~/.sshchmod 700 ~/.ssh# Append your public key (paste the contents of id_ed25519_ubuntu2204.pub)echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyData admin@workstation" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keysTest key-based login from a new local terminal before modifying the server configuration:
ssh -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10Do not proceed until this login succeeds without prompting for the account password.
Configure sshd for Key-Only Login
Ubuntu 22.04 reads /etc/ssh/sshd_config and includes additional configuration files from /etc/ssh/sshd_config.d/*.conf. On many cloud images, a drop-in file such as 50-cloud-init.conf explicitly enables password authentication, which can override your changes if you only edit the main file. Inspect existing overrides first:
sudo grep -R "PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/Create a dedicated drop-in file to keep your hardening settings explicit and easy to audit:
sudo nano /etc/ssh/sshd_config.d/60-hardening.confAdd the following directives:
PubkeyAuthentication yesPasswordAuthentication noKbdInteractiveAuthentication noPermitRootLogin prohibit-passwordMaxAuthTries 3Setting KbdInteractiveAuthentication no disables keyboard-interactive prompts, which can otherwise allow password entry depending on PAM configuration. The PermitRootLogin prohibit-password value allows root login only with keys; change it to no if direct root access is not required in your environment.
Validate the configuration syntax before applying it:
sudo sshd -tIf the command produces no output, the syntax is valid. Reload the SSH service to apply the changes without terminating existing sessions:
sudo systemctl reload sshConfirm the effective settings as parsed by sshd:
sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin|maxauthtries)'The output should reflect passwordauthentication no and kbdinteractiveauthentication no.
Verify Key-Only Access
With your original session still open, start a new terminal and attempt to connect using only the public key:
ssh -i ~/.ssh/id_ed25519_ubuntu2204 -o PreferredAuthentications=publickey user@203.0.113.10To confirm that password authentication is rejected, you can attempt a connection that explicitly prefers password auth (this should fail):
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@203.0.113.10A hardened server responds with a denial similar to Permission denied (publickey). Only after successful key-based verification should you close the original safety session.
Trade-offs and Limitations
Enforcing key-only SSH login significantly reduces the attack surface for remote password guessing, but it introduces operational constraints. Losing the private key, losing its passphrase, or corrupting ~/.ssh/authorized_keys can result in a complete lockout if no console access is available. Teams must establish a secure process for key distribution, rotation, and revocation, especially when multiple administrators or automated systems require access.
Disabling password authentication also does not protect against compromised private keys, vulnerable SSH versions, or attacks on other exposed services. Treat this change as one layer in a broader hardening baseline that includes a host firewall (such as ufw), timely security updates, and monitoring of authentication logs in /var/log/auth.log.
Troubleshooting
When key-only login fails, the client and server error messages usually point to the cause. Use verbose client output to inspect the authentication flow:
ssh -vvv -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10Permission denied (publickey).
This error means the server did not accept any offered key. Common causes include incorrect file permissions, a mismatched key, or the public key placed in the wrong user’s authorized_keys file. On the server, verify ownership and permissions:
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys# Expected:# drwx------ user user /home/user/.ssh# -rw------- user user /home/user/.ssh/authorized_keysAlso confirm the server is reading the expected file:
sudo sshd -T | grep authorizedkeysfilePassword login still works after reload
A drop-in configuration file is likely overriding your settings. Re-run the grep check and inspect the effective value:
sudo grep -R "PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/sudo sshd -T | grep passwordauthenticationSet PasswordAuthentication no in the highest-priority drop-in (for example, /etc/ssh/sshd_config.d/60-hardening.conf) or remove the conflicting directive from files like 50-cloud-init.conf, then validate and reload again.
/etc/ssh/sshd_config.d/60-hardening.conf line 2: Bad configuration option: PasswordAuthenticationn
This indicates a syntax error, often caused by a typo or invisible characters copied into the file. Open the file, correct the directive, and run sudo sshd -t to confirm the fix before reloading.
Received disconnect from 203.0.113.10 port 22:2: Too many authentication failures
The SSH client may be offering too many keys from your local agent before the correct one. Limit the attempt to a specific identity:
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10If you are locked out of remote SSH but still have console access, revert the hardening file temporarily, reload the service, and re-test your key installation:
sudo mv /etc/ssh/sshd_config.d/60-hardening.conf /etc/ssh/sshd_config.d/60-hardening.conf.baksudo sshd -t && sudo systemctl reload ssh