Set up a Firewall with UFW

Set up a Firewall with UFW

simplify secured firewall

Uncomplicated Firewall (UFW) is a simplified firewall management interface that hides the complexity of lower-level packet filtering technologies such iptables and uftables. Let’s learn how to set up a firewall with UFW on Ubuntu server.

Using IPv6 with UFW

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 favourite editor:

sudo nano /etc/default/ufw

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

IPV6=yes

Save and close the file. Now, when UFW is enabled, it will be configured to write both IPv4 & 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 up default policies

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.

If you have already edited UFW, you can set the defaults used by UFW, use these commands:

sudo ufw default deny incoming
sudo ufw default allow outgoing

These firewall defaults alone might suffice for a personal computer, but servers typically need to respond to incoming requests from outside users.

Allowing SSH connections

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, you can use this command:

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/servicesfile.

However, we can actually write the equivalent rule by specifying the port instead of the service name. For example, this command works the same as the one above:

sudo ufw allow 22

If you configured you 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

Now that your firewall is configured to allow incoming SSH connections, we can enable it.

Enabling UFW

To enable UFW, use this command:

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.

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 22. You can also do this for:

  • HTTP on port 80, using: sudo ufw allow http or sudo ufw allow 80
  • HTTPS on port 443, using sudo ufw allow https or sudo 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 X11 connections, which use ports 6000-6007, use these commands:

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

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 a work or home IP address of 201.202.203.204, you need to specify from, then the IP address:

sudo ufw allow from 201.202.203.204

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 201.202.203.204 to connect to port 22, use this command:

sudo ufw allow from 201.202.203.204 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 net mask. For example, if you want to allow all ot the IP addresses ranging from 202.0.110.1 to 202.0.110.254 you could use this command:

sudo ufw allow 202.0.110.0/24

Likewise, you may also specify the destination port that the subnet 202.0.110.0/24 is allowed to connect to. Again, we’ll use port 22 as an example:

sudo ufw allow from 202.0.110.0/24 to any port 22

Connection to a specific network interface

If you want to create a firewall rule that only applies to a specific network interface, you can do so by specifying “allow in on” followed by the name of the network interface.

You may want to look up your network interfaces before continuing. To do so, use this command:

ip addr

The output indicates the network interface names. They are typically named something like eth0 or ens3.

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

So, if your server has a public network interface called eth0, you could allow HTTP traffic (port 80) to it with this command:

sudo ufw allow in on eth0 to any port 80

Doing so would 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, for example, you could use this command:

sudo ufw allow in on eth1 to any port 3306

This would allow other servers on you private network to connect your MySQL database.

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.

For example, to deny HTTP connections, you could use this command:

sudo ufw deny http

Or, if you want to deny all connections from 201.202.203.204 you could use this command:

sudo ufw deny from 201.202.203.204

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 de delete: by rule number or by the actual rule.

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
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 connections, we can specify it in a UFW delete command like this:

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.

Checking 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:

Status: inactive

If UFW is active, the output might look something like this:

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.

Disabling or resetting UFW

If you decide to disable UFW, doing so 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.

Conclusion

Your firewall is now configured to allow specific connections. Be sure to allow any other incoming connections that your server needs, while limiting any unnecessary connections, so your server will be functional and secure.

THE END
Ads by Google

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.41 billion . This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

Setup an IKEv2 server with strongSwan

Tutorials

2020.01.09

Setup an IKEv2 server with strongSwan

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunnelling between networks. It is developed by Microsoft and Cisco (primarily) for mobile users, and introduced as an updated version of IKEv1 in 2005. The IKEv2 MOBIKE (Mobility and Multihoming) protocol allows the client to main secure connection despite network switches, such as when leaving a WiFi area for a mobile data area. IKEv2 works on most platforms, and natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary.

Hands on IBM Cloud Functions with CLI

Tools

2020.10.20

Hands on IBM Cloud Functions with CLI

IBM Cloud CLI allows complete management of the Cloud Functions system. You can use the Cloud Functions CLI plugin-in to manage your code snippets in actions, create triggers, and rules to enable your actions to respond to events, and bundle actions into packages.