
How to secure Debian Server 9.X – Setup a firewall
The most easy and fast Firewall I think there is, is UFW. UFW, or Uncomplicated Firewall, 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 you’re not sure which tool to use, UFW may be the right choice for you. This tutorial will show you how to set up a firewall with UFW on Debian 9.
Step 1 – Installing UFW
Debian does not install UFW by default. Install it by using apt-get
:
1 |
sudo apt-get install ufw |
We will set up UFW and enable it in the following steps.
Step 2 – Using IPv6 with UFW (Optional)
This tutorial is written with IPv4 in mind, but will work for IPv6 as well as long as you enable it. If your Debian 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.
1 |
sudo nano /etc/default/ufw |
Then make sure the value of IPV6
is yes
. It should look like this:/etc/default/ufw excerpt
1 |
IPV6=yes |
Save and close the file by hitting CTRL + X, then hit Y
and Enter, 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.
Step 3 – Setting Up Default Policies
If you’re just getting started with your firewall, the first rules to define are your default policies. These rules control how to handle 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.
Let’s set your UFW rules back to the defaults so we can be sure that you’ll be able to follow along with this tutorial. To set the defaults used by UFW, we use these commands:
1 2 |
sudo ufw default deny incoming sudo ufw default allow outgoing |
These commands set the defaults to deny incoming and allow outgoing connections. These firewall defaults alone might suffice for a personal computer, but servers typically need to respond to incoming requests from outside users.
Step 4 – Allowing SSH Connections
If we enabled our UFW firewall now, it would deny all incoming connections. This means that we 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. Most of the time you will have a ‘public’ Linux server with some services (like SSH). You will have to allow the port for this so you’ll be able to still login to the terminal.
If you followed my other tutorials, you now know that our SSH service is running on port 4444 instead of 22.
To configure your server to allow incoming SSH connections, you can use this command:
1 |
sudo ufw allow 4444 |
This will create firewall rules that will allow all connections on port 4444
. If you still have the default SSH configuration, SSH will listen on port 22. You can use the following command to allow this:
1 |
sudo ufw allow ssh |
UFW knows what port allow ssh
means because it’s listed as a service in the /etc/services
file.
Now that your firewall is configured to allow incoming SSH connections, we can enable it.
Step 5 – Enabling UFW
To enable UFW, use this command:
1 |
sudo ufw enable |
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
.
The firewall is now active. Run the sudo ufw status verbose
command to see the rules that are set. The rest of this tutorial covers how to use UFW in more detail, like allowing or denying different kinds of connections.
Step 6 – Allowing Other Connections
At this point, you should allow all of the other connections that your server needs to respond to. The connections that you should allow depends 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 4444. You can also do this for:
- HTTP on port 80, which is what unencrypted web servers use, using
sudo ufw allow http
orsudo ufw allow 80
- HTTPS on port 443, which is what encrypted web servers use, using
sudo ufw allow https
orsudo ufw allow 443
There are several others ways to allow other connections, aside from specifying a port or known service.
Specific Port Ranges
You can specify port ranges with UFW. Some applications use multiple ports, instead of a single port.
For example, to allow for example passive FTP ports, which can use ports between 1024
–65535
, use these commands. However, a passive FTP server will never use the range above, it will probably be a range like 50000
– 50009
1 |
sudo ufw allow 50000:50009/tcp |
When specifying port ranges with UFW, you must specify the protocol (tcp
or udp
) that the rules should apply to. We haven’t mentioned this before because not specifying the protocol automatically allows both protocols, which is OK in most cases.
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 another server in your cluser, with IP 213.182.4.458
, you need to specify from
, then the IP address:
1 |
sudo ufw allow from 213.182.4.458 |
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:
1 |
sudo ufw allow from 203.0.113.4 to any port 22 |
Step 7 – Denying Connections
If you haven’t changed the default policy for incoming connections, UFW is configured to deny all incoming connections. Generally, 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.
However, sometimes you will want to deny specific connections based on the source IP address or subnet, 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.
For example, to deny HTTP connections, you could use this command:
1 |
sudo ufw deny http |
Or if you want to deny all connections from 203.0.113.4
you could use this command:
1 |
sudo ufw deny from 203.0.113.4 |
Now let’s take a look at how to delete rules.
Step 8 – Deleting Rules
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 number method 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:
1 |
sudo ufw status numbered |
1 2 3 4 5 6 7 |
Numbered Output:Status: active To Action From -- ------ ---- [ 1] 22 ALLOW IN 15.15.15.0/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:
1 |
sudo ufw delete 2 |
This would show a confirmation prompt 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:
1 |
sudo ufw delete allow http |
You could also specify the rule by allow 80
, instead of by service name:
1 |
sudo ufw delete allow 80 |
This method will delete both IPv4 and IPv6 rules, if they exist.
Step 9 – Checking UFW Status and Rules
At any time, you can check the status of UFW with this command:
1 |
sudo ufw status verbose |
If UFW is disabled, which it is by default, you’ll see something like this:
1 |
Status: inactive |
If UFW is active, which it should by now, the output will say that it’s active and it will list any rules that are set. The output might look something like this:
1 2 3 4 5 6 7 8 9 |
Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere |
Use the status
command if you want to check how UFW has configured the firewall.
Step 10 — Disabling or Resetting UFW (optional)
If you decide you don’t want to use UFW, you can disable it with this command:
1 |
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:
1 |
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.
Conclusion
Your firewall is now configured to allow (at least) SSH connections. Be sure to allow any other incoming connections that your server, while limiting any unnecessary connections, so your server will be functional and secure.
I find it a great way to first think about any services I am running, then figuring out which ports / IP’s are needed, and then modifying my firewall.
Share This Post
Recent Posts
- How to secure Debian Server 9.X – Scan for malicious items (Rkhunter)
- How to secure Debian Server 9.X – Setup a firewall
- How to secure Debian Server 9.X – Disable root login & change SSH port
- How to secure Debian Server 9.X – Setup SSH keys
- How to make a WordPress website running on a Linux webserver – Part 1
Leave a Reply