How to configure ufw firewall

The default firewall configuration tool for Ubuntu is ufw. Developed to ease iptables firewall configuration, ufw provides a user friendly way to create an IPv4 or IPv6 host-based firewall. By default UFW is disabled.

Step1: Install ufw

#apt-get install ufw

Step2: Configure default policies

#ufw default deny incoming

#ufw default allow outgoing

Step3: Allow ssh connections

#ufw allow ssh

If ssh service running on a different port, use the below command to allow the connection.

#ufw allow port

Step4: Enable ufw

#ufw enable

Step5: Allowing other connections

Allow Service

#ufw allow http

Allow Single Port

#ufw allow 80

Allow Port Range

#ufw allow 6000:6007/tcp

#ufw allow 6000:6007/udp

Allow IPadress

#ufw allow from 192.168.10.12

Note that we can also specify a specific port that the IP address is allowed to connect to by adding to any port followed by the port number.

#ufw allow from 192.168.10.12 to any port 22

Allow Subnet

#ufw allow from 192.168.10.0/24

Note that we can also specify a specific port that the subnet is allowed to connect to by adding to any port followed by the port number.

#ufw allow from 192.168.10.0/24 to any port 22

Step6: Denying connections

To write deny rules, you can use the commands described above, replacing allow with deny.

Examples are,

#ufw deny http

#ufw deny from 192.168.10.12

Step7: Deleting the rules

There are two different ways specify which rules to delete: by rule number or by the actual rule (similar to how the rules were specified when they were created).

By Rule Number

The UFW status command has an option to display numbers next to each rule.

#ufw status numbered

Numbered Output:Status: active

To Action From

-- ------ ----

[ 1] 80 ALLOW IN Anywhere

If we decide that we want to delete rule 1, the one that allows port 80 (HTTP) connections, we can specify it in a UFW delete command like this:

#ufw delete 2

By Actual Rule

The alternative to rule numbers is to specify the actual rule to delete. For example, if you want to remove the allow http rule,

#ufw delete allow http

You could also specify the rule by allow 80, instead of by service name

#ufw delete allow 80

Step8: Check ufw status

#ufw status verbose

If UFW is disabled, which it is by default, you'll see something like this:

OutputStatus: inactive

If UFW is active, which it should be if you followed Step 3, the output will say that it's active and it will list any rules that are set.

Use the status command if you want to check how UFW has configured the firewall.

Step9: Disabling the firewall

We can disable ufw with this command

#ufw disable

Step10: Resetting the firewall

If we need to start with the inital settings, there is a reset option there. Note that this will remove all the additional rules configured.

#ufw reset

Leave a Reply

Your email address will not be published. Required fields are marked *