Skip to content
Education & Software

How to Configure a Firewall on Ubuntu

In this article

Configuring a firewall on Ubuntu is primarily managed using Uncomplicated Firewall (UFW), a user-friendly interface for managing netfilter rules. By defining specific allow or deny policies for incoming and outgoing traffic, you secure your server against unauthorized access while ensuring that legitimate services remain accessible to your users and applications.

Understanding UFW and the Netfilter Framework

At the core of Ubuntu’s network security is the Linux kernel’s netfilter framework, which controls packet filtering and network address translation. While tools like iptables or nftables offer granular control, they possess steep learning curves. UFW was designed to simplify this by providing a command-line interface that translates human-readable commands into effective firewall rules. In 2026, UFW remains the standard tool for Ubuntu distributions, including Server and Desktop editions, offering a robust balance between simplicity and security for most environments.

Prerequisites and Initial Verification

Before configuring your firewall, you must ensure you have root or sudo-level access to your terminal. Begin by verifying whether UFW is already installed, which is the default for most modern Ubuntu releases. Check the status of the service using the command sudo ufw status. If the output indicates that the service is inactive, you are in a safe position to define your rules before enabling the firewall.

It is vital to consider your current connection method—usually SSH—before activating UFW. If you enable the firewall without explicitly allowing SSH traffic, you will lock yourself out of your remote server. Always define your allow rules for essential services before switching the firewall to an active state.

Establishing Essential Rules

The primary philosophy of a secure firewall is a "deny-by-default" approach, meaning all incoming traffic is blocked unless explicitly permitted. To implement this, first set the default policies. Run sudo ufw default deny incoming and sudo ufw default allow outgoing. These commands ensure that your machine remains invisible to unsolicited inbound requests while permitting the server to initiate connections to the outside world, such as downloading updates or contacting package repositories.

Once the defaults are established, you must open specific ports for the services you intend to host. The most common requirement is SSH access, which typically operates on port 22. To prevent lockout, run sudo ufw allow ssh or, if you use a non-standard port for added security, sudo ufw allow 2222/tcp. You may also need to allow HTTP and HTTPS traffic for web servers, which can be done by running sudo ufw allow http and sudo ufw allow https.

Comparing Rule Configuration Methods

Managing your firewall effectively requires choosing the right syntax for your specific use case. The following table provides a quick reference for common UFW rule implementations.

Goal Command Scope
Allow SSH access sudo ufw allow ssh Permits inbound SSH traffic
Allow Web traffic sudo ufw allow 80,443/tcp Permits standard HTTP/HTTPS
Restrict by IP sudo ufw allow from 192.168.1.10 Limits access to a specific IP
Deny specific port sudo ufw deny 25/tcp Blocks SMTP traffic
Remove a rule sudo ufw delete allow 80 Removes an existing rule

Enabling and Verifying the Configuration

After defining your rules, use sudo ufw enable to start the service and load the ruleset. You will be prompted to confirm, as this action may disrupt existing connections if you have not properly configured the SSH rule. Once enabled, use sudo ufw status verbose to review your active configuration. The verbose output is highly recommended as it displays not only which ports are open but also the logging level and the status of IPv6 support.

If you make a mistake or need to adjust your configuration, UFW allows for simple modifications. You can delete rules by their numeric identifier or by repeating the allow command with the delete prefix. For instance, sudo ufw status numbered will list your rules with index numbers, which you can then pass to sudo ufw delete [number] to safely remove them.

Troubleshooting Common Connectivity Issues

If you find that services are unreachable after configuring the firewall, the first step is to verify that the application itself is listening on the expected interface. Sometimes, a service may be bound to the loopback address (127.0.0.1) rather than the server's public IP, rendering firewall rules irrelevant. Use ss -tunlp to inspect active listening ports.

If the application is correctly bound, check the UFW logs. By default, UFW logs are sent to /var/log/ufw.log. Increasing the logging level with sudo ufw logging medium can provide deeper insight into blocked packets. If you are experiencing complex issues where rules seem to conflict, the most effective troubleshooting step is to reset the firewall to its factory state using sudo ufw reset and proceed with a fresh configuration. This command disables the firewall and reverts all policies to the original default, providing a clean slate for debugging.

Advanced Filtering and Geolocation

For power users or those managing production environments, standard port filtering may not be enough. UFW supports limiting connection rates to prevent brute-force attacks on SSH. By using sudo ufw limit ssh, UFW will deny connections from an IP address that has attempted to initiate six or more connections within a 30-second window. This is a critical security layer for servers exposed to the public internet.

Additionally, while UFW does not support complex geographic blocking out of the box, you can achieve this by integrating external scripts that pull IP ranges from regional databases and apply them using UFW’s rule syntax. However, for most users, strict adherence to the principle of least privilege—opening only the ports absolutely necessary for your application—remains the most effective strategy for maintaining a secure and performant Ubuntu environment.

Frequently Asked Questions

Is UFW active by default on Ubuntu?

No, UFW is pre-installed on almost all Ubuntu distributions, but it is disabled by default. You must explicitly run the command to enable the firewall after you have defined your desired inbound and outbound rules to ensure the system is protected immediately upon activation.

Will enabling UFW disconnect my current SSH session?

If you have not explicitly allowed SSH traffic using sudo ufw allow ssh before running sudo ufw enable, you risk being disconnected. Always run the allow command for your SSH port first to ensure that your active remote management connection is whitelisted in the firewall configuration.

How do I allow traffic from a specific IP address only?

To restrict access to a specific service for a single trusted source, use the syntax sudo ufw allow from [IP_ADDRESS] to any port [PORT]. For example, sudo ufw allow from 203.0.113.5 to any port 22 ensures that only the machine at that specific IP can reach your SSH service.

Can I use UFW to block traffic from a specific country?

UFW does not have native, built-in support for country-based filtering. To block entire geographic regions, you would need to use more advanced tools like ipset combined with iptables or a dedicated software firewall appliance, as maintaining an up-to-date list of all IP ranges for a country is too complex for manual UFW rules.

What is the difference between UFW and iptables?

UFW is a high-level management tool that provides an easy syntax for users to control netfilter. Iptables is a lower-level, complex command-line tool that interfaces directly with the netfilter kernel modules. You should use UFW for standard configuration; using both tools simultaneously can lead to rule conflicts and unpredictable firewall behavior.

Recommended Next Steps for System Security

Once your firewall is configured, prioritize hardening your SSH service by disabling root login and enforcing key-based authentication. Ensure that you have a routine for monitoring your system logs using tools like fail2ban, which works alongside UFW to dynamically ban IP addresses showing malicious behavior. Regularly audit your open ports using sudo ufw status numbered to ensure no obsolete services remain exposed to the network.