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.
sudo ss -tulpn
sudo ufw status verboseMake 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.
grep '^IPV6=' /etc/default/ufwRefuse 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.
sudo ufw default deny incoming
sudo ufw default allow outgoingAllow 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.
sudo ufw limit OpenSSH
sudo ufw show addedIf 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.
sudo ufw allow 80,443/tcp
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcpSwitch 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).
sudo ufw enable
sudo ufw status verboseTest 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.
ip -6 -brief address show scope globalnc -vz -w 3 SERVER_IP 22
nc -vz -w 3 SERVER_IP 8080
nc -6 -vz -w 3 SERVER_IPV6 22Remove 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.
sudo ufw status numbered
sudo ufw delete allow from 203.0.113.10 to any port 5432 proto tcpRead 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.
sudo ufw logging low
sudo journalctl _TRANSPORT=kernel --since "1 hour ago" --no-pager | grep 'UFW BLOCK' | tail -n 20Switch 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.
sudo ufw disableLet an assistant do it, with a plan first
If you use an AI assistant that can run SSH commands, paste the prompt below. It works with SSH access as a user with sudo. If the assistant should also order the server, use a Cloudvix API key, which has limits (up to €500 a month and 25 servers), instead of your account login.
The prompt makes the assistant look first, show a plan before it acts, check each step and stop when a check fails. It must keep your SSH session open and must not run ufw reset or ufw disable without asking you.
You are helping me set up the UFW firewall on an Ubuntu 24.04 server at SERVER_IP. Connect over SSH as the user ops, which has sudo.
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
First only look: run ss -tulpn and ufw status verbose and tell me which ports have a program behind them and whether UFW is active. Then propose a plan that allows only the ports I confirm. Required order: set the default policy (deny incoming, allow outgoing), allow SSH first with ufw limit OpenSSH (or the real SSH port if it is not 22), then the ports I confirmed, then ufw enable. Keep this SSH session open and do not log out. Never run ufw reset or ufw disable without asking me. After enabling, show ufw status verbose and check from a second SSH connection that I can still log in.
At the end, list what you changed and the command that undoes each change.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.
Runs Cloudvix and is responsible for the guides published here.