Sign In
Sign In

How to Configure Uncomplicated Firewall (UFW) on Ubuntu 24.04

How to Configure Uncomplicated Firewall (UFW) on Ubuntu 24.04
JC Brian Refugia
Technical writer
Ubuntu Firewall
21.08.2024
Reading time: 8 min

Setting up a firewall is one of the fundamental steps in safeguarding an Ubuntu 24.04 installation. Security is an important part of running any server or system. A firewall restricts which services are permitted to communicate with the server, thus acting as a barrier between the system and any potential threats. On Ubuntu, the Uncomplicated Firewall (UFW) offers an intuitive frontend for controlling firewall rules. It makes setting up iptables, the robust but intricate underlying firewall mechanism, easier. UFW makes it simple to set up and manage their firewall settings, even if they have little experience with networking concepts. This increases system security without requiring a high level of technical expertise. With the help of this article, you will be able to install, configure, and manage UFW on Ubuntu 24.04, protecting the server from undesirable network traffic while maintaining the seamless operation of authorized services

Prerequisites

  • A local computer or a cloud server with Ubuntu 24.04 installed
  • Root access or user with sudo privileges

Installing UFW

On Ubuntu 24.04, installing the Uncomplicated Firewall (UFW) is straightforward. Normally, Ubuntu has it pre-installed, but if it's not, one may easily install it on the machine by following the instructions below:

  1. Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below: 

sudo apt update && sudo apt upgrade -y
  1. The below command can be used to install UFW on the machine.

sudo apt install ufw -y
  1. Once installation is complete, the default status of UFW is inactive. Check it using the command below:

sudo ufw status

Image1

Enabling UFW

You must enable UFW beforehand to begin securing the system. When UFW is enabled, it will begin enforcing both its default rules and any custom rules that have been established. If you are configuring a remote server, make sure to allow SSH connections before starting UFW, to avoid locking users out. Now that UFW is installed and operational, you can start creating firewall rules that meet the unique security needs. 

Enable UFW using the command below and type "y" to proceed.

sudo ufw enable

Image3

Allowing SSH Connections

Enabling the firewall before enabling SSH connections is crucial when deploying UFW on a server, particularly if if administering it remotely over SSH. SSH blocking may prevent you from accessing the server and render it unusable. 

On Ubuntu 24.04, you can enable SSH connections by following these steps:

  1. It is easy to permit SSH traffic using UFW. Use the following command to enable it.
sudo ufw allow ssh

Image9

  1. To confirm that the rule was successfully added, check the status of UFW by running thecommand below.
sudo ufw status

Image15

  1. If the SSH service is operating on a non-standard port, you can choose that port instead of the default by issuing the following command.
sudo ufw allow <custom_port>/tcp

Understanding UFW Default Policies

It's critical to learn about the default policies that UFW applies to incoming, outgoing, and forwarded traffic before digging into custom firewall rules. These default policies provide the firewall's baseline behavior, which can subsequently be adjusted with particular rules to permit or prohibit particular kinds of traffic. 

  1. UFW prevents all incoming connections to the server by default. This security precaution keeps unwanted users from accessing the system.  Any request that comes in from outside the system will be rejected unless a particular rule is configured to authorize it. Requests for SSH, FTP, HTTP, and HTTPS are included in this. 

  2. By default, UFW permits all outgoing connections. This implies that there are no restrictions on the connections the server can make to other servers or services. Because outgoing traffic is normally safe, this policy allows the server to access the internet, download updates, and connect to other services without requiring any additional settings.

  3. Since UFW's default forwarding policy is set to refuse, all forwarded traffic is blocked. Packets that are received by the firewall and subsequently forwarded to a different location are referred to as forwarded traffic. This is especially important for systems that serve as gateways or routers. The server must modify this policy if it is intended to forward traffic between networks.

Allowing and Denying Specific Ports and Services

The ability of UFW to simply control which ports and services are granted or denied access to the system is one of its main features. This feature is necessary to secure the Ubuntu 24.04 server and manage traffic. Using UFW, you can use this method to enable or block particular ports and services.

  1. To enable traffic on a specific port, run the ufw allow command followed by the port number. In this example, to allow http (port 80), run the command below.
sudo ufw allow 80/tcp

Image4

  1. To allow https (port 443), run the command below.
sudo ufw allow 443/tcp

Image20

  1. If a non-standard port is used by the application, the port can also specify using the command below.
sudo ufw allow <custom_port>/tcp

For example:

sudo ufw allow 3026/tcp

Image16

Traffic on port 3026, which is frequently used for database configurations, is now permitted.

  1. Likewise, in order to prevent access, you can restrict traffic on a particular port, run the command below.
sudo ufw deny 80/tcp

Image12

Checking UFW Status and Rules

After configuring Ubuntu 24.04's UFW (Uncomplicated Firewall), it's critical to frequently check the firewall's status and go over the rules that have been set up. By doing this, you can be sure the firewall is up and running as it should. To inspect the current firewall rules and verify the status of UFW, run the following command: 

sudo ufw status

Image2

Managing UFW Application Profiles

Predefined application profiles included in Ubuntu 24.04's UFW (Uncomplicated Firewall) make it easier to grant or restrict traffic for particular services. Firewall management is made easier by these profiles, which include preconfigured rules for popular services including SSH, POSTIFX, OPENSSH, HTTP, HTTPS, and others. Managing UFW application profiles can be done as follows.

  1. Use the following command to view every application profile that is available on the system.
sudo ufw app list

Image17

  1. Use the below command to view the rules included in a particular application profile. The ports and protocols that the profile controls will be displayed in the output.
sudo ufw app info <application_name>

For example:

sudo ufw app info OpenSSH

Image18

  1. The application profile that you wish to use can be allow with UFW once it has been identified. For instance, use the following command to enable HTTP traffic.
sudo ufw allow http

Image11

  1. Likewise, the deny command can be used to prevent traffic for a certain application profile. For example, use the following command to stop all communication related to the HTTP.
sudo ufw deny http

Image5

  1. If a rule related to an application profile is no longer required, it can be deleted using the following command:
sudo ufw delete allow http

Image6

Disabling UFW

Even though Ubuntu 24.04's UFW (Uncomplicated Firewall) is an effective tool for controlling firewall rules and system security, there may be circumstances in which it needs to be turned off, either permanently or temporarily. Unless another firewall or security solution is in place, disabling UFW will halt the firewall and erase all active rules, leaving the system unsecured.

  1. To disable UFW, run the below command. By using this command, you can successfully turn off the firewall and stop the UFW service. 
sudo ufw disable

Image8

  1. Use the command below to verify that UFW has been successfully disabled. The status output should show below.
sudo ufw status

Image14

Troubleshooting Common Issues

Although UFW (Uncomplicated Firewall) is meant to be user-friendly, there could be some problems with installation or firewall rule management. Here are some typical issues that may run into and solutions for issues.

  1. If UFW is not listed on the installed packed, try reinstalling it by running sudo apt update and sudo apt install ufw respectively.

  2. If the installation fails to install, look for issues in your sources or package management. You can also try apt update and apt clean.

  3. Conflicting firewall service. Before activating UFW, make sure that all other firewall services, such as firewall and iptables, have been stopped and deactivated. For further information, review the UFW logs if the issue continues. Useful command is sudo tail -f /var/log/ufw.log.

  4. Once UFW is enabled, you cannot connect remotely to the server and SSH access is restricted. If the you are locked out due to UFW blocking SSH, you need to access the server on the console and run command sudo ufw allow ssh to regain access.

Conclusion

In conclusion, one of the most important steps in protecting Ubuntu 24.04 is configuring the Uncomplicated Firewall (UFW), which controls inbound and outbound network traffic. Because of its intuitive interface, UFW makes firewall control simple enough even for individuals with little experience with Linux system administration. You can effectively control traffic to and from the server by following the instructions to install UFW, create default policies, allow or deny certain ports and services, and manage application profiles. Monitoring the firewall rules and UFW status on a regular basis guarantee that the system is always shielded from unwanted access. Whether it's administering a production environment or protecting a personal server, UFW offers a reliable and simple way to improve the security of the system. By adding the capability to diagnose common problems, one can keep the firewall configuration secure and effective for what is needed.

Ubuntu Firewall
21.08.2024
Reading time: 8 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us
Hostman's Support