Logo

How to Set Up a Firewall with UFW on Ubuntu 22.04

Nov 17, 2023

Setting up a firewall with UFW (Uncomplicated Firewall) on Ubuntu 22.04 is a straightforward process. UFW is a user-friendly interface for managing iptables, the default firewall management tool on Linux. Here's a step-by-step guide:

Step 1 : Install UFW, If UFW is not installed on your system, you can install it by running:

sudo apt update
sudo apt install ufw

Step 2 : Check UFW Status : Before enabling the firewall, it's a good idea to check the status of UFW to ensure it's inactive:

sudo ufw status

Step 3 : Allow SSH Access : If you are connected to your server via SSH, make sure to allow SSH traffic to avoid getting locked out:

sudo ufw allow ssh

Replace "ssh" with the port number if you are using a custom SSH port.

Step 4 : Allow Other Necessary Services

Allow other services that your server needs. For example, if you are running a web server, you might need to allow HTTP (port 80) and HTTPS (port 443):

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Step 5 : Enable UFW

sudo ufw enable

You will be prompted to confirm the action. Type "y" and press Enter.

Step 6 : Check UFW Status Again

Verify the status of UFW to ensure that it is active and that the rules are applied:

sudo ufw status

Step 7 : Additional Configuration (Optional)

You can further customize your firewall rules based on your specific needs. For example, you might want to allow or deny traffic on specific ports.

Step 8 : Disable UFW (if needed)

If you ever need to disable UFW temporarily, you can do so with the following command:

sudo ufw disable

Remember to re-enable it when you are done:

sudo ufw enable

That's it! You've successfully set up a basic firewall using UFW on Ubuntu 22.04. Adjust the rules according to your specific requirements.

Recommended