
Image retained from the original article.
Originally published 25 July 2023; rewritten and reviewed on 15 October 2026.
The most dangerous moment in a routine Linux hardening task is rarely a zero-day exploit. It is the exact second an administrator enables a firewall or disables SSH password sign-in, only to realise the rule or daemon config just cut off their own management route. A setting intended to reduce risk can lock you out of a remote VPS in Malaysia or Singapore just as effectively as an attacker would.
When I provision or inherit a production Ubuntu or Debian box, my first concern is the order of operations, not how many security checklists I can tick in ten minutes.
Verify access and keep a lifeline open
Before touching /etc/ssh/sshd_config or enabling ufw, identify three things: your distribution and version, which user account actually holds sudo privileges, and which port sshd is listening on. Never assume SSH is on port 22 if someone else set up the box before you, and never rely on a generic UFW application profile without checking what port it opens.
Keep two rules in mind before restricting anything:
- Test out-of-band console access first. Make sure you can log in through your hosting provider’s web console (DigitalOcean Droplet Console, Vultr VNC, Linode Lish, or Proxmox noVNC) in case networking breaks.
- Never close your active SSH session while testing. Apply changes in one terminal, leave that session open, and verify that a brand-new SSH connection from a second terminal succeeds before you log out of the first.
Start with a quick read-only inspection so you know what is currently running:
sudo ss -tulpn | grep ssh
sudo ufw status verbose
sudo sshd -t
systemctl --failed
If sshd -t prints nothing and exits cleanly, your current SSH configuration syntax is valid. If you are moving SSH to a custom port or enforcing key-based authentication, place your overrides in a drop-in file such as /etc/ssh/sshd_config.d/99-hardening.conf rather than editing the main distribution file directly. That makes package upgrades cleaner and rollbacks trivial.
# /etc/ssh/sshd_config.d/99-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
Validate the syntax with sudo sshd -t, reload the service (sudo systemctl reload ssh on Ubuntu/Debian, or sshd on RHEL-based distros), and immediately test key-based login in a separate terminal window.
Enable the firewall in the right sequence
Enabling UFW before allowing your SSH port is the classic self-inflicted outage. Always add the allow rule for your exact SSH port—alongside HTTP/HTTPS if this is a web server—before turning the firewall on:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp comment 'SSH management'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
sudo ufw enable
If you changed SSH to a non-standard port such as 2222, allow 2222/tcp instead of relying on ufw allow OpenSSH, because the built-in OpenSSH profile only opens port 22. After running sudo ufw enable, open another SSH session to confirm you can still connect.
Keep applications inside their own permissions
Once management access is safe, look at what is running on the machine. Web applications, Node.js processes, .NET Kestrel services, and background workers should never run as root. Create dedicated system users with no interactive login shell, and restrict write permissions to only the directories the application genuinely needs (for example, an uploads folder or cache path), leaving application binaries and source code read-only to the service account.
Review listening sockets with sudo ss -tulpn. Databases such as PostgreSQL, MySQL/MariaDB, or Redis should bind to 127.0.0.1 or a private VPC interface rather than 0.0.0.0, unless an external host explicitly requires direct access over an encrypted tunnel or private network.
Security patching also needs a repeatable cadence. On Ubuntu and Debian, unattended-upgrades handles routine security patches reliably, while kernel updates that require a reboot should be scheduled during a known maintenance window rather than left pending for months. Keep secrets—API keys, database passwords, TLS private keys—outside your Git repository and readable only by the service user (chmod 600 or 640).
Monitor what matters and rehearse recovery
Hardening does not end once the firewall is green. Failed authentication attempts, sudden disk usage spikes from unrotated logs, and unexpected service restarts are early warning signs. Tools like fail2ban help cut down automated SSH brute-force noise, and checking journalctl -u ssh --since "24 hours ago" or disk usage (df -h) should be part of routine maintenance.
Finally, back up the data and configuration needed to rebuild the server from scratch—database dumps, /etc/nginx, /etc/systemd/system units, and TLS certificates—and test restoring them onto a staging box or temporary VM. A backup you have never restored is just an assumption. Write down what you changed, which ports are open, and where the recovery console lives, so the next person maintaining that server—including future you at 2 a.m.—does not have to guess.