Skip to contentcloudvix
cloudvix
GuideServers & hosting

UFW on Ubuntu 24.04: a firewall for one server, IPv4 and IPv6

Denys DunaievskyiFounder, Cloudvix
  • 12 min read
  • Published
  • LevelBeginner
Guides
On this page

Key takeaways

  • Enabling the firewall without the right SSH rule cuts your connection.
  • A server has an IPv6 address next to its IPv4 one, and a rule for one family does not protect the other.
  • Test one allowed port and one closed port, and test the IPv6 address as well: the first command prints it on the server.

UFW (Uncomplicated Firewall) is the tool Ubuntu uses to decide which network connections a server accepts. This guide sets it up on Ubuntu 24.04 for a single server: everything incoming is refused except what you name, SSH is allowed first so that you do not lock yourself out, IPv6 gets the same protection as IPv4, and you test the result from your own computer.

Before you start

You need an Ubuntu 24.04 server, a user with sudo and a working SSH login, as set up in the first-hour guide. Keep your current SSH window open until the last check: if a rule turns out wrong, that window is your way back in. Know which ports your server has to serve: SSH on port 22, and 80 and 443 only if it runs a web site.

  • an Ubuntu 24.04 server and a user with sudo
  • your current SSH session, kept open until the end
  • a list of the ports your server has to serve to the internet

The steps

See what listens now

A firewall decides about ports, so first list the ports that have a program behind them. ss -tulpn shows listening TCP and UDP sockets with the program name. The addresses 127.0.0.1 and ::1 are reachable from the server itself only; 0.0.0.0 and [::] are open to the network. Then ask UFW for its state. If it is not active yet, it says Status: inactive.

On the serverbash
sudo ss -tulpn
sudo ufw status verbose

Make sure IPv6 is covered

A server has an IPv6 address next to its IPv4 one, and a rule for one family does not protect the other. UFW writes both when IPV6=yes is set in /etc/default/ufw, which is the Ubuntu default; the command shows the setting. Only if it says IPV6=no, change it with sudo sed -i 's/^IPV6=no/IPV6=yes/' /etc/default/ufw. UFW reads the file when it is switched on, so the change applies in the step "Switch the firewall on". If UFW is already active, run sudo ufw disable and sudo ufw enable once; the rules are kept.

On the serverbash
grep '^IPV6=' /etc/default/ufw

Refuse by default

The default policy says what happens to a connection that no rule mentions. deny incoming drops connections from the internet unless a rule allows them; allow outgoing lets the server reach update servers and APIs. While UFW is inactive, these two commands only store the policy, so they are safe to run now.

On the serverbash
sudo ufw default deny incoming
sudo ufw default allow outgoing

Allow SSH before anything else

OpenSSH is a ready-made profile for port 22/tcp (sudo ufw app list shows all profiles). limit is allow plus a brake: UFW refuses an address that opens six or more connections within 30 seconds. That slows down password guessing but does not replace key-only login. ufw show added lists the rules that are stored and not yet active.

On the serverbash
sudo ufw limit OpenSSH
sudo ufw show added
Warning

If your SSH server listens on another port, allow that port instead, for example sudo ufw limit 2222/tcp; the first step shows the port. Enabling the firewall without the right SSH rule cuts your connection.

Open only the ports you serve

The first line opens the web ports to everyone; skip it if the server does not run a web site. The second line shows the safer pattern for everything else: a port that only one address may reach, here a database port, only if you need it. 203.0.113.10 is a documentation address; put your own there. A rule without from applies to IPv4 and IPv6; a rule with an IPv4 address applies to IPv4 only.

On the serverbash
sudo ufw allow 80,443/tcp
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcp

Switch the firewall on

UFW warns that the command may disrupt existing SSH connections and asks whether to proceed; type y. Your open session keeps working because the rule for SSH exists. The status must show Status: active and deny (incoming), allow (outgoing). Every rule without a source address appears twice, once plain and once marked (v6).

On the serverbash
sudo ufw enable
sudo ufw status verbose

Test from outside

Open a terminal on your own computer, not on the server. A port that is allowed answers with succeeded (or open); a port that the firewall drops does not answer, and nc gives up after three seconds. Test one allowed port and one closed port, and test the IPv6 address as well: the first command prints it on the server. On Windows, Test-NetConnection SERVER_IP -Port 22 in PowerShell does the same as nc.

On the serverbash
ip -6 -brief address show scope global
On your computerbash
nc -vz -w 3 SERVER_IP 22
nc -vz -w 3 SERVER_IP 8080
nc -6 -vz -w 3 SERVER_IPV6 22

Remove a rule

ufw status numbered lists the rules with numbers, and sudo ufw delete 3 removes rule 3. The numbers move up after every deletion, so list the rules again before the next one. The IPv4 and IPv6 versions of a rule have their own numbers. The second command removes a rule by repeating it after delete, which also removes both versions. It deletes the example rule for port 5432 from the step "Open only the ports you serve"; if you did not add that rule, UFW says that it does not exist.

On the serverbash
sudo ufw status numbered
sudo ufw delete allow from 203.0.113.10 to any port 5432 proto tcp

Read what the firewall drops

With logging on, UFW writes a kernel log line for each blocked packet, with SRC= (who) and DPT= (which port). low logs blocked packets that match no rule and keeps the noise down. _TRANSPORT=kernel reads the kernel messages of all boots in the period. A server on the public internet usually shows some lines; none does not mean a fault.

On the serverbash
sudo ufw logging low
sudo journalctl _TRANSPORT=kernel --since "1 hour ago" --no-pager | grep 'UFW BLOCK' | tail -n 20

Switch off or start over

disable turns the firewall off and keeps the rules; enable brings them back. While UFW is off, nothing filters incoming connections. sudo ufw reset deletes all rules and switches UFW off after asking for confirmation; it is not in the block above on purpose, so that you do not run it by habit. Use it only for a clean start, and write down the rules you need before you do.

On the serverbash
sudo ufw disable

If something goes wrong

ERROR: Could not find a profile matching 'OpenSSH'

The profile file is missing. List what exists with sudo ufw app list. If OpenSSH is not there, use the port instead: sudo ufw limit 22/tcp.

I locked myself out

The usual cause is a wrong or missing SSH rule before ufw enable. If you still have an open session, fix the rule there (sudo ufw limit 22/tcp) and test from a second window. If no session is left, you need a way into the server that does not use SSH; a reinstall, as in the reinstall guide, erases the disk.

A port is blocked in UFW but still reachable

Ports published by Docker go around UFW; see Docker ignores UFW. Otherwise look at sudo ufw status numbered for an allow rule that covers the port, and at sudo ss -tulpn to see which port the program really uses.

Rules show for IPv4 only

IPv6 handling is off. Check grep '^IPV6=' /etc/default/ufw; it must say IPV6=yes. After changing it, run sudo ufw disable and sudo ufw enable once, then sudo ufw status verbose again.

Questions people ask

Is UFW a different firewall from iptables or nftables?

No. UFW is a simpler front end: you state rules, and it writes them into the firewall of the kernel that iptables and nftables also use. Use UFW for the rules of one server and do not edit the same rules with several tools.

Should I move SSH to another port?

It does not make SSH safer; key-only login does. A changed port only reduces the noise in the logs. If you do it, add the firewall rule for the new port first and test it before you close the old one.

Does ufw limit replace fail2ban?

No. limit is a coarse brake on new connections per address. fail2ban reads the log and bans addresses that fail to log in. They work well together, and the first-hour guide shows fail2ban.

Where to go next

Docker publishes ports around UFW; Docker ignores UFW shows how to close them. For the full list of checks, use the security checklist, and to reach private services through an encrypted tunnel instead of open ports, set up WireGuard.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server