,

Harden Ubuntu 24.04 & Docker: Origin Isolation & Zero-Trust SSH

Enterprise Linux server blade rack chassis with physical lock and optical status indicators

Spinning up a modern Linux cloud server with Docker is deceptively simple: install Docker Compose, bind a few ports, put a reverse proxy behind Cloudflare, and launch your applications. But under the hood of production Linux distributions, most notably Ubuntu 24.04 LTS (Noble Numbat), lies a dangerous architectural trap: by default, Docker directly manipulates iptables and completely bypasses standard UFW (Uncomplicated Firewall) rules, exposing container ports directly to the public internet.

Furthermore, thousands of servers hiding behind Cloudflare suffer from Direct Origin IP Bypass, allowing malicious actors who discover your VPS IP to launch unmitigated Layer 7 HTTP floods directly at your web server, rendering your Cloudflare WAF and rate-limiting shields useless. In this operational hardening guide, we dismantle these critical vulnerabilities: mastering the DOCKER-USER iptables chain, implementing Cloudflare origin isolation, configuring emergency swap buffers against Out-Of-Memory (OOM) kernel crashes, and locking down SSH access.

Tested Environment: Hardware Verification Baseline
Operating System
Ubuntu 24.04 LTS (Noble Numbat)
Docker Engine
v27.3.1 (API 1.47)
Docker Compose
v2.29.7
Cloudflare Tunnel
cloudflared v2024.10.0
Last Verified
November 2026

Key Takeaways

  • Docker Firewall Bypass Neutralized: Docker’s default PREROUTING NAT manipulation circumvents standard host UFW rules; restricting bindings to 127.0.0.1 or the DOCKER-USER iptables chain restores perimeter defense.
  • True Zero-Open-Port Origin: Deploying outbound Cloudflare Tunnels (cloudflared) eliminates public HTTP/HTTPS ports (80/443), making origin IP scanning and direct Layer 7 bypass attacks impossible.
  • Zero-Trust SSH & Swap Resilience: Authenticating SSH via Cloudflare Access and tuning zRAM with conservative swappiness (vm.swappiness=10) protects against brute-force attacks and OOM kernel panics.

The Silent Vulnerability: Why Docker Bypasses UFW

The standard firewall frontend on Ubuntu is UFW. Most system administrators run ufw default deny incoming, followed by selectively allowing SSH, HTTP, and HTTPS. However, when you launch a Docker container with port mappings (such as -p 8080:8080 or ports: ["80:80"]), the Docker daemon inserts packet filtering rules into the Linux kernel’s PREROUTING and FORWARD chains.

Because the Linux kernel evaluates the PREROUTING and FORWARD chains before UFW’s standard INPUT filter rules, traffic destined for Docker containers is forwarded immediately without ever consulting UFW. Even if UFW indicates that port 8080 is blocked, an external attacker can connect directly to your container.

The Engineering Solution: The DOCKER-USER Chain

As documented in the official Docker Network Packet Filtering documentation, Docker reserves a dedicated custom chain named DOCKER-USER. Any iptables rules appended to DOCKER-USER execute before Docker’s internal routing table, giving administrators full deterministic control over container ingress.

Eliminating Cloudflare Origin IP Bypass

If your website relies on Cloudflare for DDoS mitigation, WAF rule enforcement, and bot protection, your web server must refuse connections from any IP address that is not an official Cloudflare edge node. If an attacker learns your origin IP (via historical DNS records, certificate transparency logs, or outbound transactional email headers), they can direct a flood directly at your server IP.

Below is a production-grade shell script to programmatically retrieve official Cloudflare IPv4 blocks and enforce strict ingress rules inside the DOCKER-USER chain:

#!/usr/bin/env bash
# /usr/local/bin/harden-cloudflare-docker.sh
set -euo pipefail
echo "==> Configuring DOCKER-USER chain for Cloudflare Origin Isolation..."
# 1. Flush existing DOCKER-USER rules
iptables -F DOCKER-USER
# 2. Allow established and related connections (stateful return traffic)
iptables -A DOCKER-USER -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# 3. Allow loopback and local container bridge communications
iptables -A DOCKER-USER -i lo -j ACCEPT
iptables -A DOCKER-USER -s 127.0.0.1/32 -j ACCEPT
iptables -A DOCKER-USER -s 10.0.0.0/8 -j ACCEPT
iptables -A DOCKER-USER -s 172.16.0.0/12 -j ACCEPT
# 4. Fetch official Cloudflare IPv4 ranges and allow on web ports (80 & 443)
echo "==> Fetching Cloudflare IP ranges..."
for ip in $(curl -fsSL https://www.cloudflare.com/ips-v4); do
    iptables -A DOCKER-USER -p tcp -m multiport --dports 80,443 -s "$ip" -j ACCEPT
done
# 5. Drop all unauthorized external attempts to reach web containers directly
iptables -A DOCKER-USER -p tcp -m multiport --dports 80,443 -j DROP
# 6. Return all other non-web container traffic to Docker's standard routing
iptables -A DOCKER-USER -j RETURN
echo "==> DOCKER-USER firewall successfully hardened!"

Make the script executable and configure it to run on boot:

sudo chmod +x /usr/local/bin/harden-cloudflare-docker.sh
sudo /usr/local/bin/harden-cloudflare-docker.sh

Memory Architecture: Preventing Out-Of-Memory (OOM) Kernel Panics

A frequent failure mode on production VPS instances and homelabs running database workloads (MariaDB/PostgreSQL) alongside memory-hungry containers (like our Vaultwarden and local AI reasoning stacks) is sudden process termination. When physical RAM is exhausted and the system has 0 bytes of swap space, the Linux kernel triggers the OOM Killer, killing your database or web server instantly.

Configure an 8 GiB swap file with conservative swappiness tuning so the OS keeps applications running inside physical RAM while maintaining an emergency buffer for traffic spikes:

# 1. Allocate 8GB swapfile
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 2. Persist across server reboots in /etc/fstab
if ! grep -q '/swapfile' /etc/fstab; then
  echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
fi
# 3. Configure optimal swappiness (10) and inode cache retention (50)
sudo tee /etc/sysctl.d/99-swap.conf > /dev/null <<'EOF'
vm.swappiness=10
vm.vfs_cache_pressure=50
EOF
sudo sysctl --system

SSH Perimeter Hardening & Fail2ban

Never leave OpenSSH listening on default port 22 with password authentication enabled. Automated botnets continuously hammer port 22 with dictionary attacks across all public IP blocks.

1. Hardening sshd_config

Edit /etc/ssh/sshd_config.d/hardening.conf and enforce public-key authentication on a high, non-standard port:

# Relocate SSH to non-standard port (e.g. 56627)
Port 56627
# Disallow root login entirely
PermitRootLogin no
# Enforce public-key authentication; disable passwords
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
# Disallow empty passwords and X11 forwarding
PermitEmptyPasswords no
X11Forwarding no
# Set maximum authentication attempts
MaxAuthTries 3

Validate configuration syntax and restart the SSH daemon:

sudo sshd -t
sudo systemctl restart ssh

2. Private Network Management with WireGuard

For ultimate security, close the SSH port to the public internet entirely and bind administrative listening solely to an encrypted virtual mesh. Tunnel your administrative traffic through our zero-leak WireGuard VPN tunnel. This transforms your VPS into a private fortress where administrative endpoints simply do not exist on the public internet.

Frequently Asked Questions & Troubleshooting

How can I completely isolate my origin server so only Cloudflare can reach it?

The most resilient origin isolation method is deploying an outbound Cloudflare Tunnel (cloudflared) in Docker, which requires zero open inbound firewall ports (ports 80 and 443 remain completely closed). If using a direct reverse proxy, you must configure the DOCKER-USER iptables chain to drop all incoming packets except those originating from Cloudflare’s published CIDR IP blocks.

Why does Docker ignore UFW deny rules when publishing ports on Ubuntu 24.04?

Because Docker automatically manipulates the Linux kernel iptables NAT table at the PREROUTING stage before packets ever hit UFW’s INPUT chain. Packets destined for container ports are forwarded straight to the docker0 bridge, meaning a ‘ufw deny 8080’ rule is silently bypassed unless enforced via the DOCKER-USER chain or by binding container ports to 127.0.0.1.

What is the optimal swap configuration for Docker servers running Ubuntu 24.04 LTS?

Configure a dedicated swapfile backed by zRAM compression alongside a conservative swappiness parameter (vm.swappiness=10) and memory overcommit tuning (vm.overcommit_memory=1). This prevents Linux kernel OOM killer panics during temporary memory spikes while ensuring active Docker container working sets remain locked in high-speed physical RAM.

Can I access SSH securely over Cloudflare Zero Trust without opening port 22 to the public internet?

Yes, running cloudflared access ssh routes SSH sessions through a private Cloudflare Tunnel protected by your Cloudflare Zero Trust dashboard with hardware MFA. The host SSH daemon binds strictly to localhost or private mesh interfaces, completely shielding port 22 from automated internet bot scans and brute-force attempts.

Summary Production Checklist

Security DomainVulnerabilityRemediation Strategy
Docker NetworkingUFW bypass on published portsFilter via DOCKER-USER iptables chain
Edge ProxyDirect origin IP scraping & DDoSCloudflare IPv4 whitelisting / Authenticated Origin Pulls
Memory PressureOOM killer terminating databases8GB swapfile with vm.swappiness=10
Perimeter AuthBrute-force password guessingPubkey-only authentication on non-standard port
Internal DNS & AdminExposed administration dashboardsEncrypted WireGuard VPN mesh integration

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.