Mastering Firewall Setup on Ubuntu 24.04: A Beginner-to-Expert Guide

When setting up a server, security is non-negotiable—and one of the most important steps is configuring your firewall. With the release of Ubuntu 24.04 LTS, it's a perfect time to revisit best practices for firewall setup and management.

This guide walks you through setting up and mastering UFW (Uncomplicated Firewall), Ubuntu’s default firewall interface. Whether you're a beginner or looking to reinforce your knowledge, this post will give you a solid foundation.


🚧 What is a Firewall and Why Do You Need One?

A firewall acts as a barrier between your system and potential threats from the outside world. It allows or blocks traffic based on a set of security rules, helping prevent:

  • Unauthorized access
  • DDoS attacks
  • Malware propagation
  • Data leaks

On Ubuntu, UFW simplifies firewall management with a user-friendly command-line interface over iptables (or nftables in newer kernels).


🛠️ Step 1: Install and Enable UFW (If Not Already)

Ubuntu 24.04 comes with UFW pre-installed. But in case it's missing:

sudo apt update
sudo apt install ufw

Enable the firewall (don’t worry, SSH is preserved if it’s already active):

sudo ufw enable

To check the status:

sudo ufw status verbose

🌐 Step 2: Allow Essential Services

UFW works by default-deny, meaning all incoming traffic is blocked unless explicitly allowed.

Here’s how to open common ports:

# Allow SSH (port 22)
sudo ufw allow ssh

# Allow HTTP (port 80)
sudo ufw allow http

# Allow HTTPS (port 443)
sudo ufw allow https

You can also specify ports manually:

sudo ufw allow 8080/tcp

🔐 Step 3: Restrict Access to Specific IPs or Ranges

Only want a specific IP to access your server?

sudo ufw allow from 192.168.1.100 to any port 22

Or allow a whole subnet:

sudo ufw allow from 192.168.1.0/24 to any port 22

This is especially useful for internal access or zero-trust network designs.


📤 Step 4: Allow Outgoing Traffic (Default Behavior)

UFW by default allows all outgoing traffic. You can restrict this if you need tighter control:

sudo ufw default deny outgoing

Then selectively allow needed services (e.g., DNS, HTTP):

sudo ufw allow out 53    # DNS
sudo ufw allow out 80    # HTTP
sudo ufw allow out 443   # HTTPS

Be cautious—blocking outgoing can break package updates, DNS lookups, and more.


🚫 Step 5: Block Specific IPs or Ports

Block a known malicious IP:

sudo ufw deny from 203.0.113.45

Block a port (e.g., Telnet):

sudo ufw deny 23

📋 Step 6: View and Manage Firewall Rules

List all rules with line numbers:

sudo ufw status numbered

To delete a rule:

sudo ufw delete [number]

Reset the entire firewall:

sudo ufw reset

🧪 Step 7: Test Your Configuration

Use tools like:

  • nmap from another machine to scan open ports
  • curl or telnet to verify port access
  • Online tools like ShieldsUP

🧠 Pro Tips for Mastery

  • Use ufw logging on to monitor blocked attempts in /var/log/ufw.log
  • Integrate with fail2ban for brute-force protection

Use aliases for faster management:

alias ufws='sudo ufw status numbered'
alias ufwr='sudo ufw reload'

✅ Final Thoughts

UFW makes firewall management on Ubuntu 24.04 simple yet powerful. With just a few commands, you can harden your server, minimize the attack surface, and gain peace of mind.

Whether you're running a production web server, a personal project, or a home media server—don’t skip the firewall.


🧰 Quick Reference

Command Description
sudo ufw status verbose Show current status
sudo ufw allow ssh Allow SSH
sudo ufw deny 23 Block port 23
sudo ufw delete [n] Delete rule number n
sudo ufw reset Reset all rules
sudo ufw reload Reload configuration
sudo ufw enable/disable Turn firewall on/off

Have questions or suggestions? Leave a comment below or share your favorite UFW tip!