Hardening a fresh server in the first hour
A public address is scanned within minutes of going live. These steps close the obvious doors before you install anything else.
Why harden a server straight away?
Automated scanners sweep the whole IPv4 space continuously, so a new address sees login attempts on port 22 almost as soon as it answers. Our edge DDoS filtering absorbs floods, but it cannot tell a legitimate login from a guessed one. That part is yours: with full root on bare metal, the operating system, its services and its users are under your control. The good news is that a handful of settings stop nearly all opportunistic attacks.
Step one: log in with keys, not passwords
Add your public key under SSH keys in the console before ordering, and pick it in the order: the installer places it in root's authorized_keys and no password is sent. If you skipped that, copy a key now with ssh-copy-id. Then create a normal user with sudo rights and disable password logins and direct root login:
adduser ops && usermod -aG sudo ops install -d -m 700 -o ops -g ops /home/ops/.ssh install -m 600 -o ops -g ops /root/.ssh/authorized_keys /home/ops/.ssh/ printf 'PasswordAuthentication no\nKbdInteractiveAuthentication no\nPermitRootLogin no\n' > /etc/ssh/sshd_config.d/50-keys-only.conf sshd -t && systemctl reload ssh
Keep your current session open and test a new login as the new user before you close it. On AlmaLinux and Rocky Linux the group is wheel and the service is sshd.
Step two: a default deny firewall
Open the ports your services answer on and nothing else. On Ubuntu and Debian, ufw is the simplest front end; on the Red Hat family, firewalld comes installed.
ufw allow 22/tcp ufw allow 80,443/tcp ufw --force enable # ufw denies inbound and allows outbound by default # Red Hat family firewall-cmd --permanent --add-port=443/tcp --add-port=80/tcp firewall-cmd --reload
Both tools cover IPv4 and IPv6 when IPv6 is enabled in their configuration. Check with ufw status verbose or firewall-cmd --list-all.
Step three: install security updates automatically
Most compromises use known holes that already have patches. On Debian and Ubuntu install unattended-upgrades and enable it with dpkg-reconfigure -plow unattended-upgrades; on the Red Hat family install dnf-automatic and enable dnf-automatic-install.timer. Kernel updates still need a reboot, which takes a minute or two on our machines and can be triggered from the console if the system stops answering.
Step four: slow down guessing
Even with passwords off, log noise from failed attempts hides real events. fail2ban reads the logs and blocks addresses that fail repeatedly:
apt install fail2ban # /etc/fail2ban/jail.local [sshd] enabled = true maxretry = 5 bantime = 1h systemctl enable --now fail2ban
Moving SSH to another port cuts the noise further but is not a defence on its own. Restricting port 22 to your office or VPN addresses in the firewall is stronger, and the Company VPN gateways solution describes a private entry point.
Step five: know what is listening
Run ss -tulpn and read every line. Databases, caches and admin panels should listen on 127.0.0.1 or a private interface, not on the public address. A database left open to the internet is one of the most common ways data leaks, and it is easy to avoid by changing one bind address.
Step six: protect the data, not only the machine
The drives in most of our models are mirrored in software RAID, which keeps the server running when one disk fails. A mirror is not a copy you can go back to: deleting a file deletes it on both disks. Keep regular copies off the machine, for example on a second server in another city with restic or borg, and test a restore. Our Backup and archive storage page shows machines sized for that job.
How do I keep an eye on the machine afterwards?
Hardening is not a one-off task. Read the authentication log now and then with journalctl -u ssh --since today to see what is knocking, and let a small monitoring agent alert you on full disks, failed services and a degraded RAID array; cat /proc/mdstat shows the mirror state in one line. Review user accounts and the keys in every authorized_keys file each quarter, and remove people who no longer need access. When you install a new service, repeat the listening check and add only the firewall rule it needs. A short written list of these routines takes ten minutes a month and catches most problems while they are still small.
What else is worth doing?
Set the time zone and NTP so logs line up, set the reverse DNS of the address if the machine sends mail, turn on two factor authentication for any web admin panel you install, and remove packages you do not use. Write down how you recover: rescue mode in the console boots a network system that can mount your disks if the installed system ever refuses to start.
Related pages
IPv6 and reverse DNS on your server
Configuring the IPv6 block that comes with every machine and setting the reverse DNS name of its IPv4 address from the console.
Read moreDDoS protection on bare metal
How network edge filtering stops volumetric attacks before they reach your port, and what you still handle yourself.
Read moreIPMI and rescue mode
What out-of-band management is, and how console power control, reinstall and rescue boot cover the same needs on our machines.
Read moreDebian
Debian 13 or 12: a lean, conservative base that stays out of the way on machines you want to forget about.
Read moreQuestions and answers
Can I receive a server with only SSH key access?
Yes. Add your public key in the console and pick it when ordering; the installer places it and no root password is sent.
Does DDoS protection replace a firewall?
No. Edge filtering absorbs floods of traffic. A host firewall decides which services answer at all, so you need both.
Is software RAID a backup?
No. A mirror survives a failed disk, but deleted or encrypted files are gone on both disks at once. Keep copies on another machine.
What if I lock myself out?
Boot rescue mode from the console, mount the system disk and fix the SSH or firewall configuration, then boot from disk again.
Which ports should be open on a web server?
Usually 80 and 443 for the web and 22 for SSH, ideally limited to your own addresses. Everything else stays closed.