Marcio Cunha

How to configure firewall rules with UFW on Linux

Learn how to easily secure your Linux server using UFW. Manage ports and network connections effortlessly with straightforward terminal commands.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • UFW simplifies Linux firewall management without requiring deep iptables knowledge.
  • Setting default policies to deny incoming traffic and allow outgoing traffic builds a secure baseline.
  • Opening specific ports for essential services like SSH guarantees remote access and functionality.
  • Detailed inspection of active rules prevents unintended configuration mistakes.
  • Real-time network traffic monitoring complements the static defense provided by firewalls.

What is UFW and why use it on Linux

Managing server security can feel daunting for beginners, but modern tools make the process accessible. UFW, which stands for Uncomplicated Firewall, is a user-friendly frontend for the Linux kernel packet filtering system known as iptables. In practice, it acts like an intelligent doorman that decides which network connections can enter or leave your machine, translating complex rules into easy-to-understand commands.

Instead of dealing directly with long and confusing syntax for raw network rules, UFW lets you protect ports and IPs with just a few words. This is essential both for experienced administrators looking for speed and enthusiasts building home servers. Before applying blocking rules, keep in mind that incorrect configurations can lock you out of remote access, requiring caution and prior planning for essential ports.

To better understand your server's network ecosystem, you can combine firewall usage with diagnostic tools. For instance, learning how to monitor Linux network traffic in real time with bpfTOP helps identify suspicious connections instantly, complementing the static barrier created by UFW with a dynamic view of circulating packets.

Installation and initial status check

Most Debian and Ubuntu-based Linux distributions include UFW by default, but it is wise to ensure the package is present and up to date. If it is not installed on your system, the process requires only a quick terminal command using your distribution's standard package manager. Keeping the tool updated ensures security patches and better network service stability.

The first practical step before enabling the firewall is checking the current service status to see if it is already blocking or allowing connections. A disabled firewall means all ports on your server are vulnerable to external scans and automated intrusion attempts. To run these initial checks safely, follow the sequence below directly in your Linux server terminal:

  1. Install the UFW package if it is not already present on your system using
    sudo apt update && sudo apt install ufw -y
  2. Verify whether the service is active or inactive by running
    sudo ufw status verbose
  3. Set default security policies to block all incoming traffic and allow outgoing traffic using
    sudo ufw default deny incoming && sudo ufw default allow outgoing

These initial steps ensure the server adopts a defensive default posture, allowing only strictly necessary traffic across the network barrier. From this point on, any new service you install will require explicit permission to be accessed externally.

How to allow and block ports and IP addresses

With the firewall installed and configured with the default deny policy, the next step is opening ports for the services you want to expose. For example, if your server needs to accept remote SSH connections, the default port 22 must be opened immediately to prevent losing machine access after enabling UFW. You can open ports either by their number or the corresponding service name.

Beyond individual ports, UFW lets you restrict access to specific IP addresses or entire subnets, greatly enhancing security in corporate environments or database servers. If a malicious IP begins making repeated intrusion attempts, you can block it with a simple denial command. This granular control turns the firewall into a surgical tool for infrastructure protection.

To further deepen connectivity troubleshooting and ensure opened ports are responding correctly, it is worth complementing your skills by learning how to inspect network packets with tcpdump on Linux. This deep inspection helps confirm that packets reach their expected destination after passing through UFW rules.

Saving, enabling, and testing applied rules

After configuring all necessary permissions for web applications, databases, and administrative access, the moment arrives to enable the firewall permanently. The activation command immediately applies runtime rules and configures the system to start UFW automatically whenever the server reboots. It is crucial to perform this activation carefully to avoid locking yourself out, especially when managing the server remotely.

Following activation, the detailed status command displays the complete list of active rules, showing exactly which ports are open and which addresses hold special privileges. Testing external access from another machine on the network is the best way to validate if the firewall behaves as expected. Should any issues occur, UFW allows temporarily disabling the system easily for emergency fixes.

Maintaining a secure server requires continuous maintenance routines and old log cleanup to prevent excessive disk consumption. In environments where multiple services generate constant logs, managing disk space is just as important as configuring the firewall, as practically addressed when you manage container logs with Logrotate on Linux, ensuring the system remains stable and auditable long-term.

Final considerations on Linux network security

Configuring firewall rules with UFW is one of the most efficient and direct practices to shield Linux servers against unwanted access and automated internet attacks. By establishing strict default denial policies and allowing only what is strictly necessary, you drastically reduce your IT infrastructure attack surface. The tool's simplicity does not compromise its robustness, making it ideal for both beginners and experienced system administrators.

Success in network administration relies on a preventive mindset, where constant monitoring and periodic security rule reviews walk hand in hand. Documenting open ports and understanding data flow on your server guarantees smooth maintenance and high availability for users. With UFW properly enabled, your architecture gains a solid layer of protection against everyday threats.