Posted by

Set Up a UFW Firewall on Ubuntu Server Without Locking Yourself Out

A new server often listens on more ports than you think: a database left on its default port, an admin panel, a test service someone forgot. A host firewall makes sure only the services you chose are reachable from the network. On Ubuntu and Debian, the easiest way to manage it is UFW (Uncomplicated Firewall), a front end for the Linux kernel firewall.

The biggest risk with any firewall on a remote server is cutting off your own SSH session. This guide follows an order that avoids that: allow SSH first, check the rules, and only then switch the firewall on.

1. Install UFW and check its status

UFW is installed by default on Ubuntu but is inactive until you enable it. On Debian, install it first:

sudo apt update
sudo apt install ufw

Check the current state:

sudo ufw status verbose

On a fresh system you will see Status: inactive. Leave it that way until step 4.

IPv6 rules are handled automatically when IPV6=yes is set in /etc/default/ufw, which is the default on current Ubuntu releases. If your server has an IPv6 address, keep it on, or IPv6 traffic will not get the same protection.

2. Set safe default policies

The usual server setup is to block all incoming connections unless a rule allows them, and allow all outgoing connections:

sudo ufw default deny incoming
sudo ufw default allow outgoing

These commands only change the policy. Nothing is enforced yet because the firewall is still inactive.

3. Allow SSH before anything else

UFW ships application profiles for common services. The OpenSSH server installs one called OpenSSH:

sudo ufw app list
sudo ufw app info OpenSSH
sudo ufw allow OpenSSH

If SSH runs on a different port, the profile will not match. Allow the real port instead, for example:

sudo ufw allow 2222/tcp

If you always connect from a fixed address, such as an office IP or a VPN, you can allow SSH only from there. Replace the example address with your own:

sudo ufw allow from 203.0.113.10 to any port 22 proto tcp

Do this only if the address really is fixed. If it changes, you lose access.

4. Review the rules, then enable

UFW can show the rules you added while it is still inactive:

sudo ufw show added

Make sure your SSH rule is in the list. Before you enable the firewall:

  • Keep your current SSH session open.
  • Know how to reach the server another way, for example your hosting provider's web console or out-of-band access.

Then enable it:

sudo ufw enable

UFW warns that the command may disrupt existing SSH connections. Answer y. Now open a second terminal and start a new SSH connection. Close the first session only when the new one works. Once enabled, UFW also starts automatically at boot.

5. Open only the services you need

For a web server, allow HTTP and HTTPS. You can use port numbers or the profile your web server installs:

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

# or, with Nginx installed
sudo ufw allow 'Nginx Full'

# or, with Apache installed
sudo ufw allow 'Apache Full'

Services like databases should not be open to the whole internet. Allow them only from the servers that need them:

# PostgreSQL only from the private app subnet
sudo ufw allow from 10.0.0.0/24 to any port 5432 proto tcp

# MySQL only on the private network interface
sudo ufw allow in on eth1 to any port 3306 proto tcp

Check your interface name with ip addr; it is not always eth1. For a port range, you must give the protocol:

sudo ufw allow 6000:6007/tcp

6. Slow down SSH password guessing with limit

The limit rule allows a connection but blocks an IP address that opens 6 or more connections within 30 seconds. It is a simple brake on brute-force attempts:

sudo ufw limit OpenSSH

If you already have an allow rule for SSH, delete it after adding the limit rule (see step 7), otherwise the earlier allow rule matches first and the limit never applies. Rate limiting is not a substitute for key-only SSH login; use both.

7. Rule order, deleting and blocking

UFW checks rules from top to bottom and the first matching rule wins. List them with numbers:

sudo ufw status numbered

Delete a rule by its number, or by repeating the rule with delete in front:

sudo ufw delete 3
sudo ufw delete allow 80/tcp

Numbers change after every deletion, so run status numbered again before deleting another rule.

To block one address, the deny rule must sit above any allow rule that would match it. Use insert to put it at the top:

sudo ufw insert 1 deny from 198.51.100.25

deny silently drops the traffic. reject sends an error back, so the client knows at once that it is blocked. For public-facing servers, deny is the common choice.

8. Quick command reference

TaskCommand
Show status, defaults and rulessudo ufw status verbose
Show rules with numberssudo ufw status numbered
Show added rules (also while inactive)sudo ufw show added
Allow a service profilesudo ufw allow OpenSSH
Allow a port from one addresssudo ufw allow from 203.0.113.10 to any port 22 proto tcp
Rate-limit a servicesudo ufw limit OpenSSH
Put a rule at the topsudo ufw insert 1 deny from 198.51.100.25
Delete a rulesudo ufw delete 3
Reload after editing files in /etc/ufwsudo ufw reload
Turn the firewall offsudo ufw disable
Remove all rules and disablesudo ufw reset

9. Logging: see what is being blocked

Turn on logging at the default low level:

sudo ufw logging on

Blocked packets are tagged [UFW BLOCK]. On Ubuntu they are written to /var/log/ufw.log; you can also read them from the kernel log:

sudo tail -n 50 /var/log/ufw.log
sudo journalctl -k | grep 'UFW BLOCK'

Higher levels (medium, high, full) log much more and can fill the disk on a busy server. Use them only for short troubleshooting sessions.

10. Watch out: Docker can bypass UFW

This is the most common surprise. When you publish a container port, for example with docker run -p 8080:80, Docker adds its own firewall rules. According to the Docker documentation, traffic to published container ports is diverted before it reaches UFW's rules, so the port can be reachable from the internet even though ufw status does not allow it.

Safer options:

  • Publish the port only on localhost and put a reverse proxy (such as Nginx) in front: docker run -p 127.0.0.1:8080:80 ...
  • Do not publish ports that only other containers need; connect those containers on a Docker network instead.
  • For filtering that must apply to containers, add rules to the DOCKER-USER chain, as described in the Docker documentation.

Always test from outside the server, for example with nc -zv your-server-ip 8080 from another machine, instead of trusting the rule list alone.

11. UFW and cloud firewalls

Many hosting providers have their own network firewall (often called security groups or a cloud firewall). It works in front of the server and is separate from UFW. Using both is fine and adds a second layer, but traffic must be allowed in both places. If a port is open in UFW but still unreachable, check the provider's firewall too.

12. If you lock yourself out

  • Use your provider's web console or recovery console, log in locally and run sudo ufw allow OpenSSH or sudo ufw disable.
  • Check that the SSH port in your rule matches the port sshd really uses: sudo sshd -T | grep -i '^port'.
  • If you limited SSH to one IP address, check whether your public IP has changed.

Quick checklist

  • Defaults set to deny incoming and allow outgoing.
  • SSH allowed (or limited) on the correct port before ufw enable.
  • A second SSH session tested before closing the first.
  • Only the ports you need are open; databases restricted to private addresses.
  • IPv6 enabled in /etc/default/ufw if the server has an IPv6 address.
  • Docker published ports checked from outside the server.
  • Provider firewall rules reviewed alongside UFW.

A host firewall is one layer, not the whole defense. Combined with key-only SSH login and automatic security updates, it gives a Linux server a solid, low-maintenance baseline.

Sources

  • Ubuntu Server documentation: Firewall (documentation.ubuntu.com/server)
  • ufw(8) manual page (manpages.ubuntu.com)
  • Ubuntu Community Help Wiki: UFW (help.ubuntu.com/community/UFW)
  • Debian Wiki: Uncomplicated Firewall (ufw) (wiki.debian.org)
  • Docker documentation: Packet filtering and firewalls, Docker and ufw (docs.docker.com)