How to Set Up and Configure a UFW Firewall on Ubuntu Linux

If you are running a server or just using a desktop machine on an untrusted network, securing your system is an absolute priority. One of the best ways to improve Ubuntu Linux security is by enabling a firewall. Fortunately, Ubuntu comes pre-installed with UFW (Uncomplicated Firewall), which is specifically designed to make managing your firewall rules as easy as possible. In this guide, you will learn how to set up and configure UFW in minutes.

How to Check Your Firewall Status

Before you make any changes, you should check if your firewall is already active. Open your terminal and run the following command:

sudo ufw status

If UFW is inactive, the terminal will simply return Status: inactive. If it is active, you will see a list of your current networking rules.

How to Allow Ports and Enable UFW

By default, when you enable the UFW firewall on Ubuntu, it blocks all incoming connections and allows all outgoing connections. Warning: If you are connected to an external server via SSH, you must allow SSH connections before enabling the firewall, otherwise you will lock yourself out!

  1. To allow standard SSH connections (Port 22), run: sudo ufw allow ssh
  2. If you are running a web server and need to allow standard HTTP (Port 80) and HTTPS (Port 443) traffic, run: sudo ufw allow http and sudo ufw allow https
  3. Once you have allowed your required connections, enable the firewall by running: sudo ufw enable

You can check the updated status and active rules anytime by running sudo ufw status verbose.

Frequently Asked Questions

How do I delete a UFW rule?

The easiest way to delete a rule is by finding its assigned number. Run sudo ufw status numbered. Find the number of the rule you wish to remove, and type sudo ufw delete [number] (for example, sudo ufw delete 3).

How do I completely disable the UFW firewall?

If you need to temporarily turn off the firewall for troubleshooting, run sudo ufw disable. This will leave your rules intact so they are ready when you re-enable it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.