LinuxPublished on July 24, 20267 min read

How to set up the UFW firewall on Ubuntu or Debian

UFW (Uncomplicated Firewall) is the easiest Linux firewall to set up on a Debian or Ubuntu VPS. It drives the kernel's packet filter, iptables or nftables, with readable commands such as ufw allow 443. This guide shows how to install it, open the ports you need without cutting off your SSH access, then deal with Docker.

The client area has no network firewall for VPS. Ports are closed and opened inside the operating system, with UFW. The principle is always the same. Everything coming in is refused by default, then each exposed service gets its own exception.


Install UFW on Ubuntu and Debian

This guide applies to Ubuntu 22.04 and 24.04 and to Debian 12 and 13, with the root account or a sudo user. UFW is often already there on Ubuntu. On Debian, you need to install it.

sudo apt update
sudo apt install -y ufw
sudo ufw status

The answer Status: inactive is normal. You set the rules first and enable the firewall afterwards.


Block incoming traffic and allow SSH

Start with the basic rule. Everything coming in is refused, everything going out is allowed.

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allow SSH before you enable UFW, never after. Otherwise enabling it drops your connection and locks you out.

The OpenSSH profile opens port 22. You can also give the port itself.

sudo ufw allow OpenSSH
sudo ufw allow 22/tcp

Does your SSH listen on another port? Replace 22 with that port, for example sudo ufw allow 2222/tcp. Check it before enabling the firewall, because the Port directive may sit in a file under /etc/ssh/sshd_config.d/.

sudo sshd -T | grep '^port '

Enable UFW and check the rules

sudo ufw enable

UFW warns that the command may disrupt existing SSH connections. Since SSH is allowed, answer y. Your session stays open.

The first command below shows the detailed status, with the default policies and logging. The second one numbers the rules, which you need to delete one.

sudo ufw status verbose
sudo ufw status numbered

The numbered list looks like this.

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 80/tcp                     ALLOW IN    Anywhere
[ 3] 443/tcp                    ALLOW IN    Anywhere

UFW also applies its rules to IPv6 when /etc/default/ufw contains IPV6=yes, the default value. Open rules then appear a second time, followed by (v6).

Log refused connections

sudo ufw logging on

The level goes from low to full. For a production server, low is enough and does not flood the logs. On Ubuntu, entries go to /var/log/ufw.log. On Debian 12 and 13, which have no rsyslog by default, read them in the kernel journal.

sudo journalctl -k -g UFW

Open ports for your services

Open one port for each service you expose. Give the protocol when you know it. SSH and the web use TCP, while a WireGuard VPN or many games use UDP, for example sudo ufw allow 51820/udp.

sudo ufw allow 80/tcp      # HTTP
sudo ufw allow 443/tcp     # HTTPS
sudo ufw allow 25565/tcp   # example, game server
sudo ufw allow 6000:6007/tcp

The last line opens a port range.

Open a port to a trusted address only

sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp

This rule opens MySQL (port 3306) to the address 203.0.113.10 only. It is the right approach for a database that must not be visible from the whole internet. A whole subnet is written in CIDR notation, such as 203.0.113.0/24.

Use application profiles

A profile groups the ports of a program. It appears when the program's package provides one. List the profiles on your system, then show the details of one.

sudo ufw app list
sudo ufw app info 'Nginx Full'
sudo ufw allow 'Nginx Full'

Nginx Full opens ports 80 and 443 at once. On Ubuntu, the Apache equivalent is called Apache Full. If a profile is missing from the list, open the ports by number. To set up the web server itself, follow the guide Install Nginx and PHP-FPM on a Linux VPS.

Allow, deny or reject

  • allow lets the connection through. Use it for your public services.

  • deny drops the packet without answering. A scan cannot tell whether the port is closed or filtered. This is the recommended behaviour on an exposed server.

  • reject blocks the connection and sends back a refusal. Useful for internal troubleshooting, less discreet against a scan.

On a public server, keep the deny incoming policy and add targeted allow rules.


Delete a rule or reset UFW

The safest method uses the rule number. You can also repeat the rule, preceded by delete.

sudo ufw status numbered
sudo ufw delete 2
sudo ufw delete allow 80/tcp

To turn the firewall off for a while, or to start from scratch, use these commands. reset deletes every rule.

sudo ufw disable
sudo ufw reset

After a reset, allow SSH again before you re-enable UFW.


Limit SSH connection attempts

The limit rule blocks for a while an address that opens too many connections. By default, the threshold is six connections within half a minute.

sudo ufw limit OpenSSH

This protection stays basic. To ban persistent addresses for longer, on SSH as on your other services, add Fail2ban with the guide Install and configure Fail2ban. UFW decides which ports are open, Fail2ban decides which addresses to block.

Next, the guide Set up an SSH key on a Linux VPS replaces the password with a key. The guide Secure a Linux VPS covers every step, from SSH to backups.


Docker and UFW

Docker bypasses UFW. When you publish a container port, Docker writes its own rules, which come before UFW's. The port is then open to the whole internet, even if ufw status shows it as blocked. The Docker documentation confirms it.

Publish the port on 127.0.0.1

A service behind a reverse proxy such as Nginx does not need to be public. Publish its port on the local address.

# Open to the whole internet, even if UFW refuses the port
docker run -d -p 8080:80 nginx

# Reachable from the VPS only, for example behind Nginx
docker run -d -p 127.0.0.1:8080:80 nginx

In a Compose file, the line becomes "127.0.0.1:8080:80" under ports:. The guide Install Docker on a Linux VPS covers installing Docker.

Filter public container ports with ufw-docker

For a port that must stay public, the open source project ufw-docker puts UFW back in charge. Add the block provided by the repository at the end of /etc/ufw/after.rules, then reload UFW. Ports published by Docker are then closed until you open them.

sudo ufw reload
sudo ufw route allow proto tcp from any to any port 80

This rule targets the container port, not the VPS port. For a container started with -p 8080:80, use 80. Until this is in place, treat every port published by Docker as open to the world.


If you are locked out

A forgotten rule can cut SSH. The Console button on the VPS page, in the client area, shows the server screen without going through the network.

  1. Click Console. The Open the remote console? window appears. Click Open the console.

  2. Log in with the user and password shown in the Password tab.

  3. Fix the rule, for example with sudo ufw allow OpenSSH, or turn the firewall off with sudo ufw disable.

Open the remote console window with the Open the console button

UFW does not protect against an attack that floods the network. That kind of attack is handled upstream, before it reaches the VPS. The guide How to manage your VPS from the client area covers the console and the other tools of the VPS page.


Need help?

Open a ticket from Tickets › Open a Ticket, or from the Contact support tile at the bottom of the VPS page. In the Related Service field, pick the VPS so the team saves time. You can also ask your question on the OuiHeberg Discord.

Was this article helpful?

298 views
Share this article
VPS Linux
Need a Linux VPS?

Root access, your choice of distribution and management from the OuiHeberg client area.

View plans →

Related Articles