Marcio Cunha

UFW on Ubuntu Server: How to Configure Ports, Rules and Security

Learn how to secure your Ubuntu Server by controlling network traffic with UFW in a simple, practical and straightforward way.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • Enabling UFW without prior remote connection authorization can completely lock the administrator out of the server
  • Default policies of denying incoming traffic and allowing outgoing traffic provide the baseline defensive posture for web servers
  • Creating rules restricted to specific IP subnets drastically reduces the attack surface in corporate environments
  • Using application profiles simplifies the management of standard ports for common software like web and database servers
  • Continuous auditing of system logs ensures early detection of potential intrusion attempts and port scanning activities

Understanding the role of UFW in Ubuntu Server security

When we place a server connected to the internet, we open an invisible door to the entire world. Any computer can attempt to talk to it, and those intentions are not always friendly. This is where the firewall comes in, acting like a digital traffic cop that decides who gets in and who stays outside. On Ubuntu Server, the standard tool for this job is UFW, which stands for Uncomplicated Firewall.

The great merit of UFW is translating complex network rules into simple commands that are easy to understand and type. Under the hood, it manages iptables, the native packet filtering system of the Linux kernel, without requiring you to memorize cryptic codes. In practice, configuring UFW means drawing virtual fences around your server, allowing only essential services to receive legitimate visitors.

Installation and current status verification

Before creating any rules, we need to ensure the tool is installed and ready for use. On most modern versions of Ubuntu Server, the package comes pre-installed, but it never hurts to confirm. For this, we use the terminal to verify the program's presence and its current operational state, discovering whether it is already blocking traffic or running completely wide open.

To check if the service is active, we run the command sudo ufw status in the terminal. If the response is inactive, it means your server is vulnerable to any external traffic arriving at network ports. Before activating protection, however, there is a critical step every administrator must take to avoid a digital disaster: ensuring remote access ports are not blocked in the process.

The essential SSH port precaution before activation

SSH, which stands for Secure Shell, is the protocol we use to access our server remotely from another computer. If you enable the firewall without telling it that SSH is welcome, the entrance door will be locked and you will lose access to the machine, requiring physical access or the cloud provider control panel to fix the mistake. In practice, this is the most common trap for beginners.

To avoid this unwanted scenario, we release remote access by typing sudo ufw allow ssh or specifying the exact port if we changed the default configuration, such as sudo ufw allow 22/tcp. By doing this, we instruct the firewall to carve out a specific exception for management traffic, ensuring our communication line remains intact as soon as general blocking takes effect.

Defining default traffic policies

A default policy defines the basic behavior of the firewall when no other rule applies to a specific situation. Think of this like a gated community rule: the front desk must bar everyone by default unless a resident explicitly authorizes entry. In server security, the most recommended strategy is to adopt a strictly defensive posture.

We configure this posture by applying the commands sudo ufw default deny incoming to reject anything trying to enter without permission, and sudo ufw default allow outgoing to allow the server to check for updates and talk to the internet freely. In practice, this means the server becomes completely invisible and shielded against arbitrary external attacks, while still having total freedom to download packages and send data outbound.

Allowing essential services like web servers

With basic security configured and remote access secured, the next step is opening the necessary ports for the server to fulfill its real-world purpose. If your machine is going to host a website, for example, it needs to accept connections on standard internet ports, which are port 80 for regular pages and port 443 for encrypted, secure pages.

We can open these ports using friendly names recognized by the system or directly by their numbers. The command sudo ufw allow 'Nginx Full' automatically opens both ports for those using the Nginx web server, while sudo ufw allow 80/tcp does the same thing manually. In practice, this flexibility allows tailoring the firewall precisely to the needs of the application running on the machine.

Controlling access by specific IP addresses

Sometimes, we do not want a service open to everyone on the internet, but only to a specific person or office. Imagine an internal administrative panel or a database that should only be accessed from your company network. UFW allows restricting rules not just by ports, but also by source IP addresses.

To allow only a specific IP to access the PostgreSQL database port, for example, we use a command formatted as sudo ufw allow from 192.168.1.50 to any port 5432. In practice, this creates an extra layer of isolation, ensuring that even if someone discovers the service password, the connection attempt is summarily ignored if it comes from an unauthorized address.

Enabling the firewall and validating rules

With all essential rules properly registered, the moment arrives to put the system into real operation. Activation is done via a simple command, but it requires heightened operator attention. The system will display a warning stating that the operation may interrupt existing SSH connections, reinforcing the importance of having carefully executed prior release steps.

We run the command sudo ufw enable and confirm the operation by typing 'y' when prompted. To verify everything was configured correctly, we use the command sudo ufw status verbose again, which will display a detailed list of all active rules, default policies, and the current state of the protection system.

Conclusion and network administration best practices

Configuring UFW on Ubuntu Server is a fundamental rite of passage for any professional looking to keep their computing environments secure and resilient. More than memorizing commands, the secret lies in adopting a least-privilege mindset, where everything is blocked by default and only what is strictly necessary receives network transit authorization.

Maintaining the habit of periodically reviewing active rules and monitoring system logs helps identify suspicious behaviors before they turn into severe incidents. With a solid packet filtering foundation, your server gains the robustness needed to operate on the modern internet without unpleasant surprises.