A new VPS or dedicated server gets scanned and hit with SSH password guessing within minutes of going online. So the first steps are not installing the site but basic protection. Here is the order for Ubuntu, Debian, AlmaLinux and Rocky Linux; it takes about half an hour.
1. Update the system
sudo apt update && sudo apt full-upgrade -y # Debian, Ubuntu sudo dnf upgrade -y # AlmaLinux, Rocky
If the kernel was updated, reboot: sudo reboot.
2. Your own user with sudo
Working as root all the time is dangerous: any mistake in a command runs with full rights. Create a user and give it sudo:
sudo adduser ivan sudo usermod -aG sudo ivan # Debian, Ubuntu sudo usermod -aG wheel ivan # AlmaLinux, Rocky
If someone needs only certain commands rather than full root, build the rules in the sudoers generator — it warns about commands that give root in a roundabout way.
3. Key-only login
On your own computer, create an Ed25519 key and copy it to the server:
ssh-keygen -t ed25519 ssh-copy-id ivan@server-address
You can also create the key right in the browser on the SSH keys page. Test key login in a new terminal window, and only then turn off passwords and root login. Create /etc/ssh/sshd_config.d/10-hardening.conf:
PermitRootLogin no PasswordAuthentication no KbdInteractiveAuthentication no MaxAuthTries 3
Check the syntax and reload the service: sudo sshd -t && sudo systemctl reload ssh (sshd on RHEL). A full set of safe settings, including modern algorithms, comes from SSH hardening, and details are in the article on securing SSH.
4. Firewall
Open only what the server is for. With ufw:
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow OpenSSH sudo ufw allow 80,443/tcp sudo ufw enable
If SSH is used only from known addresses, allow it only from them. Rules for ufw, nftables or firewalld, with IPv6 and a safeguard against losing access, are built by the firewall rules generator; what exactly to close is in the article on ports.
5. Brute-force protection
Even with key login, the logs will be full of guessing attempts. fail2ban blocks addresses after several failed attempts:
sudo apt install fail2ban
A ready jail.local for SSH, nginx and mail with blocking through nftables or ufw is built by fail2ban config. To see who has already tried to get in, paste /var/log/auth.log into log analysis.
6. Automatic security updates
Vulnerabilities are found all the time, and a server updated "when someone remembers" will sooner or later be broken into. Turn on automatic installation of security updates: unattended-upgrades on Debian and Ubuntu, dnf-automatic on RHEL-like systems. Settings with a night-time reboot only when needed come from automatic updates.
7. Time, name and small things
- Time zone and time sync:
sudo timedatectl set-timezone Europe/London, plussystemd-timesyncdorchrony. - Server name:
sudo hostnamectl set-hostname web1— handy in logs and alerts. - Swap on a server with little memory, so the OOM killer does not take it down.
- Recurring jobs — via cron or systemd timers; the syntax is covered in the article on cron.
All at once in one script
A user with a key and sudo, no root or passwords, a firewall, fail2ban, automatic updates, kernel settings and swap in one Bash or cloud-init script are built by first server setup. When the server is ready, check the result: paste the sshd -T output and the nginx configuration into the sshd and nginx audit.