Every public server gets scanned and attacked within minutes. These steps block most of it. The commands are for Ubuntu and Debian; run them as root or with sudo.
1. Update the system
apt update && apt upgrade -y
2. Turn on the UFW firewall
Allow SSH first, or you will lock yourself out:
apt install -y ufw
ufw allow OpenSSH
ufw enable
Then open only the ports your services need, for example a website:
ufw allow 80/tcp
ufw allow 443/tcp
or a Minecraft server:
ufw allow 25565/tcp
Check the rules any time:
ufw status verbose
Changed your SSH port? Allow that port (for example
ufw allow 2222/tcp) before enabling the firewall.
3. Block brute-force logins with Fail2ban
Fail2ban watches your logs and bans IPs that keep guessing passwords.
apt install -y fail2ban
systemctl enable --now fail2ban
On Ubuntu and Debian the SSH protection is on by default. See who it has banned:
fail2ban-client status sshd
Unban an IP (for example if you locked yourself out from home):
fail2ban-client set sshd unbanip 1.2.3.4
4. Next step: SSH keys
The strongest protection is logging in with a key instead of a password. See Log in to your VPS with SSH keys.