How To's

Allow or deny a port UFW - Ubuntu

61,500 views · 17 found this helpful · 6 did not

Was this helpful?

Problem

Configure a port to allow or deny traffic in UFW.

tl;dr

sudo ufw allow portnumber
sudo ufw deny portnumber

Solution

UFW can be configured to allow or deny specific ports.
In the following example we will first allow port 2000, then show how to deny port 2000.
Depending on the default rule for UFW, it will decide which is best for you.

UFW can be set to automatically deny or allow all ports without a rule.

The command to change this is as follows:

sudo ufw default allow

This will set it to allow all connections not specifically blocked.

To set all ports by default to be blocked:

sudo ufw default deny

To allow port 2000 from any IP

sudo ufw allow 2000

To block port 2000.

sudo ufw deny 2000

  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.