Get started

How to secure SSH on a Linux VPS: key logins, no passwords and fail2ban

Bots try to guess SSH passwords on every new server. Switch to key logins, turn password logins off without locking yourself out, and add fail2ban on Ubuntu or AlmaLinux.

5 December 2026 · 7 min read

Put a new Linux VPS on the internet and its SSH log soon fills with failed logins from machines trying common usernames and passwords. Nothing is wrong with your server; that is simply what the internet does. But if root can log in with a password, a weak or reused password is all that stands between those bots and your server.

The fix is straightforward: log in with an SSH key instead of a password, switch password logins off, and add fail2ban to cut the noise. This guide covers Ubuntu and AlmaLinux, and shows how to do it without locking yourself out.

Before you start: do not lock yourself out

  • Keep your current SSH session open the whole time, and test every change from a second terminal window. If a change goes wrong, you can still undo it from the first session.
  • Know your way back in. Find out whether your provider offers a web console (VNC or serial) that works without SSH, before you need it.
  • Back up the config: cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original

Step 1: create an SSH key on your computer

On your own computer, not the server, open a terminal. macOS and Linux include the OpenSSH client, and so do recent versions of Windows 10 and 11 (use PowerShell). Run:

ssh-keygen -t ed25519

Ubuntu’s documentation recommends the Ed25519 type because the keys are short and fast. Accept the default file name and set a passphrase when asked. The passphrase protects the key if your laptop is ever lost or stolen. This creates two files: ~/.ssh/id_ed25519, your private key, which never leaves your computer, and ~/.ssh/id_ed25519.pub, the public key you put on servers.

Step 2: create a normal user with sudo

Working as a normal user and using sudo for admin tasks is safer than logging in as root. On the server, as root:

# Ubuntu
adduser deploy
usermod -aG sudo deploy

# AlmaLinux
adduser deploy
passwd deploy
usermod -aG wheel deploy

Replace deploy with the username you want. Give it a strong password: you will still need it for sudo, even after SSH password logins are switched off.

Step 3: copy your public key to the server

From your computer:

ssh-copy-id deploy@your-server-ip

This adds your public key to ~/.ssh/authorized_keys for that user. If ssh-copy-id is not available (on Windows, for example), log in as the user and paste the contents of id_ed25519.pub into the file by hand, then fix the permissions:

mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Now, from a second terminal, check that you can log in with the key and use sudo:

ssh deploy@your-server-ip
sudo whoami

If you were not asked for the server password (only your key’s passphrase, if you set one) and sudo whoami prints root, you are ready for the next step. Do not go on until this works.

Step 4: switch off password logins

On both Ubuntu and AlmaLinux 9, the main file /etc/ssh/sshd_config starts with a line that includes every .conf file in /etc/ssh/sshd_config.d/. For most settings, OpenSSH uses the first value it finds, so a setting in one of those files beats the main file, and files are read in name order.

That matters because some VPS images add their own file there. On cloud images, a file such as 50-cloud-init.conf can turn password logins back on. Create your own file with a name that sorts first:

sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

Put this in it:

PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

If you still need root to log in directly, for a backup system for example, use PermitRootLogin prohibit-password instead, which allows root with a key but never with a password.

Check the syntax, then check the values the server will actually use:

sudo sshd -t
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|pubkeyauthentication)'

sshd -t prints nothing if the configuration is valid. The second command should show the four values you set. If one is different, another file in sshd_config.d is overriding it. Then restart SSH:

# Ubuntu
sudo systemctl restart ssh

# AlmaLinux
sudo systemctl restart sshd

Test again from a new terminal. A password login attempt should now be refused with Permission denied (publickey), and your key login should still work.

Step 5: add fail2ban

With passwords off, guessing attacks cannot succeed, but they still fill your logs and use a little CPU. Fail2ban watches the SSH log and blocks addresses that fail repeatedly. Install it:

# Ubuntu
sudo apt update
sudo apt install fail2ban

# AlmaLinux (fail2ban comes from EPEL)
sudo dnf install epel-release
sudo dnf install fail2ban

Never edit jail.conf, because updates overwrite it. Create /etc/fail2ban/jail.local instead:

[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h

That bans an address for an hour after 5 failures within 10 minutes. If fail2ban later reports that it cannot find the SSH log file, the system is logging only to the systemd journal; add backend = systemd under [sshd]. Start it and check the jail:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Step 6: make sure the firewall allows SSH

If you turn on a firewall, allow SSH first, or the firewall will cut off the session you are using.

# Ubuntu (ufw)
sudo ufw allow OpenSSH
sudo ufw enable

# AlmaLinux (firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

If only you and your team ever log in, allowing SSH from your own fixed IP addresses only is even better.

Should you change the SSH port?

Moving SSH off port 22 reduces log noise, because most bots only try the default port. It is not a security control in itself, and it adds steps: the firewall must allow the new port before you restart, and on AlmaLinux SELinux must be told about it (semanage port -a -t ssh_port_t -p tcp 2222). On Ubuntu 24.04 and later, SSH is started through systemd socket activation, so after changing Port you need sudo systemctl daemon-reload and sudo systemctl restart ssh.socket for the new port to take effect. If you have done steps 1 to 5, you can safely leave the port alone.

On a cPanel server

The same rules apply, but use WHM where it has a tool for the job. Security Center » Manage root’s SSH Keys adds keys for root, SSH Password Authorization Tweak switches off password logins, and cPHulk Brute Force Protection blocks repeated failed logins across SSH, cPanel, WHM and email. Add and test your key before you disable passwords, exactly as above.

Frequently asked questions

Is it safe to disable SSH password authentication?

Yes, once you have tested a key login from a second terminal. Keep a key backup and know how to reach your provider’s web console in case you lose the key.

Should I disable root login over SSH?

Ideally, yes: log in as a normal user and use sudo. If something needs root over SSH, set PermitRootLogin prohibit-password so root can only use a key.

Why does password login still work after I set PasswordAuthentication no?

Another file in /etc/ssh/sshd_config.d/ probably sets it to yes first, since OpenSSH uses the first value it reads. Run sshd -T to see the value in effect, and name your file so it sorts first.

Which SSH key type should I use?

Ed25519, created with ssh-keygen -t ed25519. Use RSA with 4096 bits only for old systems that do not support Ed25519.

Do I still need fail2ban with key-only login?

It is not essential, because password guesses cannot succeed, but it reduces log noise and blocks scanners early.

A VPS you control, with help when you want it

Our VPS hosting gives you full root access to set SSH up your way. If you would rather not do this yourself, our Linux server management service hardens SSH and the firewall, keeps the server patched and watches the logs for you. Upgrading the operating system first? See our guide to upgrading Ubuntu 24.04 to 26.04.

Tags:

secure SSH Linux VPS

disable SSH password authentication

SSH key ed25519

fail2ban sshd

VPS security

VPS Guide

How to upgrade an Ubuntu 24.04 VPS to Ubuntu 26.04 LTS

Upgrades from Ubuntu 24.04 LTS to 26.04.1 LTS opened on 29 September 2026. Back up, update fully, run do-release-upgrade, and check what can break.

Read More →

VPS Guide

How to set up n8n with Docker on a VPS

Run n8n in Docker on your own VPS so it restarts on its own, keeps its data across updates and sits behind HTTPS.

Read More →