Skip to content

Essential Server Hardening Guide: Securing a Public Linux Node

Technical blueprint graphic of a Linux server node with glowing cyan circuit pathways and a central security shield and padlock emblem.

When spinning up a new Virtual Private Server (VPS) or a public-facing cloud instance, it goes live on the internet within milliseconds. Automated bots instantly begin scanning for vulnerabilities, default credentials, and open ports.

This practical guide outlines the core security practices required to harden a fresh Debian or Ubuntu server.


Update the System Packages

Before doing anything else, ensure your package lists are up to date and your existing system binaries are fully patched against known flaws.

sudo apt update && sudo apt full-upgrade -y

Install Essential Administrative Tools

Install common utilities required for network inspection, firewall management, and general system administration.

sudo apt install sudo curl wget ufw net-tools -y

Create a Non-Root Administrative User

Operating primarily out of the root account is a quick way to break a system by accident or give an attacker absolute control if something goes wrong. Set up a dedicated user account with administrative privileges right away, and always require a password for sudo to maintain a vital second layer of defense.

Create a new user and add them to the sudo group:

adduser your_username
sudo usermod -aG sudo your_username

Log in as your new user and verify that sudo apt update prompts you for your password. Never enable passwordless sudo (NOPASSWD) on a server exposed to the internet.


Lock Down Secure Shell (SSH) Access and Disable Root Login

Leaving Secure Shell exposed on port 22 with password authentication enabled is the digital equivalent of leaving your car keys in the ignition in a dodgy multi-storey car park. Automated scanners hammer SSH ports around the clock.

Seamlessly push your local SSH public key to your new remote user from your local machine:

ssh-keygen -t ed25519 -a 100
ssh-copy-id your_username@your_server_ip

Log into your server as your new user and open the SSH daemon configuration file (sudo nano /etc/ssh/sshd_config) to enforce key-only auth:

PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin no

Test the syntax (sudo sshd -t) and restart the SSH service (sudo systemctl restart ssh).

Lock the root account password entirely to prevent direct root logins at the system level:

sudo passwd -l root

Configure and Enable the Firewall (UFW)

Before doing anything else that exposes your services to the web, establish a strict firewall policy to shrink your attack surface.

CRITICAL WARNING: Always allow your SSH connection before enabling the firewall’s default-deny policy. Failing to do so will immediately lock you out of the server.

Allow SSH traffic explicitly first (adjust the port if using a non-standard port):

sudo ufw allow 22/tcp

Set default policies to block incoming traffic and allow outgoing traffic:

sudo ufw default deny incoming
sudo ufw default allow outgoing

Open necessary public web ports (such as 80 and 443):

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Enable and verify the firewall:

sudo ufw enable
sudo ufw status verbose

Configure Time Zone and Network Time Synchronisation

Accurate system time is critical. It underpins log auditing, TLS certificate validation, and intrusion detection tools.

Set your appropriate local time zone (e.g., London):

sudo timedatectl set-timezone Europe/London

Ensure network time synchronisation is active:

sudo timedatectl set-ntp true

Install and Configure Fail2Ban

To mitigate automated brute-force attacks and keep system logs clean of endless failed login chatter, install an intrusion prevention utility.

Install Fail2Ban via your package manager:

sudo apt install fail2ban -y

Create a local configuration override file and enable the SSH jail to automatically lock out persistent attackers after a handful of failed attempts:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo systemctl enable --now fail2ban

Secure Container Environments (Docker)

If your server runs containerized applications via Docker, keep a close eye on how your containers bind to network interfaces. By default, some containers love to bind across all interfaces (0.0.0.0), exposing internal databases or management dashboards directly to the wider internet.

Install Docker cleanly via the official script:

curl -fsSL https://get.docker.com | sudo sh

Add your user to the docker group so you don’t need sudo for every container command:

sudo usermod -aG docker your_username

Always ensure sensitive services (like PostgreSQL or Redis) bind exclusively to localhost in your compose files:

ports:
  - "127.0.0.1:5432:5432"

Route public-facing applications through a dedicated reverse proxy (like Caddy or Nginx Proxy Manager) to handle SSL/TLS termination and secure ingress traffic properly.


Automate Maintenance with a System Update Script

Instead of manually running updates and forgetting to check for pending service restarts, encapsulate your routine maintenance into a clean automated shell script using needrestart.

cat << 'EOF' > update.sh
#!/bin/bash

#Exit immediately if a command exits with a non-zero status
set -e

echo "Starting system update..."
sudo apt update

echo "Upgrading packages..."
sudo apt full-upgrade -y

echo "Removing orphaned packages..."
sudo apt autoremove -y

echo "Checking for services that need a restart..."
if ! command -v needrestart &> /dev/null; then
echo "Installing needrestart..."
sudo apt install needrestart -y
fi

sudo needrestart

echo "System maintenance complete!"
EOF

chmod +x update.sh

To run future updates, you then only need a single command:

./update.sh

Join the conversation