Ubuntu: How to open/close a port in firewall (UFW)

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Managing network ports is a fundamental aspect of securing a Linux server. In Ubuntu, the Uncomplicated Firewall (UFW) provides a user-friendly way to configure your firewall settings. This tutorial walks you through the process of opening and closing ports on your Ubuntu system using UFW, with a step-by-step approach that ranges from basic to advanced configurations.

Prerequisites

  • An Ubuntu server or desktop
  • Access to a user account with sudo privileges
  • UFW installed (usually installed by default on Ubuntu)

Basic Commands to Manage Ports with UFW

Before making any changes, it’s good practice to check the status of your UFW firewall. Use the following command:

sudo ufw status verbose

If UFW is inactive, you can enable it with the command:

sudo ufw enable

To open a port, you need to allow traffic through it. For example, allowing traffic through port 22 (SSH) can be done with the command:

sudo ufw allow 22

If you wish to close the port, you can deny traffic through it like so:

sudo ufw deny 22

Allowing Specific Port Ranges and Protocols

To allow a range of ports, for instance, ports 3000-3005, use:

sudo ufw allow 3000:3005/tcp

If you want to specify the protocol for a single port, you can add it at the end of your command:

sudo ufw allow 53/udp

Using UFW with IPv6

By default, UFW is configured to support IPv6. To allow a port for both IPv4 and IPv6, the commands are the same:

sudo ufw allow 80

If you specifically want to deny a port for IPv6, you can directly specify this:

sudo ufw deny 80 from any to any proto tcp

Advanced UFW Configuration

For more advanced scenarios, you might want to specify a particular IP address for the rule. For example, to allow access to port 22 only from the IP 192.168.1.100, you can do:

sudo ufw allow from 192.168.1.100 to any port 22

To allow traffic to a specific network interface, such as eth0, you could specify:

sudo ufw allow in on eth0 to any port 80

Managing UFW Rules

You can list all the current UFW rules with the command:

sudo ufw status numbered

To remove a rule by its number, you can do the following. For example, to remove the rule number 2:

sudo ufw delete 2

Remember to reload the firewall for changes to take effect:

sudo ufw reload

Configuration with Configuration Files

For those who prefer to manually edit configuration files, UFW keeps its main configuration in /etc/default/ufw. To apply rule sets consistently across reboots, you can edit the /etc/ufw/before.rules and /etc/ufw/after.rules files.

Be cautious when manually editing configuration files, as incorrect settings can lock you out of your system.

Logging and Monitoring

UFW provides an easy way to enable or disable logging:

sudo ufw logging on
sudo ufw logging off

To monitor live firewall logs, you can use the command:

sudo tail -f /var/log/ufw.log

Using UFW with Application Profiles

UFW supports application profiles. If an application includes a UFW profile, you can allow or deny it using its profile name. To check available profiles, use:

sudo ufw app list

To allow an application called ‘Apache,’ do the following:

sudo ufw allow 'Apache'

Troubleshooting and Tips

If you encounter issues, you can reset UFW to its default settings by using:

sudo ufw reset

To ensure you don’t get locked out during configuration, UFW includes the ‘ufw limit’ command, which you can use to limit connections to a service and help prevent brute-force attacks:

sudo ufw limit 22

As a tip, when configuring your firewall, always make sure you have another means of accessing your server, such as physical access or an out-of-band management interface.

Conclusion

In this guide, we’ve seen how to manage ports in Ubuntu’s Uncomplicated Firewall, from the basics of opening and closing ports to more advanced examples. Knowing how to properly manage your firewall is key to maintaining server security and ensuring authorized traffic flows properly.