The server is up, and the email holds a root login and a password. From that moment it is visible to the whole internet: the first attempts to guess the SSH password arrive within the hour, and they arrive not because somebody heard about your site, but because hosting address ranges are swept in order, around the clock.
Below are six steps that cover the essentials. Each takes a couple of minutes, and each comes with a check: a command that shows the step actually worked rather than merely ran without errors. That is the difference between "set up" and "thought it was set up". The whole sequence assumes a fresh Debian or Ubuntu.
Step 1. Update the system
The image the server was built from was assembled months ago. Everything out of date in it is already described in public vulnerability lists:
sudo apt update && sudo apt upgrade -y The check: running apt list --upgradable again should print nothing but the Listing... line. If the file /var/run/reboot-required has appeared, the kernel was updated and a reboot is needed:
ls -l /var/run/reboot-required sudo reboot Reboot at a moment when console access in the hosting panel is within reach, in case the machine does not come back up on its own.
Step 2. Create your own user
Working as root is awkward and dangerous: any typo runs without being questioned. What you need is an ordinary user able to raise privileges when required:
sudo adduser alex sudo usermod -aG sudo alex The check: log in as that user and confirm sudo works. Do this before root login is closed off:
su - alex sudo -v The command will ask for a password and finish silently — that is success. The answer "is not in the sudoers file" means the group has not taken effect: leave the session and log in again.
Step 3. Log in with a key, not a password
A password can be guessed, a key cannot. On your own computer, not on the server:
ssh-keygen -t ed25519 ssh-copy-id alex@203.0.113.10 Key-based login should now work without a password. Once you are sure of that, password login can be turned off in /etc/ssh/sshd_config:
PasswordAuthentication no PermitRootLogin no The check, and here it matters most of all. Do not close your current connection. First make sure there is no mistake in the configuration, then restart the service:
sudo sshd -t sudo systemctl reload ssh Then, leaving the old window open, open a second one and log in again. If the login goes through, all is well. If it does not, you still have a working session in which the setting can be put back. A single session closed with a typo in the configuration means a trip to the hosting console.
One separate subtlety: files in /etc/ssh/sshd_config.d/ are included in alphabetical order, and the value that takes effect is the one encountered first. Images from cloud providers often carry a 50-cloud-init.conf there that allows passwords. That is why your own file is given a lower number, say 10-hardening.conf, or the edit achieves nothing.
Step 4. Close everything else with a firewall
On a fresh system usually only SSH faces outward, but install a web server or a database and ports open by themselves. The rule is simple: everything is forbidden except what is allowed explicitly.
sudo ufw allow OpenSSH sudo ufw enable In that order. Enable the firewall without allowing SSH and the connection drops on the next command.
The check: sudo ufw status numbered shows the list of rules and Status: active. It is also worth looking at what is listening on the network at all:
sudo ss -tulpn | grep LISTEN The address 127.0.0.1 in the output means the service is available only to the server itself, and that is fine. What should give you pause is 0.0.0.0 next to something you never opened outward.
Step 5. Install fail2ban
Password guessing will not stop because passwords are off — it will simply stop being dangerous. But the log entries remain, and with them the load. Fail2ban bans an address after a few failed attempts:
sudo apt install fail2ban In the file /etc/fail2ban/jail.local:
[sshd] enabled = true maxretry = 5 bantime = 1h The check:
sudo systemctl restart fail2ban sudo fail2ban-client status sshd The output should hold the lines Currently failed and Total banned. An error about an unavailable jail instead means it did not come up.
A common case on Debian 12 and newer: the jail is running, but the counters stay at zero although failed logins are visible in the journal. The reason is that the system file /var/log/auth.log may not exist at all — the entries go to the systemd journal. One line in the same section fixes it:
backend = systemd Step 6. Turn on automatic updates
The update from step one will be out of date in a week. So that you do not have to come back to it by hand:
sudo apt install unattended-upgrades sudo dpkg-reconfigure -plow unattended-upgrades The check: a trial run shows exactly what would be installed, changing nothing:
sudo unattended-upgrade --dry-run -d A day later the result appears in /var/log/unattended-upgrades/unattended-upgrades.log. By default only security updates are installed, and that is a sensible setting: it closes holes without touching the versions of the programs your site depends on.
What you end up with
An hour of work, and the server is no longer an easy target: passwords are not guessable, spare ports are closed, persistent addresses are cut off, patches install themselves. That is enough to start deploying on it whatever it was bought for.
What remains is the question those six steps do not answer: how do you know all of it still works. Keys can be quietly reverted to passwords by the next edit of the configuration, a jail can fall over after an update, automatic updates can stop silently because of a held apt lock. Nobody is going to run six commands by hand every week, and a month later the state of the server is unknown again.
That is what a panel is for: it shows the same checks on one screen and marks the moment one of them stops passing. Arcivéo has a free edition that installs with one command, switches on UFW and fail2ban, and from then on shows the state. What the same data looks like on the full panel is on the open demo, no sign-up needed.
Originally published at arciveo.com.