X
X
X
X

Knowledge Base

HomepageKnowledge BaseServer ManagementHow to Set Up a Firewall With UFW: ...

How to Set Up a Firewall With UFW: Open, Close and Delete Ports on Ubuntu

In short: UFW (Uncomplicated Firewall) is a tool for managing firewall rules with simple commands on Ubuntu and Debian servers. For a safe setup, first deny incoming traffic by default, then allow SSH, and only after that turn the firewall on with sudo ufw enable. You open a port with sudo ufw allow 443/tcp and delete a rule with sudo ufw status numbered followed by sudo ufw delete number.

What is UFW?

The Linux kernel filters incoming and outgoing packets with the netfilter framework; UFW is a front end on top of it that lets you write rules in plain language. On Ubuntu it is usually installed but turned off. We explain why a firewall should protect your server in what is a firewall. You run the commands below after connecting to your server over SSH; all of them need administrator rights, so they start with sudo.

UFW firewall: traffic from the internet passes through the rules; ports 22 and 80,443 allowed, 3306 and other incoming connections denied, sudo ufw allow OpenSSH in the terminal

1. Check the UFW status

sudo ufw status verbose

If the output shows Status: inactive, UFW is installed but off. If the command is not found, install it with sudo apt update && sudo apt install ufw. Before you start, listing which services listen on which ports, as shown in checking open ports with ss, makes it clear which ports you need to keep open.

2. Set the default policies

sudo ufw default deny incoming
sudo ufw default allow outgoing

With these two commands, every incoming connection is refused unless you explicitly allow it, while connections the server makes to the outside (updates, sending email, API calls) stay open. The firewall is still off at this point, so your connection is not affected.

3. Allow SSH

sudo ufw allow OpenSSH

OpenSSH is a ready-made application profile that opens port 22/tcp; sudo ufw allow 22/tcp does the same. If SSH runs on a different port, use that port instead, for example sudo ufw allow 2222/tcp. To slow down brute-force attempts you can use limit instead of allow. If you skip this step, your SSH connection drops the moment you turn on the firewall and you can only reach the server through your provider's console.

4. Turn on the firewall

sudo ufw enable

UFW warns that existing SSH connections may be disrupted; if you added the SSH rule, confirm with y. Then, without closing your current session, test that you can connect from a new terminal window. UFW also starts automatically when the server reboots.

5. Open ports for web and other services

sudo ufw allow 80,443/tcp

If you run a web server, this command opens the HTTP and HTTPS ports. You can see the ready-made profiles of installed packages with sudo ufw app list and allow them with, for example, sudo ufw allow "Nginx Full" or sudo ufw allow "Apache Full". For services such as a database that only one address should reach, limit the source instead of opening the port to everyone:

sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp

6. List and delete rules

sudo ufw status numbered

This command shows the rules with their numbers. To delete a rule, give its number: sudo ufw delete 3. You can also delete a rule by repeating it: sudo ufw delete allow 80/tcp. The numbers change after every deletion, so if you are removing several rules, list them again each time. A rule may appear on separate lines for IPv4 and IPv6; you need to delete both.

Common UFW commands

Command What it does
sudo ufw status verbose Shows the status, default policies and rules.
sudo ufw allow 443/tcp Allows incoming connections to a port.
sudo ufw deny 3306/tcp Blocks incoming connections to a port.
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp Allows a port only from a specific IP address.
sudo ufw limit OpenSSH Temporarily blocks an IP address that tries 6 or more connections within 30 seconds.
sudo ufw status numbered Lists the rules with their numbers.
sudo ufw delete 3 Deletes the rule with the given number.
sudo ufw reload Reloads the rules.
sudo ufw disable Turns the firewall off; rules are kept.

All options are covered in the ufw manual page and the Ubuntu firewall documentation.

Things to watch out for

  • If you use Docker: Docker writes iptables rules directly for published ports, and those ports can be exposed without passing your UFW rules. Bind container ports to the local address only, such as 127.0.0.1:8080:80, or restrict them as described in Docker's documentation.
  • Cloud firewall: If your provider's panel has a separate network firewall, the port must be open there too for traffic to reach the server.
  • Logs: sudo ufw logging on records blocked connections; you can see them in /var/log/ufw.log or with sudo journalctl -k | grep UFW.
  • AlmaLinux and Rocky Linux: These distributions use firewalld instead of UFW; rules are managed with the firewall-cmd command.

A firewall is only one of the steps for a new server. To handle it together with updates, SSH keys and Fail2ban, see our Linux server hardening article. If you have locked yourself out of the server, you can open a support ticket.

 

Frequently Asked Questions

My SSH connection dropped after enabling UFW. What should I do?

The firewall was turned on without an SSH rule. Connect through your provider's web console (VNC) and run sudo ufw allow OpenSSH, or turn the firewall off temporarily with sudo ufw disable.

Are UFW rules lost after a reboot?

No. A firewall enabled with sudo ufw enable and its rules are persistent; they load automatically when the server restarts.

Does UFW filter IPv6 traffic too?

Yes. On Ubuntu, the IPV6=yes setting in /etc/default/ufw is on by default, and the rules you add are also created for IPv6.

Can I delete all rules and start over?

The sudo ufw reset command turns the firewall off and deletes all rules. Then apply the steps in this article again, starting with the SSH rule.

Can't find the information you are looking for?

Create a Support Ticket
Did you find it useful?
(25 times viewed / 0 people found it helpful)

Powered by WISECP
Top