Photo by Ines Cvitić on Unsplash
How do I use ufw firewall?
6 min read
Debian and Ubuntu distributions come with a firewall configuration tool called UFW (Uncomplicated Firewall). It is the most popular and easy-to-use command line tool for configuring and managing a firewall on Ubuntu and Debian distributions.
In this article, I will explain how to install and use a UFW firewall on Ubuntu.
Make sure you have logged into your Ubuntu or Debian server with a
sudouser or with the root account.
How to Install UFW Firewall on Ubuntu
The UFW (Uncomplicated Firewall) should be installed by default in Ubuntu and Debian, if not, install it using the APT package manager]using the following command.
sudo apt-get update sudo apt install ufw
Using IPv6 with UFW (Optional)
If your Ubuntu server has
IPv6 enabled, ensure that UFW is configured to support
IPv6 so that it will manage firewall rules for
IPv6 in addition to
IPv4. To do this, open the UFW configuration with
nano or your favorite editor.
sudo nano /etc/default/ufw
Then make sure the value of
yes. It should look like this:
# /etc/default/ufw # Set to yes to apply rules to support IPv6 (no means only IPv6 on loopback # accepted). You will need to 'disable' and then 'enable' the firewall for # the changes to take affect. IPV6=yes
Save and close the file using
CTRL+X if opened with nano editor. Now, when UFW is enabled, it will be configured to write both IPv4 and IPv6 firewall rules. However, before enabling UFW, we will want to ensure that your firewall is configured to allow you to connect via SSH. Let's start with setting the default policies.
Setting UFW to Default
By default, UFW is set to deny all incoming connections and allow all outgoing connections.
You can set your UFW rules back to the default policies using:
sudo ufw default deny incoming sudo ufw default allow outgoing
Be default SSH might be disabled so don't forget to enable
SSH or you will not be able to login to your server.
sudo ufw allow ssh sudo ufw allow OpenSSH sudo ufw allow 22
How do I enable UFW in Linux?
ufw is disabled. To enable
ufw, use this command
sudo ufw enable
Firewall is active and enabled on system startup
Important: You will receive a warning that says the command may disrupt existing SSH connections. We already set up a firewall rule that allows SSH connections, so it should be fine to continue. Respond to the prompt with y and hit ENTER.
If you enabled your UFW firewall now, it would deny all incoming connections. So, First you should allow
ssh connection or you will not be able to log in to your server as soon as you close your terminal.
sudo ufw allow ssh sudo ufw allow 22
The firewall is now active. Run the
sudo ufw status verbose command to see the rules that are set.
Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), deny (routed) New profiles: skip
How to Disable or Reset UFW
If you decide you don't want to use UFW, you can disable it with this command:
sudo ufw disable
Any rules that you created with UFW will no longer be active. You can always run
sudo ufw enable if you need to activate it later.
If you already have UFW rules configured but you decide that you want to start over, you can use the reset command:
sudo ufw reset
This will disable UFW and delete any rules that were previously defined. Keep in mind that the default policies won't change to their original settings, if you modified them at any point. This should give you a fresh start with UFW.
Allow and Deny Connections in UFW
Syntax for ALLOW
sudo ufw allow <port>/<optional: protocol>
To allow incoming tcp and udp packet on port 53, run
sudo ufw allow 53
To allow incoming tcp packets on port 53, run
sudo ufw allow 53/tcp
To allow incoming udp packets on port 53, run
sudo ufw allow 53/udp
Syntax for Deny
sudo ufw deny <port>/<optional: protocol>
To deny tcp and udp packets on port 53, run
sudo ufw deny 53
To deny incoming tcp packets on port 53, run
sudo ufw deny 53/tcp
To deny incoming udp packets on port 53, run
sudo ufw deny 53/udp
List of Useful UFW Commands
sudo ufw enable
sudo ufw disable
sudo ufw allow ssh
Allow SSH Port:
sudo ufw allow 22. For custom ssh port like 2222:
sudo ufw allow 2222
sudo ufw allow httpor
sudo ufw allow 80
sudo ufw allow httpsor
sudo ufw allow 443
Allow Specific IP:
sudo ufw allow from 203.0.113.4
Allow Specific IP for specific PORT:
sudo ufw allow from 203.0.113.4 to any port 22
Allow Specific TCP Port Range:
sudo ufw allow 6000:6007/tcp
Allow Specific UDP Port Range:
sudo ufw allow 6000:6007/udp
Knowing how to delete firewall rules is just as important as knowing how to create them. There are two different ways to specify which rules to delete: by rule number or by the actual rule (similar to how the rules were specified when they were created). We'll start with the delete by rule numbermethod because it is easier.
By Rule Number
If you're using the rule number to delete firewall rules, the first thing you'll want to do is get a list of your firewall rules. The UFW status command has an option to display numbers next to each rule, as demonstrated here:
sudo ufw status numbered
Numbered Output: Status: active To Action From -- ------ ---- [ 1] 22 ALLOW IN 126.96.36.199/24 [ 2] 80 ALLOW IN Anywhere
If we decide that we want to delete rule 2, the one that allows port 80 (HTTP) connections, we can specify it in a UFW delete command like this:
sudo ufw delete 2
This would show a confirmation prompt and then delete rule 2, which allows HTTP connections. Note that if you have IPv6 enabled, you would want to delete the corresponding IPv6 rule as well.
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, you could write it like this:
sudo ufw delete allow http
You could also specify the rule by allow 80, instead of by service name:
sudo ufw delete allow 80
This method will delete both IPv4 and IPv6 rules, if they exist.
Subnets in UFW
If you want to allow a subnet of IP addresses. For example, if you want to allow all of the IP addresses ranging from
203.0.113.1 to 203.0.113.254 you could use this command:
sudo ufw allow from 203.0.113.0/24
Likewise, you may also specify the destination port that the subnet 203.0.113.0/24 is allowed to connect to. Again, we'll use port 22 (SSH) as an example:
sudo ufw allow from 203.0.113.0/24 to any port 22
Logging in UFW
sudo ufw logging on
sudo ufw logging off
How to check UFW Status and Rules
At any time, you can check the status of UFW with this command:
sudo ufw status verbose
If UFW is disabled, which it is by default, you'll see something like this:
If UFW is active, the output will say that it's active and it will list any rules that are set.
Output Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere
I hope this article helped you in securing your server and learning more about UFW firewall. You can checkout more tutorials.
Did you find this article valuable?
Support Nitin Raturi by becoming a sponsor. Any amount is appreciated!