Harden SSH on your VPS
A hardened server accepts only keys, only for named users, and never for root; with that in place the constant password guessing on port 22 simply fails. The changes below take about fifteen minutes, and each one is tested before the next so you keep access the whole time.
Why is this worth doing on day one?
Every public IPv4 address receives login attempts within hours, from scripts that try common user names and leaked passwords. You can see them for yourself after a day: journalctl -u ssh --since today | grep -c "Failed password" on Ubuntu and Debian, or journalctl -u sshd on Rocky and Alma. Hundreds of lines is normal.
Key-only logins make those attempts pointless, because there is no password to guess. The other steps reduce noise, shrink the number of accounts an attacker could target and keep the system patched. None of it costs anything, and it runs the same on a P1 as on a D192.
How do you create a normal user with sudo?
Work as a named user and use sudo for admin tasks. On Ubuntu and Debian:
adduser --gecos "" deploy
usermod -aG sudo deploy
install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
install -m 600 -o deploy -g deploy /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keysOn Rocky Linux, AlmaLinux, CentOS Stream and Fedora the admin group is called wheel:
useradd -m -G wheel deploy
passwd deploy
install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
install -m 600 -o deploy -g deploy /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys
restorecon -R /home/deploy/.sshThe install commands create the folder and copy your key with the right owner and permissions in one step; SSH ignores key files that other users can write to. restorecon resets the SELinux labels on Red Hat style systems so sshd may read the file.
Test now: open a new terminal and run ssh deploy@203.0.113.10, then sudo whoami. It must print root. Do not continue until this works.
Which sshd settings should you change?
Put your settings in a drop-in file rather than editing the main config, so package updates never overwrite them. Ubuntu 22.04 and later, Debian 12 and 13, and Rocky and Alma 9 and 10 all read /etc/ssh/sshd_config.d/*.conf before the main file. Create /etc/ssh/sshd_config.d/10-hardening.conf:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
AllowUsers deploy
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding noWhat each line does: no root logins at all, no passwords, no keyboard-interactive prompts, keys only, and only the user deploy may log in. MaxAuthTries 3 closes a connection after three failed keys, LoginGraceTime 20 drops clients that do not finish logging in within 20 seconds, and X11 forwarding is off because a server does not need a desktop.
Some images also ship a file such as 50-cloud-init.conf in the same folder with PasswordAuthentication yes. The first value sshd reads wins, and files are read in name order, so your 10- prefix takes priority. Check for conflicts with grep -r PasswordAuthentication /etc/ssh/sshd_config.d/.
How do you apply the change without locking yourself out?
Test the syntax, check the effective values, reload, then prove a fresh login still works while the old session stays open.
sshd -t
sshd -T | grep -Ei "permitrootlogin|passwordauthentication|allowusers"
systemctl reload ssh # Ubuntu and Debian
systemctl reload sshd # Rocky, Alma, CentOS Stream, Fedorasshd -t prints nothing when the file is valid. sshd -T shows the final settings after every file is merged, which is the only reliable way to see what is really active. A reload keeps current sessions alive.
Now, from a second terminal, try ssh deploy@203.0.113.10 (must work) and ssh root@203.0.113.10 (must be refused). Only then close the first session. If something went wrong and you are fully locked out, the servers have no browser console: a rebuild from the console restores access but wipes the disk, so the second-session habit is the real safety net.
Should you move SSH to another port?
Moving SSH off port 22 does not add real security once passwords are off, but it cuts the log noise to almost nothing. If you do it, three things must change together: the SSH listener, the firewall and, on Red Hat style systems, SELinux.
On Ubuntu 24.04, SSH is started by a systemd socket, so the port lives in the socket unit, not only in sshd_config:
mkdir -p /etc/systemd/system/ssh.socket.d
printf "[Socket]\nListenStream=\nListenStream=2222\n" > /etc/systemd/system/ssh.socket.d/port.conf
ufw allow 2222/tcp
systemctl daemon-reload && systemctl restart ssh.socketOn Debian, Rocky and Alma, add Port 2222 to your drop-in file instead. On Rocky and Alma, also tell SELinux and firewalld before reloading:
dnf install -y policycoreutils-python-utils
semanage port -a -t ssh_port_t -p tcp 2222
firewall-cmd --permanent --add-port=2222/tcp && firewall-cmd --reload
systemctl restart sshdCheck with ss -tlnp | grep 2222, connect with ssh -p 2222 deploy@203.0.113.10, and only after that remove the old port 22 rule.
How does Fail2ban help?
Fail2ban reads the login log and bans an IP in the firewall after repeated failures. With keys only it mostly keeps logs short, and it protects other services you add later, such as a web login form. Install it with apt install -y fail2ban, or on Rocky and Alma with dnf install -y epel-release && dnf install -y fail2ban. Then create /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
[sshd]
enabled = true
port = ssh,2222Start it with systemctl enable --now fail2ban and check it with fail2ban-client status sshd, which lists the number of failures seen and the IPs currently banned. If you ever ban your own address, fail2ban-client set sshd unbanip 198.51.100.7 lifts it from another connection.
How do you keep security updates coming automatically?
Unpatched packages are a bigger risk than SSH settings, so let the system install security fixes on its own. On Ubuntu and Debian:
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgradesThe second command writes /etc/apt/apt.conf.d/20auto-upgrades. Confirm it works with unattended-upgrade --dry-run --debug. On Rocky, Alma and Fedora use dnf-automatic:
dnf install -y dnf-automatic
sed -i "s/^upgrade_type.*/upgrade_type = security/; s/^apply_updates.*/apply_updates = yes/" /etc/dnf/automatic.conf
systemctl enable --now dnf-automatic.timerKernel updates need a reboot to take effect. Check for a pending one with ls /var/run/reboot-required on Ubuntu or needs-restarting -r on Rocky, and reboot at a quiet hour with sudo reboot or the reboot button on the server page.
How do you check the result a week later?
A short review tells you the setup holds. last -n 20 lists recent successful logins and the IP each came from; every line should be you. journalctl -u ssh --since "7 days ago" | grep Accepted shows which key type was used. fail2ban-client status sshd shows the ban count, and sshd -T | grep -i passwordauthentication should still say no after all updates. If you add a teammate, create a separate user, add that name to AllowUsers and reload, rather than sharing one key.
Questions about this guide
Is it safe to disable root login completely?
Yes, as long as your sudo user works first. Test sudo whoami in a fresh session before you reload sshd, and keep the old session open until the new login is proven.
What if I lose my only private key?
There is no browser console, so without a working key you cannot log in. Keep a second key on another device in the console from the start. As a last resort, a rebuild with a new key restores access but wipes the disk.
Does a root password reset undo these settings?
No. Reset sets a new root password, but with PasswordAuthentication no and PermitRootLogin no that password is not accepted over SSH. It remains useful for local tools and sudo setups that ask for it.
Do I need Fail2ban if passwords are off?
Not for security of SSH itself, since there is nothing to guess. It is still handy to keep logs readable and to protect web logins or mail services you add later.
Is ed25519 better than RSA for this?
For a server you control, yes: shorter keys, faster checks and strong security. Use RSA 4096 only if an old client cannot handle ed25519.