Secure SSH on a VPS: keys, port, no password login

Check every change in a new terminal window without closing the current one. If login breaks, revert the settings from the open session. Lost access completely — open the VPS VNC console in the client area.

1. Key login

On your computer create a key and copy it to the VPS:

ssh-keygen -t ed25519
ssh-copy-id root@203.0.113.10

On Windows: PowerShell, the same ssh-keygen; instead of ssh-copy-id add the line from .ssh/id_ed25519.pub to /root/.ssh/authorized_keys on the VPS. Check: ssh root@203.0.113.10 — no password prompt any more.

2. Disable password login

Only after key login works. On Ubuntu the password is enabled by /etc/ssh/sshd_config.d/50-cloud-init.conf — edit it:

sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config.d/50-cloud-init.conf
sshd -t && systemctl reload ssh
sshd -T | grep -E '^(passwordauthentication|permitrootlogin) '

To let root log in only with a key, add PermitRootLogin prohibit-password to /etc/ssh/sshd_config.d/10-hardening.conf.

3. Another SSH port

Changing the port cuts most automatic password guessing. On Ubuntu 24.04 and newer SSH starts via ssh.socket, so the port is changed in the socket settings, not in sshd_config:

mkdir -p /etc/systemd/system/ssh.socket.d
cat > /etc/systemd/system/ssh.socket.d/override.conf <<EOF
[Socket]
ListenStream=
ListenStream=0.0.0.0:2222
ListenStream=[::]:2222
EOF
systemctl daemon-reload && systemctl restart ssh.socket
ss -ltn | grep 2222

On AlmaLinux and other systems without ssh.socket: Port 2222 in /etc/ssh/sshd_config and systemctl restart sshd.

Open the new port in the panel firewall before switching and connect: ssh -p 2222 root@203.0.113.10.

A firewall rule for the new port
A firewall rule for the new port

You can listen on both ports (22 and 2222) first, check login on the new one, and only then remove 22 from override.conf.

4. fail2ban

In ISPmanager: "Navigation board" → "Network services" → "Fail2ban rules" — check that the sshd rule is on. Put your IP into "Never ban": Firewall and fail2ban.

If access is lost

Client area → VPS → VNC console. Log in as root and revert: delete /etc/systemd/system/ssh.socket.d/override.conf, return PasswordAuthentication yes, run systemctl daemon-reload && systemctl restart ssh.socket ssh.

Questions and answers
On Ubuntu 24.04 and newer SSH is started by ssh.socket. The port is set in /etc/systemd/system/ssh.socket.d/override.conf.
On Ubuntu PasswordAuthentication yes is set in /etc/ssh/sshd_config.d/50-cloud-init.conf, which overrides the main config. Change it there.