On this pageBefore you change a firewall over SSH
Turning UFW on takes five commands. Living with it is where admins trip: a deny rule that never fires, a delete that removes the wrong line, a port closed over IPv4 but open over IPv6, logs nobody reads. This guide covers the working side of UFW on Ubuntu and Debian servers: how it evaluates rules, how to change them without surprises, and how to see what it blocks. If you are still choosing a front end, read UFW, firewalld or nftables first. Everything below assumes UFW won.
Before you change a firewall over SSH
UFW applies each change the moment you press Enter. Enabling it without an SSH rule, or adding a broad deny above your own address, ends the session you are typing in. Three habits prevent that:
- Keep the current session open and test every change from a second terminal before you close anything.
- Allow SSH before you enable UFW or tighten a default policy, on the port your server really listens on.
- Preview risky changes with
--dry-run, which prints the rules UFW would load and changes nothing.
sudo ufw allow OpenSSH # or: sudo ufw allow 2222/tcp
sudo ufw --dry-run enable # show what would be loaded
sudo ufw enable # warns that SSH may be disrupted; answer y
If you lock yourself out anyway, the provider's console reaches the server without going through the network. On CloudSat, request console access from the server's Access tab, sign in and run ufw disable, as described in the recovery tools guide.
Read what is loaded
Four commands answer most questions about a running firewall:
sudo ufw status verbose # default policies, logging level, rules
sudo ufw status numbered # rules with the numbers used by insert and delete
sudo ufw show added # rules written as the commands that created them
sudo ufw show listening # listening sockets and the rules that cover them
Read status verbose first. Its Default: line shows the policy for incoming, outgoing and routed traffic, which decides everything no rule matches. show added works even while UFW is inactive, so it doubles as a readable backup. show listening puts each listening socket next to the rules that apply to it, a fast way to spot a service nobody meant to publish.
Numbered output on a small web server looks like this:
To Action From
-- ------ ----
[ 1] OpenSSH ALLOW IN 198.51.100.24
[ 2] 80,443/tcp ALLOW IN Anywhere
[ 3] 80,443/tcp (v6) ALLOW IN Anywhere (v6)
Rules marked (v6) are IPv6 copies. UFW adds them for any rule that names no address, as long as IPV6=yes is set in /etc/default/ufw, which is the default. A rule with an IPv4 source has no IPv6 twin. That is fine for an admin address, and a trap when you meant to restrict a service everywhere.
How UFW decides
The manual says it in one line: rule ordering is important and the first match wins. UFW walks the list from the top, applies the first rule that matches and ignores the rest. The default policy only applies when nothing matched. That is why this sequence fails:
sudo ufw allow 22/tcp
sudo ufw deny from 203.0.113.50 # appended below the allow, never reached for SSH
The deny lands at the bottom, so 203.0.113.50 still reaches SSH through the first rule. Put blocks above the allows instead:
sudo ufw insert 1 deny from 203.0.113.50 comment 'abusive host'
sudo ufw prepend deny from 2001:db8:bad::/48 comment 'abusive range, IPv6'
insert takes a position in the combined numbered list, where every IPv6 rule sits below the IPv4 ones. prepend saves the arithmetic: it puts the rule first among the rules of its own address family.
Rules you can still read next year
A rule set grows one urgent fix at a time. Three habits keep it understandable: name the source whenever you can, comment every rule, and choose the action on purpose.
| Goal | Command |
|---|---|
| Open a public port | sudo ufw allow 443/tcp comment 'HTTPS' |
| Admit one address to SSH | sudo ufw allow from 198.51.100.24 to any port 22 proto tcp comment 'office' |
| Trust a tunnel subnet | sudo ufw allow in on wg0 from 10.10.0.0/24 comment 'tunnel peers' |
| Open a port range | sudo ufw allow 60000:61000/udp comment 'mosh' |
| Brake password guessing | sudo ufw limit 22/tcp |
| Refuse fast on an internal port | sudo ufw reject 8080/tcp |
deny drops packets silently, so the client waits for a timeout. reject answers at once, which is kinder to your own tools on an internal network and pointless courtesy toward the internet. limit accepts a connection unless the same address opened 6 or more in the last 30 seconds.
Delete and change rules without surprises
UFW has no edit command: you delete a rule and add its new version. There are two ways to delete:
- By number. Run
sudo ufw status numbered, thensudo ufw delete 3. UFW prints the rule and asks you to confirm. Numbers shift after every deletion, so list again before the next one, and remember that the IPv4 and IPv6 copies carry separate numbers. - By specification.
sudo ufw delete allow 80/tcpremoves the rule exactly as it was written, both copies at once. This is the safer form in scripts.
To replace a rule that carries live traffic, insert the new version at the old position first, then delete the old one, which has moved down by one. The traffic is never left uncovered.
Application profiles
Packages can ship profiles in /etc/ufw/applications.d/ that name their ports, which is why sudo ufw allow OpenSSH works. sudo ufw app list shows the installed names, and sudo ufw app info 'Nginx Full' shows the ports behind one. Your own services can have profiles too. Save this as /etc/ufw/applications.d/billing-api:
[billing-api]
title=Billing API
description=Internal API behind the reverse proxy
ports=8443/tcp
sudo ufw app update billing-api
sudo ufw allow from 10.10.0.0/24 to any app billing-api comment 'app servers'
When the port changes, edit the file and run sudo ufw app update billing-api again. UFW rewrites every rule that uses the profile, so you never hunt for stray port numbers.
Outbound and routed traffic
The default allow outgoing suits most servers. Filtering egress pays off on machines that should talk to only a few places, because malware that lands on a server usually needs to call out. Add the allow rules first, then switch the policy:
sudo ufw allow out 53 comment 'DNS'
sudo ufw allow out 123/udp comment 'NTP'
sudo ufw allow out 80,443/tcp comment 'package updates and APIs'
sudo ufw default deny outgoing
Replies to connections you accepted keep flowing, because UFW tracks connection state. Watch the logs for a day afterward: backup jobs, monitoring agents and outgoing mail are the usual services that turn out to need a rule.
Routed traffic passes through the host instead of ending there, for example a WireGuard gateway forwarding clients to the internet. It is denied by default and needs route rules such as sudo ufw route allow in on wg0 out on eth0, IP forwarding enabled in /etc/ufw/sysctl.conf, and a masquerade rule in /etc/ufw/before.rules if the clients use private addresses. Docker is the exception that bites: it writes its own rules for published ports, which never pass through UFW's input rules, as the firewall comparison explains.
Read the logs
sudo ufw logging low is the sensible level for a server. Each level adds to the one before:
| Level | What it logs |
|---|---|
low |
Packets blocked by the default policy (rate limited), plus packets matching rules marked log |
medium |
Low, plus allowed packets that match no rule, invalid packets and all new connections |
high |
Medium without rate limiting, plus all packets with rate limiting |
full |
High without any rate limiting |
Raise the level for an hour while you debug, then lower it: high and full write a lot on an internet-facing server. Entries go to the kernel log with a [UFW BLOCK] prefix, so journalctl -k reads them on any system, and /var/log/ufw.log holds them where rsyslog runs.
journalctl -k --since "1 hour ago" | grep 'UFW BLOCK' | tail -5
journalctl -k --since today | grep -o 'DPT=[0-9]*' | sort | uniq -c | sort -rn | head
The first command shows recent blocks. In each line, SRC is the sender, DPT the destination port and PROTO the protocol. The second counts blocked destination ports, a quick picture of what the internet keeps knocking on. To record the matches of one rule, add the log keyword when you create it: sudo ufw allow log 22/tcp.
Back up and start over
Rules you add live in /etc/ufw/user.rules and /etc/ufw/user6.rules. Hand-written extras, such as NAT, belong in /etc/ufw/before.rules. Copy the whole /etc/ufw directory before big changes, and keep the output of sudo ufw show added with your server notes. When a rule set is past saving, sudo ufw reset disables UFW and returns it to the installation defaults, keeping timestamped copies of the old rule files. Add the SSH rule again before you re-enable it.
FAQ
Why does every rule appear twice?
One copy is for IPv4 and the other, marked (v6), for IPv6. UFW creates both when a rule names no address and IPv6 support is on in /etc/default/ufw.
Does UFW protect my Docker containers?
Not their published ports. Docker inserts forwarding rules that run before UFW's, so ufw deny 8080 does not close a container published on 8080. Publish internal containers on 127.0.0.1, or filter in a firewall in front of the server.
How do I stop the server answering ping?
Echo requests are accepted in /etc/ufw/before.rules, not in your rule list. Change the echo-request line in the input section from ACCEPT to DROP, then run sudo ufw reload. Leave ICMPv6 in before6.rules alone, because IPv6 needs it to work at all.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




