Tools › Cheat sheets › Networking & Security › ufw
🛡️

ufw cheat sheet

ufw (Uncomplicated Firewall) is a friendly front end to iptables/nftables on Ubuntu and Debian. Always allow SSH before enabling the firewall, or you can lock yourself out of a remote server.

Click any command to copy it.

Turn on safely

CommandWhat it does
sudo ufw default deny incomingBlock all incoming connections unless allowed.
sudo ufw default allow outgoingAllow all outgoing connections.
sudo ufw allow 22/tcpAllow SSH. Do this first.
sudo ufw enableTurn the firewall on (and at boot).
sudo ufw status verboseShow status, defaults and rules.
sudo ufw status numberedShow rules with numbers for deleting.

Allow and deny

CommandWhat it does
sudo ufw allow 80,443/tcpAllow HTTP and HTTPS.
sudo ufw allow 6000:6007/tcpAllow a port range.
sudo ufw allow from 203.0.113.4Allow all traffic from one IP.
sudo ufw allow from 203.0.113.4 to any port 22Allow one IP to reach SSH only.
sudo ufw allow from 10.0.0.0/24 to any port 5432Allow a subnet to reach PostgreSQL.
sudo ufw deny 23Block Telnet.
sudo ufw limit sshRate-limit SSH to slow down brute-force attempts.
sudo ufw allow "Nginx Full"Allow HTTP and HTTPS using an application profile.
sudo ufw app listList available application profiles.

Delete and reset

CommandWhat it does
sudo ufw delete allow 80/tcpRemove a rule by its definition.
sudo ufw delete 2Remove a rule by its number from status numbered.
sudo ufw disableTurn the firewall off.
sudo ufw reloadReload the rules.
sudo ufw resetDestructive: Disable ufw and delete all rules.

Logging

CommandWhat it does
sudo ufw logging onEnable logging.
sudo ufw logging mediumSet the log level.
sudo tail -f /var/log/ufw.logWatch blocked and allowed connections live.

Frequently asked questions

How do I avoid locking myself out over SSH?

Run ufw allow 22/tcp (or your SSH port) before ufw enable, and keep a second session open while testing.

Does Docker bypass ufw?

Yes: published container ports are added to iptables directly and can bypass ufw rules. Bind ports to 127.0.0.1 or manage the DOCKER-USER chain.

More Networking & Security cheat sheets

🔑 SSH🌐 curl🛰️ Linux networking🔐 OpenSSL🔒 Certbot (Let's Encrypt)🌐 Subnet / CIDR🧱 iptablesAll Networking & Security cheat sheets →
Copied to clipboard