UFW Allow Port: Open, List and Delete Firewall Rules

Linux systems administrator managing UFW firewall rules and opening server network ports in modern terminal environment
UFW Allow Port: Open, List and Delete Firewall Rules 3

To open a network port on Ubuntu or Debian using UFW (Uncomplicated Firewall), run the command sudo ufw allow <port>, or specify the protocol explicitly with sudo ufw allow <port>/<tcp|udp>. Managing network access with ufw allow port commands lets you expose web servers, database endpoints, or custom services while keeping all other inbound traffic blocked by default. In this guide, you will learn how to open single ports, port ranges, and service names, list active rules with index numbers, and safely delete firewall entries without locking yourself out of your server.

Prerequisites

  • A Linux server running Ubuntu or Debian with sudo privileges.
  • UFW installed via sudo apt update && sudo apt install ufw -y.

Before modifying rules, establish a secure default baseline by dropping inbound connections and permitting outbound traffic:

sudo ufw default deny incoming
sudo ufw default allow outgoing

How to Open Ports with UFW

1. Open a Single Port

To open a port for both TCP and UDP traffic, specify the port number directly:

sudo ufw allow 80

In production environments, specify the transport protocol to minimize your attack surface:

# Allow incoming TCP traffic for HTTP and HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Allow incoming UDP traffic for DNS or VPN tunnels
sudo ufw allow 53/udp

For example, self-hosted VPN tunnels such as WireGuard require UDP port 51820. For an architectural comparison of tunnel protocols, see our guide on Tailscale vs WireGuard for server infrastructure.

2. Open a Port Range

When running applications that negotiate dynamic connections (such as passive FTP or media streaming), open a contiguous range of ports using a colon (:). Specifying the protocol is mandatory for port ranges:

# Allow TCP ports 6000 through 6007
sudo ufw allow 6000:6007/tcp

# Allow UDP ports 7000 through 7010
sudo ufw allow 7000:7010/udp

3. Open Ports by Service Name

UFW resolves standard service names from /etc/services:

sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

Before opening port 22, ensure your host uses cryptographic keys instead of vulnerable passwords; read our technical guide on what is an SSH key and how it works.

4. Restrict Port Access to Specific IP Addresses

Exposing database ports directly to the public internet presents severe security risks. Restrict inbound traffic to a specific source IP address or subnet:

# Allow MySQL (port 3306) only from a trusted application server
sudo ufw allow proto tcp from 192.168.1.100 to any port 3306

# Allow PostgreSQL (port 5432) from an internal management subnet
sudo ufw allow proto tcp from 10.8.0.0/24 to any port 5432

Binding internal services like AI inference endpoints to public interfaces without IP filtering is a frequent source of data exposure. If you run local AI tools, review our analysis on whether Ollama is safe and how to lock down exposed AI ports.

Summary of Common UFW Port Commands

Target / Service Protocol Command Use Case
SSH (Port 22) TCP sudo ufw allow 22/tcp Remote server terminal access
HTTP (Port 80) TCP sudo ufw allow 80/tcp Web traffic & Let’s Encrypt validation
HTTPS (Port 443) TCP sudo ufw allow 443/tcp Encrypted web traffic (TLS/SSL)
WireGuard (Port 51820) UDP sudo ufw allow 51820/udp Encrypted VPN tunnel endpoints
PostgreSQL (Port 5432) TCP sudo ufw allow from 10.0.0.5 to any port 5432 proto tcp Internal database communication

How to List Active UFW Rules

Inspect your firewall status using one of three listing modes:

sudo ufw status
sudo ufw status verbose
sudo ufw status numbered

The sudo ufw status numbered command outputs rules prefixed with an index bracket:

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 80/tcp                     ALLOW IN    Anywhere
[ 3] 443/tcp                    ALLOW IN    Anywhere

How to Delete UFW Rules

You can remove firewall rules by exact command syntax or by rule index number.

Method 1: Delete by Exact Rule Syntax

Prepend delete to the original allow command:

sudo ufw delete allow 80/tcp
sudo ufw delete allow from 192.168.1.100 to any port 3306 proto tcp

Method 2: Delete by Rule Number

  1. List all active rules with index numbers: sudo ufw status numbered
  2. Identify the target rule index and run: sudo ufw delete <number> (e.g. sudo ufw delete 2).
  3. Confirm the prompt with y.

Note: When you delete a numbered rule, higher rule numbers decrement by one. When deleting multiple rules, delete them from highest to lowest index or refresh the list between commands.

Activating and Testing Your Firewall

If UFW is inactive, enable it with:

sudo ufw enable

Confirm with y only after allowing your SSH port. To apply rule changes without dropping active sessions, reload UFW:

sudo ufw reload

Verify network reachability from a remote client using Netcat:

nc -zv <server-ip> 443

Common Pitfalls and How to Avoid Them

  • Locking Yourself Out of SSH: Running sudo ufw enable before running sudo ufw allow 22/tcp immediately drops your remote session. Always open your SSH port first, and keep your current session active until a secondary terminal connection succeeds.
  • Omitting Transport Protocols: Running sudo ufw allow 8080 opens both TCP and UDP. If your service only uses TCP, open 8080/tcp instead.
  • The Docker Firewall Bypass: By default, Docker inserts rules directly into the iptables NAT PREROUTING chain. Container ports published with -p 8080:8080 bypass UFW rules and remain publicly accessible. Bind container ports to localhost (-p 127.0.0.1:8080:8080) to enforce reverse proxy filtering.

Frequently Asked Questions (FAQ)

How do I list UFW rules with rule numbers?

Run sudo ufw status numbered in your terminal. This command displays all active rules with a bracketed index number (such as [ 1], [ 2]), making it easy to identify specific rules for removal.

How do I allow SSH safely before enabling UFW?

Execute sudo ufw allow ssh or sudo ufw allow 22/tcp before running sudo ufw enable. If your SSH daemon uses a custom port (such as 2222), execute sudo ufw allow 2222/tcp instead.

How do I delete a UFW rule by number or syntax?

To delete by number, run sudo ufw status numbered and execute sudo ufw delete <number>. To delete by syntax, prepend delete to the original allow command, such as sudo ufw delete allow 80/tcp.

Conclusion

The ufw allow port command is the foundation of network security on Ubuntu and Debian hosts. By enforcing a default deny policy, specifying exact transport protocols, and restricting sensitive services to private subnets, you maintain an effective firewall that protects your server infrastructure from unauthorized external probes.