1. Update OpenSSH
Use the distribution’s packages; security fixes may be backported without changing the upstream version number. Follow Update SSHD.2. Create and test an administrator account
On the VPS, create an account if you do not already have one. Replaceadminuser throughout this guide with your chosen username:
3. Configure SSH key authentication
On your computer, generate a key if you do not already have one. Protect it with a passphrase and do not overwrite an existing key you still use:ssh-copy-id copies the public key; keep the private key on your computer. If your client does not include this tool, use its public-key installation instructions. See the OpenSSH key-generation manual.
Test a fresh connection that permits only public-key authentication:
sudo -v also works in this new session.
4. Restrict login methods
Back up/etc/ssh/sshd_config and any files you will edit in /etc/ssh/sshd_config.d/. Inspect the existing configuration before adding settings: included snippets can take precedence over the main file.
For a key-only setup without PAM-based MFA, configure:
sudo.
Check syntax and the effective configuration:
Match blocks, effective settings can differ by user and connection. Resolve unexpected values before proceeding. OpenSSH generally uses the first value found for each setting; see Ubuntu’s OpenSSH configuration guide and the OpenSSH configuration reference.
If validation passes, reload SSH on Debian/Ubuntu:
sudo before closing your original session.
5. Apply firewall rules before enabling the firewall
Allow the actual SSH TCP port before enabling UFW. Port 22 is the default; replace it if you changed it. Follow the UFW setup guide, including its reconnect test. A custom port can reduce automated login noise, but it is not a substitute for authentication or access controls. Port changes also require matching firewall, client, and sometimesssh.socket configuration. They are optional and are not part of the key-only procedure above.
6. Monitor access and consider additional controls
Inspect recent SSH events on Debian/Ubuntu:/var/log/auth.log exists on a journal-only installation.
For MFA, use a complete procedure for your distribution and authentication provider. Enabling keyboard-interactive authentication alone does not require a second factor. An MFA configuration must combine the intended methods and enroll all affected users; it also differs from the key-only settings above. See OpenSSH AuthenticationMethods.
If a new login fails
Keep the existing session open, review the SSH journal, and restore the configuration files you changed. Validate withsshd -t before reloading. If all SSH access is lost, use the VNC console to repair the configuration.