Firewall Configuration & Security Hardening
Configure UFW / nftables, deploy Fail2ban with systemd journal backend for Debian 12/13 & Ubuntu 24.04/26.04, and establish a production security baseline.
Any server connected to the public IPv4 space encounters automated port scanning and credential brute-forcing within minutes of deployment. Implementing proactive firewall boundaries and intrusion prevention is critical for long-term operational health.
1. Managing Ports with UFW (Uncomplicated Firewall)
UFW is the cleanest front-end for managing nftables / iptables rules on Ubuntu and Debian distributions.
1.1 Install and Define Default Policies
# Install UFW on Ubuntu / Debian
sudo apt update && sudo apt install -y ufw
# Deny all incoming connections by default; allow all outgoing traffic
sudo ufw default deny incoming
sudo ufw default allow outgoing
1.2 Open Essential Management & Web Ports
[!IMPORTANT] Always allow your SSH port (default 22) before enabling UFW, otherwise your active remote session will be severed immediately!
# Allow SSH port
sudo ufw allow 22/tcp comment 'SSH Port'
# Allow standard HTTP/HTTPS web traffic (TCP & QUIC UDP)
sudo ufw allow 80/tcp comment 'Web HTTP'
sudo ufw allow 443/tcp comment 'Web HTTPS'
sudo ufw allow 443/udp comment 'Web HTTP/3 QUIC'
# Enable the firewall
sudo ufw enable
1.3 Inspecting and Deleting Rules
# List active rules with index numbers
sudo ufw status numbered
# Delete a specific rule by its index number (e.g., rule #3)
sudo ufw delete 3
2. Automated Brute-Force Mitigation with Fail2ban
Fail2ban monitors authentication logs in real time and dynamically bans IP addresses that exhibit repeated login failures.
2.1 Installing Fail2ban
# Ubuntu / Debian (includes python3-systemd for journald support)
sudo apt update && sudo apt install -y fail2ban python3-systemd
# AlmaLinux / Rocky Linux 9 & 10
sudo dnf install -y epel-release && sudo dnf install -y fail2ban
2.2 Creating a Modern jail.local Configuration
[!NOTE] Starting with Debian 12/13 and Ubuntu 24.04/26.04 LTS, traditional
rsyslog(/var/log/auth.log) is no longer pre-installed by default. You must specifybackend = systemdso Fail2ban queries thesystemd-journaldlog stream directly.
Never edit /etc/fail2ban/jail.conf directly (it is overwritten during package upgrades). Instead, create /etc/fail2ban/jail.local:
cat << 'EOF' | sudo tee /etc/fail2ban/jail.local
[DEFAULT]
backend = systemd
bantime = 1d
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 4w
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
mode = aggressive
port = ssh
filter = sshd
maxretry = 3
bantime = 3d
EOF
Restart and enable Fail2ban at boot:
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
2.3 Common Fail2ban Administration Commands
# View active sshd jail statistics and currently banned IPs
sudo fail2ban-client status sshd
# Manually unban a specific IP address
sudo fail2ban-client set sshd unbanip 198.51.100.25
3. Production Security Baseline Checklist
- Create a Non-Root Administrative User:
sudo adduser deployer sudo usermod -aG sudo deployer # Ubuntu / Debian # Or: sudo usermod -aG wheel deployer (AlmaLinux / Rocky Linux) - Disable Password Authentication in OpenSSH: Enforce Ed25519 SSH key authentication (see Connecting via SSH & Key Authentication);
- Enable Unattended Security Updates:
sudo apt install -y unattended-upgrades sudo dpkg-reconfigure -plow unattended-upgrades - Audit Open Listening Ports Regularly:
sudo ss -tulnp