Advance

How to set up a Firewall with UFW on Debian 10

How to set up a Firewall with UFW on Debian 10
0
(0)

You have read our latest firewall tutorial. In this article, you will learn How to set up a Firewall with UFW on Debian 10. Uncomplicated Firewall (UFW) is an interface to iptables that is geared towards simplifying the process of configuring a firewall. While iptables is a solid and flexible tool, it can be difficult for beginners to learn how to use it to properly configure a firewall. If you’re looking to get started securing your network and are not sure which tool to use, UFW may be the right choice for you.

 

Prerequisites

The tutorial may be more useful if you know:

a non-root user with sudo privileges

To set up, follow our Initial server setup on Debian 10

 

Recommended Article: Tutorial Install Docker CE on Debian 11 [Bullseye]

How to set up a Firewall with UFW on Debian 10

Stay with us, this tutorial will show you how to set up a firewall with UFW on Debian 10. Review the following steps to get an expert on this subject.

 

1- Installing UFW

Since Debian does not install UFW by default, you will have installed and enabled UFW as you followed the entire Initial Server Setup tutorial, you will have installed and enabled UFW. If not, install it now using apt:

sudo apt install ufw

 

Using IPv6 with UFW (Optional)

This tutorial is written with IPv4 in mind but will work for IPv6 as long as you enable it. If your Debian server has IPv6 enabled, you will want to ensure that UFW is configured to support IPv6; this will ensure that UFW will manage firewall rules for IPv6 in addition to IPv4. To configure this, open the UFW configuration file /etc/default/ufw with nano or your favorite editor

sudo nano /etc/default/ufw

Make sure the value of IPV6 is yes. It should look like this:

/etc/default/ufw excerpt
IPV6=yes  

You can save and close the file now. When UFW is enabled, it will be configured to write both IPv4 and IPv6 firewall rules. Before enabling UFW, however, you will want to ensure that your firewall is configured to allow you to connect via SSH. Let’s start by setting the default policies.

 

3- Setting Up Default Policies

The first rules to define are your default policies if you’re just getting started with your firewall. These rules handle the traffic that does not explicitly match any other rules. By default, UFW is set to deny all incoming connections and allow all outgoing connections. This means anyone trying to reach your server would not be able to connect, while any application within the server would be able to reach the outside world.

To set your UFW rules back to the defaults, you can be sure that you’ll be able to follow along with this tutorial. To set the defaults used by UFW, use these commands:

sudo ufw default deny incoming  sudo ufw default allow outgoing

 

buy vps with bitcoin

 

4- Allowing SSH Connections

By enabling your UFW firewall, it would deny all incoming connections. And you will need to create rules that explicitly allow legitimate incoming connections — SSH or HTTP connections, for example — if we want our server to respond to those types of requests. If you’re using a cloud server, you will probably want to allow incoming SSH connections so you can connect to and manage your server.

To configure your server to allow incoming SSH connections:

sudo ufw allow ssh

This will create firewall rules that will allow all connections on port 22, which is the port that the SSH daemon listens on by default. UFW knows what port allow ssh means because it’s listed as a service in the /etc/services file.

In conclusion, you can actually write the equivalent rule by specifying the port instead of the service name. For example, this command produces the same result as the one above:

sudo ufw allow 22

You will have to specify the appropriate port, if you configured your SSH daemon to use a different port, you will have to specify the appropriate port. For example, if your SSH server is listening on port 2222, you can use this command to allow connections on that port:

sudo ufw allow 2222      

5- Enabling UFW

Since you configured your firewall to allow incoming SSH connections, you can enable it.

sudo ufw enable

By running the command above, you would 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. Then, you will find the firewall active. But to see the rules that you have set, run the sudo ufw status verbose command.

6- Allowing Other Connections

From now on, allow all of the other connections that your server needs to function properly. The connections that you should allow depend on your specific needs. Luckily, you already know how to write rules that allow connections based on a service name or port; we already did this for SSH on port 22. You can also do this for:

  • HTTP on port 80, which is what unencrypted web servers use. To allow this type of traffic, you would type sudo ufw allow http or sudo ufw allow 80.
  • HTTPS on port 443, which is what encrypted web servers use. To allow this type of traffic, you would type sudo ufw allow https or sudo ufw allow 443.

Let’s verify other ways to allow connections, however, aside from specifying a port or known service.

Specific Port Ranges

You are learning how to specify port ranges with UFW. For example, some applications use multiple ports instead of a single port.

To allow X11 connections, which use ports 6000-6007, use these commands:

sudo ufw allow 6000:6007/tcp  sudo ufw allow 6000:6007/udp

 

Specific IP Addresses

When working with UFW, you can also specify IP addresses. For example, if you want to allow connections from a specific IP address, such as a work or home IP address of 203.0.113.4, you need to specify from and then the IP address:

sudo ufw allow from 203.0.113.4

You 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. For example, if you want to allow 203.0.113.4 to connect to port 22 (SSH), use this command:

sudo ufw allow from 203.0.113.4 to any port 22

Subnets

If you want to allow a subnet of IP addresses, you can do so using CIDR notation to specify a netmask. For example, if you want to allow all of the IP addresses ranging from 203.0.113.1 to 203.0.113.254 you can use this command:

sudo ufw allow from 203.0.113.0/24

Also, you may 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.

 

Connections to a Specific Network Interface

Specify allow in on, followed by the name of the network interface, if you want to create a firewall rule that only applies to a specific network interface

To look up your network interfaces before continuing:

ip addr

Output

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state  . . .  3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default  . . .

The highlighted output indicates the network interface names. They have typically named something like eth0 or enp3s2.

In case your server has a public network interface called eth0, for example, you could allow HTTP traffic to it with this command:

sudo ufw allow in on eth0 to any port 80

So it will allow your server to receive HTTP requests from the public internet.

Or, if you want your MySQL database server (port 3306) to listen for connections on the private network interface eth1, you could use this command:

sudo ufw allow in on eth1 to any port 3306

7- Denying Connections

It is clear that UFW is configured to deny all incoming connections if you haven’t changed the default policy for incoming connections. However, this simplifies the process of creating a secure firewall policy by requiring you to create rules that explicitly allow specific ports and IP addresses through.

And anytime you wish to deny specific connections based on the source IP address or subnet, however, perhaps because you know that your server is being attacked from there. Also, if you want to change your default incoming policy to allow (which is not recommended), you would need to create deny rules for any services or IP addresses that you don’t want to allow connections for.

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

To deny HTTP connections:

sudo ufw deny http

Also, you can use the following command if you want to deny all connections from 203.0.113.4 

8- Deleting Rules

It is important to learn how to create and delete a firewall. There are two ways to specify which rules to delete: by the rule number or by the rule itself. This is similar to how the rules were specified when they were created. We’ll start by explaining the delete by rule number method.

By Rule Number

You should get a list of your firewall rules if you have chosen to use the rule number to delete firewall rules. The UFW status command has the numbered option, which displays numbers next to each rule:

sudo ufw status numbered
Output
Status: active         To                         Action      From       --                         ------      ----  [ 1] 22                         ALLOW IN    15.15.15.0/24  [ 2] 80                         ALLOW IN    Anywhere

 

By deciding to delete rule 2, which allows HTTP connections on port 80, we can specify this in the following UFW delete command:

sudo ufw delete 2

Next, you would see a confirmation prompt, which you can answer with y/n. Typing y will then delete rule 2.

Note: If you have IPv6 enabled, you will want to delete the corresponding IPv6 rule as well.

By Actual Rule

To use an alternative way to rule numbers, 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

Even though instead of the service name, you can also specify the rule with allow 80

sudo ufw delete allow 80

Point: This method will delete both IPv4 and IPv6 rules if they exist.

Recommended Article: How to Install Elastic Stack 7 on Debian 11

9- Checking UFW Status and Rules

To check the status of UFW with this command:

sudo ufw status verbose

If UFW is disabled, which is the default, you’ll see something like this:

Output
Status: inactive

If UFW is active, which it should be if you followed Step 3, the output will say that it’s active and will list any rules that you have set. For example, if the firewall is set to allow SSH (port 22) connections from anywhere, the output might look something like this:

Output
Status: active    To                         Action      From  --                         ------      ----  22/tcp

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

 

 

10- Disabling or Resetting UFW (optional)

To 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.

But 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

 

Finally, this will disable UFW and delete any rules that you have 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.

Good job! Your firewall is now configured to allow SSH connections. Be sure to allow any other incoming connections that your server needs, while also limiting unnecessary connections. This will ensure that your server is both functional and secure.

Dear user, we hope you would enjoy this tutorial How to set up a Firewall with UFW on Debian 10, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

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

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day