Complete Guide to Configuring the UFW Firewall on Ubuntu Server

Last update: 30/11/2026
Author Isaac
  • Implementation of a security policy based on total entry blocking and exit permission.
  • Detailed rule management for essential services such as SSH, HTTP and HTTPS using simple commands.
  • Security optimization through access restriction by specific IP addresses and log monitoring.

Systems engineer configuring firewall rules on a server using a laptop in a modern data center.

When putting a server online, it's crucial not to be blindly trusting. Even though GNU/Linux is often touted as a secure system, leaving ports open without control is like playing Russian roulette with security. If a vulnerable application allows malicious code to run, an attacker could create arbitrary sockets or establish reverse connections to external machines if there's no barrier to stop them.

To avoid these surprises, Ubuntu offers a tool called UFW (Uncomplicated Firewall). It's not a firewall in the strictest sense, but rather a simplified interface for managing netfilter and iptables, which are the ones that actually do the dirty work in the Linux kernel. Essentially, UFW translates simple commands into complex packet filtering rules so you don't have to get bogged down in the technical syntax of iptables.

Fundamental concepts of traffic filtering

Close-up of a server rack with blue lighting, representing the physical infrastructure protected by the UFW firewall.

A firewall acts like a guard, examining every data packet that attempts to enter or leave the system. Based on a set of rules, it decides whether the traffic passes through or is blocked. In UFW, we can define three main actions: allow , which permits the connection; deny , which silently blocks the traffic; and reject , which blocks the connection but notifies the sender that the request was discarded.

For a server to be truly secure, the ideal approach is to apply the principle of least privilege. This involves configuring strict default policies : blocking all incoming traffic and allowing all outgoing traffic. In this way, the server can update itself and browse the web, but no one can access it unless we have specifically granted permission.

  Linux Security: A Complete Guide to Firewalls and Network Rules

First steps: Installation and basic policies

Overview of a server room with network equipment illuminated in blue, ideal for contextualizing the Ubuntu Server guide.

The first step is to ensure that the tool is installed on the system using the command sudo apt install ufwBefore turning on the power switch, it is vital to configure the basic rules to avoid being locked out of the server, especially if we are working remotely.

To establish the security foundation, we executed sudo ufw default deny incoming to close all entries and sudo ufw default allow outgoing so that outgoing traffic flows smoothly. If at any point we mess up and want to erase everything to start from scratch, the command sudo ufw reset It saves our skin resetting the settings factory.

Rule management for common services

Computer screen with cybersecurity interfaces in shades of green, illustrating firewall log monitoring and management.

Once the wall is up, we need to make room for the services we actually need. For remote administration, it's essential to open port 22 using sudo ufw allow ssh o sudo ufw allow 22/tcpIf you have a web server, you will need to authorize HTTP and HTTPS traffic with sudo ufw allow http y sudo ufw allow httpswhich will automatically open the ports 80 and 443 respectively.

If we want to be more meticulous, we can define rules based on IP addresses. For example, if we only want our office team to access via SSH, we use sudo ufw allow from 192.168.1.1 to any port 22 proto tcp. This ability to segment access by address That's what differentiates a basic configuration from a professional one.

Advanced cases: Docker and Databases

Person wearing a mask interacting with network cables on a server, symbolizing security risks and open port attacks.

The world of containers can be treacherous. By default, Docker often bypasses UFW rules by directly modifying iptables. To prevent containers from being exposed, it is necessary adjust the bridge network from Docker. A useful command to allow web traffic to a container is sudo ufw allow in on docker0 to any port 80.

Regarding databases like MySQL or PostgreSQL, the golden rule is never expose them to the internetThey should only accept connections from the application server's IP address or from the internal network, using commands like sudo ufw allow from 192.168.10.0/24 to any port 3306 proto tcp to restrict access to the local subnet.

  Complete tutorial of the gcc command and its key options

Monitoring, logging, and troubleshooting

To check if everything is in order, the command sudo ufw status verbose It gives us a complete picture of the current state. If we need to manage legacy rules, sudo ufw status numbered It allows us to see a numbered list so we can delete a specific rule using sudo ufw delete [número].

Activity logging is key to detecting attacks. We can enable logging with sudo ufw logging onThese files, normally saved in /var/log/ufw.log, allow us analyze intrusion attempts or discover why a legitimate service isn't working. If the system becomes unstable, we can always use sudo ufw disable to temporarily disable the firewall and rule out that it is the cause of the problem.

Graphic alternatives and tools

Although the command line is the most efficient method, graphical interfaces like GUFW or FrontEnds are available for those who prefer not to type commands. GUFW is the most popular option and can be installed with sudo apt install gufwprovided that UFW is already installed on the system. These tools are basically wrappers that facilitate the visual management of rules for the average user.

For those who need precise control or work with other distributions, there are alternatives such as firewalld (common in Red Hat) or nftables, the modern framework that replaces iptables. While UFW is ideal for quick configurations, nftables allows for advanced packet filtering and NAT with superior performance.

Maintaining a secure server requires a balance between accessibility and security. By blocking all incoming traffic by default and opening only the strictly necessary ports, restricting access to sensitive services by IP address, and monitoring activity logs, we drastically reduce the attack surface. This strategy, combined with proper container and database management, forms the basis of a robust and resilient Linux infrastructure.

FAQ

What happens if I activate UFW without having allowed the SSH port?
What is the difference between denying and rejecting a connection in UFW?

Deny ignores the request completely without sending a response, while reject blocks access but sends a notification to the requester informing them that the connection was dropped.

How can I delete a specific rule that I've already created?

The numbered status command must be executed to identify the rule index, and then the delete instruction followed by the corresponding number must be used.

Is UFW compatible with Docker containers?

Yes, it is compatible, although Docker modifies iptables directly and can bypass UFW rules, so it is necessary to configure specific rules for the docker0 network interface.

Is it possible to restrict access to a port to only one IP address?

Yes, UFW allows you to create detailed rules specifying the source IP address, destination port, and specific protocol to authorize only that user.

Where are UFW activity logs stored?

Information about allowed and blocked connections is usually saved in the /var/log/ufw.log file once the logging function is active.

Isaac

Isaac

Passionate writer about the world of bytes and technology in general. I love sharing my knowledge through writing, and that's what I'll do on this blog, show you all the most interesting things about gadgets, software, hardware, tech trends, and more. My goal is to help you navigate the digital world in a simple and entertaining way.