How to Configure Uncomplicated Firewall (UFW) on Ubuntu 24.04
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 virtual 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.
Hostman offers a cloud managed firewall that provides cutting-edge defense tailored for businesses of all sizes.
Prerequisites Copy link
- A local computer or a cloud server with Ubuntu 24.04 installed
- Root access or user with
sudoprivileges
Installing UFW Copy link
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:
-
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-
The below command can be used to install UFW on the machine.
sudo apt install ufw -y-
Once installation is complete, the default status of UFW is inactive. Check it using the command below:
sudo ufw status
Enabling UFW Copy link
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
Allowing SSH Connections Copy link
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:
- It is easy to permit SSH traffic using UFW. Use the following command to enable it.
sudo ufw allow ssh
- To confirm that the rule was successfully added, check the status of UFW by running thecommand below.
sudo ufw status
- 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>/tcpUnderstanding UFW Default Policies Copy link
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.
-
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.
-
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.
-
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 Copy link
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.
- To enable traffic on a specific port, run the
ufw allowcommand followed by the port number. In this example, to allow http (port 80), run the command below.
sudo ufw allow 80/tcp
- To allow https (port 443), run the command below.
sudo ufw allow 443/tcp
- If a non-standard port is used by the application, the port can also specify using the command below.
sudo ufw allow <custom_port>/tcpFor example:
sudo ufw allow 3026/tcp
Traffic on port 3026, which is frequently used for database configurations, is now permitted.
- Likewise, in order to prevent access, you can restrict traffic on a particular port, run the command below.
sudo ufw deny 80/tcp
Checking UFW Status and Rules Copy link
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
Managing UFW Application Profiles Copy link
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.
- Use the following command to view every application profile that is available on the system.
sudo ufw app list
- 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
- 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
- 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
- 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
Disabling UFW Copy link
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.
- 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
- Use the command below to verify that UFW has been successfully disabled. The status output should show below.
sudo ufw status
Troubleshooting Common Issues Copy link
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.
-
If UFW is not listed on the installed packed, try reinstalling it by running
sudo apt updateandsudo apt install ufwrespectively. -
If the installation fails to install, look for issues in your sources or package management. You can also try
apt updateandapt clean. -
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. -
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 sshto regain access.
Conclusion Copy link
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.