Configure UFW on Ubuntu

Installation of UFW

UFW is already installed on Ubuntu 18.04. You can verify it from here

which ufw

Output:

 root@vps:~#  which ufw
 /usr/sbin/ufw

Enable/Disable/Reload UFW

Enable UFW

Run the command below to activate the firewall

ufw enable

Disable UFW

Run the command below to activate the firewall

ufw disable

Reload UFW

Run the following command to turn off the firewall

ufw reload

If you don’t receive output, you need to install UFW. Please refer the following commands.

sudo apt-get install ufw

Allow/Deny port

Allow port

Run the command below to allow traffic on a specific port (example: port 80 for HTTP):

ufw allow 80/tcp

Change the port which you want to open.

Deny Port

Run the following command to block/deny traffic on a specific port

ufw deny 80

Change the port that you want to deny

Check Status of UFW

Run the following command to check whether UFW is active and view current rules

ufw status

Listing Rules

Run the following command to display rules in a numbered format (useful for managing rules)

ufw status numbered

Output:

root@vps:~# ufw status numbered
Status: active

  To                         Action      From
  --                         ------      ----
 [ 1] 80/tcp                     ALLOW IN    Anywhere
 [ 2] 80                         DENY IN     Anywhere
 [ 3] 80/tcp (v6)                ALLOW IN    Anywhere (v6)
 [ 4] 80 (v6)                    DENY IN     Anywhere (v6)

Removing rules

First, check the status of UFW using the following command.

ufw status numbered

Output:

root@vps:~# ufw status numbered
Status: active

  To                         Action      From
  --                         ------      ----
 [ 1] 80/tcp                     ALLOW IN    Anywhere
 [ 2] 80                         DENY IN     Anywhere
 [ 3] 80/tcp (v6)                ALLOW IN    Anywhere (v6)
 [ 4] 80 (v6)                    DENY IN     Anywhere (v6)

This command will show the rules with numbers

Now remove the rule using the following command(Replace the 1 with the rule number which you want to remove)

ufw delete 1

Output:

 root@vps:~# ufw delete 1
 Deleting:
  allow 80/tcp
 Proceed with operation (y|n)? y
 Rule deleted
 root@vps:~#

CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS