Marcio Cunha

How to configure UFW firewall rules on Ubuntu Linux

Learn how to secure your Ubuntu server by configuring UFW practically. Discover how to open essential ports and block unwanted access.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • UFW simplifies iptables management on Linux through intuitive and straightforward text commands
  • Setting default incoming denial policies is the foundation of any robust security strategy
  • Opening essential services like SSH and HTTP ensures operation without exposing the system
  • Regularly checking firewall status prevents silent network connectivity failures over time
  • Integrating UFW with complementary tools strengthens the overall defense of cloud servers

Why do you need a firewall on your Ubuntu server?

When deploying a server to the internet, whether in the cloud or on local hardware, we open a direct door to the world. Any computer connected to the network can attempt to communicate with the services running there, from legitimate web apps to malicious bots searching for vulnerabilities. In practice, this means leaving ports open without control is like leaving your front door unlocked. To prevent intrusions, we use a firewall, which acts like a strict doorman deciding who gets in and who stays out.

In the Linux ecosystem, the standard packet filtering framework is iptables, but configuring it directly can be complex and full of confusing syntaxes. That is precisely where UFW comes in, standing for Uncomplicated Firewall. It acts as a friendly layer on top of iptables, allowing administrators to create clear and objective rules without having to memorize long commands or complex routing rules.

Before tweaking rules, it is worth remembering that system security relies on multiple fronts, including proper hardware resource monitoring. Just as we track system health as discussed in the guide on how to monitor RAM memory on Linux via terminal using free and top, keeping network traffic under control is a vital routine for the stability of any modern infrastructure.

Understanding the basic operation of UFW

UFW operates based on default policies, meaning general rules that determine what happens to any traffic that does not fit a specific exception. The standard security recommendation on servers is to block absolutely all incoming connections while allowing all outgoing traffic. In practice, this prevents outside entities from initiating conversations with your server, while letting your server fetch internet updates or query external APIs smoothly.

Another fundamental concept involves ports and protocols. On a network, applications listen on specific numeric ports, such as port 22 for secure terminal connections or port 80 for traditional web pages. Additionally, data travels using communication protocols like TCP, which guarantees ordered packet delivery, or UDP, focused on speed for streaming and quick lookups. UFW allows filtering traffic by combining these exact elements: allowing or blocking a specific port using a designated protocol.

Step-by-step guide to installing and enabling UFW

In many Ubuntu distributions, UFW comes pre-installed by default, but it might be disabled to prevent accidental lockouts during the operating system setup. To ensure everything is ready on your machine, the first step is checking package presence and performing the initial activation safely.

  1. Update the package list and install UFW if it is not already present on the system:
    sudo apt update && sudo apt install ufw -y
  2. Set the default policy to block all incoming traffic and allow all outgoing traffic:
    sudo ufw default deny incoming && sudo ufw default allow outgoing
  3. Enable the firewall on the system and confirm the operation to ensure rules take effect immediately:
    sudo ufw enable

An essential precaution before running the activation command is ensuring remote terminal access is not cut off. If you are configuring the server remotely and close the terminal port before allowing access, you will lose control of the machine. Therefore, always authorize remote access before flipping the main firewall switch.

How to open essential ports for web services and SSH

With the firewall active and blocking everything by default, we need to open specific paths for services that truly require external access. The classic example is the remote administration port, ensuring you can continue managing the server. If you want to explore advanced protection scenarios for this specific service, check out the recommendations on how to block SSH brute force attacks with UFW.

To allow secure remote access, we run the command authorizing traffic on the corresponding port. Similarly, if your server hosts a website, you will need to open standard web ports so visitors can load pages in their browsers. Check out these practical commands for common administrative tasks:

sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

Besides using known service names recognized by the system, you can also specify exact port numbers, which is very useful for custom applications, exposed databases, or game servers. For instance, to open port 8080 for an application in development, simply type the corresponding command providing the desired number and protocol.

Checking status and managing active rules

After configuring initial permissions, it is essential to verify if everything was applied correctly and check the current state of rules in the system. UFW offers simple commands to display the control panel in a tabular format, showing precisely which ports are open and which IP addresses have special access privileges.

sudo ufw status verbose

This command outputs a comprehensive report containing the current default policy, activated rules, and whether the firewall is active or inactive. If at any point you need to remove a rule added by mistake, simply repeat the original command replacing the word allow with delete, ensuring the environment remains clean and secure.

Final Considerations

Configuring a firewall on Ubuntu with UFW is one of the most important steps to ensure the integrity and security of any digital infrastructure. Throughout this article, we explored how simple default denial policies combined with surgical port opening form a solid barrier against unwanted intrusions. Maintaining this operational discipline protects your data and ensures services run without unforeseen interruptions.

Keep in mind that technology security is not a one-time event, but an ongoing process of monitoring and adjustments. Perform periodic reviews of your server rules, remove permissions for unused ports, and closely monitor system logs to identify suspicious behavior before it turns into a real problem.