How to allow a specific port through UFW

Open a TCP or UDP port in the UFW firewall on Ubuntu to allow inbound network traffic.

Open a specific TCP or UDP port in the UFW (Uncomplicated Firewall) to allow inbound traffic on Ubuntu and Debian servers.

Prerequisites

  • Ubuntu or Debian with UFW installed (preinstalled on Ubuntu)
  • Root or sudo access
  • UFW enabled ( sudo ufw enable)

Step-by-Step: Allow a Specific Port Through UFW

1. Allow a TCP Port with UFW

UFW opens a TCP port for inbound traffic with the allow command:

sudo ufw allow 80/tcp

The UFW rule allows inbound TCP connections on port 80 from any source IP address.

2. Allow a UDP Port with UFW

sudo ufw allow 53/udp

3. Allow Both TCP and UDP on the Same Port

Omit the protocol to allow both TCP and UDP:

sudo ufw allow 443

4. Allow a Port Range with UFW

sudo ufw allow 8000:8100/tcp

UFW opens all TCP ports from 8000 through 8100.

How to Verify the Rule Was Added

sudo ufw status numbered

Common Issues

Rules have no effect after adding— UFW may not be enabled. Check with sudo ufw status. If it shows "inactive", enable with sudo ufw enable (ensure SSH is allowed first).