Configuring a firewall is a crucial step in securing your Ubuntu system. The Uncomplicated Firewall (UFW) is a popular choice among Ubuntu users due to its simplicity and effectiveness. In this guide, we will walk you through the process of configuring UFW on your Ubuntu system.
What You’ll Need
- Ubuntu system (version 18.04 or later)
- Root access or sudo privileges
- Basic knowledge of Linux commands
Step 1: Install UFW
To start using UFW, you need to install it first. Open a terminal and run the following command: sudo apt update && sudo apt install ufw. This command updates your package list and installs UFW.
Step 2: Enable UFW
After installation, you need to enable UFW. You can do this by running the command sudo ufw enable. You will be warned that this may disrupt existing SSH connections. If you are connected remotely, make sure you have a way to regain access.
Step 3: Set Default Policies
UFW allows you to set default policies for incoming and outgoing traffic. To deny all incoming traffic and allow all outgoing traffic, run the following commands: sudo ufw default deny incoming and sudo ufw default allow outgoing.
Step 4: Allow SSH Connections
To avoid locking yourself out, especially if you are connected via SSH, you need to allow SSH connections. Run the command sudo ufw allow ssh or sudo ufw allow 22 to allow traffic on the default SSH port.
Step 5: Allow HTTP and HTTPS Traffic
If your server is a web server, you will need to allow HTTP and HTTPS traffic. You can do this by running the commands sudo ufw allow http and sudo ufw allow https.
Step 6: Enable and Reload UFW
After setting your rules, you need to reload UFW to apply the changes. First, enable UFW if it’s not already enabled: sudo ufw enable. Then, reload UFW with the command sudo ufw reload.
Step 7: Check UFW Status
To verify that UFW is enabled and to see the current set of rules, run the command sudo ufw status. This command will display the current status of UFW and all the rules that are currently applied.
Common Mistakes to Avoid
One of the most common mistakes is enabling UFW before allowing SSH connections, which can lock you out of your server if you are connected remotely. Always ensure you have allowed SSH connections before enabling UFW. Another mistake is not reloading UFW after making changes to the rules.
Tips and Tricks
Regularly review your UFW rules to ensure they are up-to-date and aligned with your system’s requirements. You can also use sudo ufw limit ssh to limit SSH connections and prevent brute-force attacks.
Frequently Asked Questions
What is UFW?
UFW stands for Uncomplicated Firewall. It is a firewall configuration tool for Ubuntu and other Debian-based systems, designed to be easy to use.
How do I disable UFW?
You can disable UFW by running the command sudo ufw disable. This will disable the firewall but keep the rules intact for when you decide to enable it again.
Can I use UFW on other Linux distributions?
While UFW is primarily designed for Ubuntu and Debian, it can be used on other Linux distributions with some modifications. However, the simplicity and integration that make UFW appealing on Ubuntu might be lost on other distributions.
Conclusion
Configuring UFW on Ubuntu is a straightforward process that significantly enhances the security of your system. By following the steps outlined in this guide and being mindful of common pitfalls, you can effectively manage your firewall rules and protect your server from unauthorized access.
Photo by Zulfugar Karimov on Unsplash






