Skip to content
masdzub.com
~/articles/ssh — one from the notebook

Hardening SSH on Ubuntu 22.04: Disable Password Auth, Enforce Key-Only Login

Secure Ubuntu 22.04 SSH by disabling password authentication, enforcing key-only login, and validating changes safely without locking yourself out.

masdzub@website:~$ cat ~/articles/hardening-ssh-on-ubuntu-2204-disable-password-auth-enforce-key-only-login.aspx.md
# 1 file · 6 min read · topic: ssh · status: published

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:

Terminal window
lsb_release -a
sshd -V 2>&1 | head -n 1
systemctl status ssh --no-pager

On 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.

Terminal window
ssh-keygen -t ed25519 -a 64 -C "admin@workstation" -f ~/.ssh/id_ed25519_ubuntu2204

Copy the public key to the server. The simplest method uses ssh-copy-id, which also sets safe permissions on the remote side:

Terminal window
ssh-copy-id -i ~/.ssh/id_ed25519_ubuntu2204.pub user@203.0.113.10

If ssh-copy-id is unavailable, install the key manually:

Terminal window
# On the server, as the target user
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Append your public key (paste the contents of id_ed25519_ubuntu2204.pub)
echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyData admin@workstation" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Test key-based login from a new local terminal before modifying the server configuration:

Terminal window
ssh -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10

Do 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:

Terminal window
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:

Terminal window
sudo nano /etc/ssh/sshd_config.d/60-hardening.conf

Add the following directives:

PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
MaxAuthTries 3

Setting 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:

Terminal window
sudo sshd -t

If the command produces no output, the syntax is valid. Reload the SSH service to apply the changes without terminating existing sessions:

Terminal window
sudo systemctl reload ssh

Confirm the effective settings as parsed by sshd:

Terminal window
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:

Terminal window
ssh -i ~/.ssh/id_ed25519_ubuntu2204 -o PreferredAuthentications=publickey user@203.0.113.10

To confirm that password authentication is rejected, you can attempt a connection that explicitly prefers password auth (this should fail):

Terminal window
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@203.0.113.10

A 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:

Terminal window
ssh -vvv -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10

Permission 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:

Terminal window
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys
# Expected:
# drwx------ user user /home/user/.ssh
# -rw------- user user /home/user/.ssh/authorized_keys

Also confirm the server is reading the expected file:

Terminal window
sudo sshd -T | grep authorizedkeysfile

Password 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:

Terminal window
sudo grep -R "PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
sudo sshd -T | grep passwordauthentication

Set 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:

Terminal window
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_ubuntu2204 user@203.0.113.10

If 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:

Terminal window
sudo mv /etc/ssh/sshd_config.d/60-hardening.conf /etc/ssh/sshd_config.d/60-hardening.conf.bak
sudo sshd -t && sudo systemctl reload ssh
found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.