Linux Firewall Basics: Lock Down Your Server With UFW
Every open port on a Linux server is a door. Some doors you need — SSH for administration, a web port for your site. The rest are just attack surface. A firewall decides which doors exist at all, and UFW (Uncomplicated Firewall) is the easiest way to manage that on Ubuntu and Debian systems.
What UFW Actually Does
UFW is a friendly front end for iptables/netfilter, the firewall built into the Linux kernel. It inspects incoming and outgoing network traffic and blocks anything you haven’t explicitly allowed. Think of it as a bouncer with a guest list.
The Golden Rule: Allow SSH FIRST
The classic beginner mistake is enabling the firewall before allowing SSH — and instantly locking yourself out of your own server. Always do this first:
sudo ufw allow ssh
Or, if you use a custom SSH port (say 2222):
sudo ufw allow 2222/tcp
Only after SSH is allowed do you enable the firewall:
sudo ufw enable
A Sensible Default Setup
Start with deny-by-default, then open only what you need:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
That is a complete, sane firewall for most web servers: SSH and web traffic in, everything else blocked, all outbound traffic allowed.
Managing Rules Day to Day
sudo ufw status verbose
sudo ufw allow 8080/tcp
sudo ufw delete allow 8080/tcp
sudo ufw deny 23/tcp
Numbered rules make deletion easier:
sudo ufw status numbered
sudo ufw delete 3
Rate-Limiting SSH Against Brute Force
UFW has a built-in trick for SSH brute-force attacks:
sudo ufw limit ssh
This allows normal SSH use but blocks an IP that tries to connect too many times in quick succession — the exact pattern brute-force bots use. Pair it with key-based authentication and disabled password login for a very strong SSH setup.
Common Mistakes to Avoid
- Enabling UFW before allowing SSH (the lockout above).
- Opening wide port ranges “just in case” — every open port is an invitation to probe.
- Forgetting the firewall exists when a new service doesn’t work — check ufw status before debugging the service itself.
- Running a firewall on a server but leaving the same service exposed on another interface (like a cloud security group) — check both layers.
Check Your Work
From another machine, scan your own server’s ports (only servers you own or have permission to test):
nmap -Pn your-server-ip
You should see only the ports you allowed. Everything else should show as filtered.
A firewall is not exciting. It will never make a demo video. But it is the single most effective five-minute security improvement you can make to any Linux server — and UFW makes it genuinely uncomplicated.



Comments
Post a Comment