Logo
Overview
How to setup a Firewall with UFW on Ubuntu 18.04+

How to setup a Firewall with UFW on Ubuntu 18.04+

July 16, 2018
5 min read

Overview

UFW (Uncomplicated Firewall) is a user-friendly interface to manage iptables firewall rules in Ubuntu and Debian-based systems. It simplifies the process of allowing and denying network traffic without complex command syntax.

Note

UFW is installed by default on most Ubuntu installations (18.04+), but it comes disabled. This prevents accidental lockouts during initial server setup.

Prerequisites

You’ll need:

  • Root or sudo access on your Ubuntu server
  • SSH connection for remote management (highly recommended)
  • Basic understanding of network ports

Installation

UFW comes with Ubuntu by default. If it was removed or you’re on a minimal installation, install it with:

Terminal window
sudo apt update
sudo apt install ufw

Step 1: Check Current Status

UFW is disabled by default. Check the current status and existing rules:

Terminal window
sudo ufw status

Output will show Status: inactive on first run. No rules are loaded yet.

Warning (Critical: Secure SSH First)

Before enabling the firewall, you must add a rule allowing SSH. If you block SSH while connected remotely, you’ll lock yourself out and lose access to your server!

Step 2: Allow SSH Access (CRITICAL)

Add a rule to allow SSH connections before enabling the firewall:

Terminal window
sudo ufw allow ssh

Alternatively, allow by port number:

Terminal window
sudo ufw allow 22

If SSH runs on a custom port (recommended for security), allow that port instead:

Terminal window
sudo ufw allow 2222

Replace 2222 with your actual SSH port.

Tip (Why Allow SSH First?)

UFW defaults to deny all incoming connections. Once enabled, only explicitly allowed ports accept traffic. Without an SSH rule, you lose remote access immediately.

Step 3: Enable the Firewall

Now enable UFW:

Terminal window
sudo ufw enable

You’ll see a warning:

Command may disrupt existing ssh connections. Proceed with operation (y|n)?

Enter y — you’ve already whitelisted SSH, so your connection will remain active.

Note

UFW is now active! It denies all incoming connections except those you explicitly allow.

Step 4: Add Rules for Your Services

Allow Web Server (HTTP/HTTPS)

If running a web server, allow HTTP and HTTPS:

Terminal window
sudo ufw allow http # Port 80
sudo ufw allow https # Port 443

Or allow specific ports:

Terminal window
sudo ufw allow 80
sudo ufw allow 443

Allow Custom Application Ports

For any service, use the port number:

Terminal window
sudo ufw allow 3000 # Node.js development server
sudo ufw allow 5432 # PostgreSQL database
sudo ufw allow 3306 # MySQL database
sudo ufw allow 6379 # Redis cache

Allow Specific Protocols

Allow TCP-only or UDP-only traffic:

Terminal window
sudo ufw allow 53/tcp # DNS over TCP
sudo ufw allow 53/udp # DNS over UDP
sudo ufw allow 1194/udp # OpenVPN

Allow by Service Name

UFW recognizes common service names from /etc/services:

Terminal window
sudo ufw allow dns # Port 53
sudo ufw allow ftp # Port 21
sudo ufw allow smtp # Port 25
sudo ufw allow imap # Port 143
sudo ufw allow pop3 # Port 110

Step 5: View and Manage Rules

Check Current Rules

Terminal window
sudo ufw status

Output example:

Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)

For detailed rule information:

Terminal window
sudo ufw status numbered

Deny Specific Ports

Explicitly deny a port (rarely needed due to default deny):

Terminal window
sudo ufw deny 80

Delete Rules

Delete by allow/deny and port:

Terminal window
sudo ufw delete allow 80
sudo ufw delete deny 80

Or delete by rule number (from ufw status numbered):

Terminal window
sudo ufw delete 3
Warning

Always verify the rule number before deletion. Removing the wrong rule could expose your server.

Advanced Usage

Allow from Specific IP Address

Restrict access to a service from a single IP:

Terminal window
sudo ufw allow from 192.168.1.100 to any port 3306

This allows only 192.168.1.100 to access MySQL.

Allow Ranges of Ports

Terminal window
sudo ufw allow 6000:6007/tcp # Allow TCP ports 6000-6007 (X11)

Enable/Disable Without Clearing Rules

Terminal window
sudo ufw disable # Deactivate temporarily (rules preserved)
sudo ufw enable # Re-enable with existing rules

Reset to Defaults

Danger (Destructive Operation)

This removes all rules and disables UFW. Use only for troubleshooting:

Terminal window
sudo ufw reset

Common Service Ports Reference

ServicePortProtocolUFW Command
SSH22TCPufw allow ssh
HTTP80TCPufw allow http
HTTPS443TCPufw allow https
DNS53TCP/UDPufw allow dns
SMTP25TCPufw allow smtp
MySQL3306TCPufw allow 3306
PostgreSQL5432TCPufw allow 5432
MongoDB27017TCPufw allow 27017
Redis6379TCPufw allow 6379
OpenVPN1194UDPufw allow 1194/udp

Troubleshooting

Problem (Locked Out of SSH)

If you can’t SSH after enabling UFW:

  1. Use the hosting provider’s console/IPMI to access the server
  2. Run: sudo ufw allow 22
  3. Reconnect via SSH

Always allow SSH before enabling the firewall!

Problem (Application Not Accessible)

If your web server shows “connection refused”:

  1. Verify the application is running: sudo netstat -tlnp | grep LISTEN
  2. Check if port is allowed: sudo ufw status | grep PORT_NUMBER
  3. Add rule if missing: sudo ufw allow PORT_NUMBER
Problem (UFW Not Starting After Reboot)

Some systems disable UFW on boot. Make it persistent:

Terminal window
sudo systemctl enable ufw
sudo systemctl start ufw

Security Best Practices

Tip (Security Hardening)
  • Principle of Least Privilege: Only allow ports your services actually need
  • Use Service Names: ufw allow ssh is clearer than ufw allow 22
  • Document Rules: Comment your UFW configuration for future reference
  • Review Regularly: Periodically run sudo ufw status to audit active rules
  • Monitor Denied Connections: Check logs with sudo tail -f /var/log/ufw.log

Conclusion

UFW provides a simple yet powerful way to secure your Linux server with a default-deny firewall policy. By allowing only necessary ports and protocols, you significantly reduce your attack surface. Always prioritize SSH security to maintain remote access.