The ufw command
The ufw command sets up Ubuntu’s firewall in plain words…
The ufw command sets up Ubuntu’s firewall in plain words: sudo ufw allow 22/tcp lets ssh in, sudo ufw enable turns it on, and sudo ufw status lists the rules.
Taught in Advanced, lesson 47; more in Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
sudo ufw status [verbose|numbered] sudo ufw allow|deny|reject PORT[/tcp|/udp] sudo ufw allow from ADDRESS to any port PORT sudo ufw delete RULE|NUMBER sudo ufw enable|disable
The firewall: allow or block ports. ufw ("uncomplicated firewall") decides which incoming connections a machine accepts. With it enabled, everything not allowed is dropped, so allow what the machine serves: ssh (22), http (80), https (443). A dropped connection looks like a timeout from outside; reject answers "refused" instead. Rules are checked in order and the first match wins. Before enabling ufw on a server you reach over ssh, allow OpenSSH, or you lock yourself out. web and db have ufw switched on. All of it needs sudo. In this practice shell the network is a simulation inside your browser: the machines web, db and router and the website example.com are pretend, and nothing leaves your device.
In the practice terminal:A simulation: web, db, router and example.com are pretend machines inside your browser, and nothing leaves your device. Each has its own firewall: web allows ports 22 and 80, and db lets only web reach its database.
How to use ufw
See the rules: ufw status
Each rule says which port, what happens to it (ALLOW, DENY) and from where.
ssh webThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: Welcome to Ubuntu 24.04 LTS (practice web server web) This is a pretend machine inside your browser. Type exit to go back to sandbox. Last login: Mon Jan 12 06:39:27 2026 from 10.0.0.5
sudo ufw status[sudo] password for learner: Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6)
Allow a port, then turn it on
On a server you reach with ssh, allow port 22 before you enable the firewall, or you lock yourself out.
sudo ufw allow 22/tcp[sudo] password for learner: Rule added Rule added (v6)
sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup
sudo ufw status verboseStatus: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp ALLOW IN Anywhere 22/tcp (v6) ALLOW IN Anywhere (v6)
Allow one address only
On db, PostgreSQL (port 5432) takes connections from web (10.0.0.10) and nowhere else.
ssh dbThe authenticity of host 'db (10.0.0.20)' can't be established. ED25519 key fingerprint is SHA256:fLxDFExDWEnysz8O/eQFLvp7rre0Hshqu8oYMO9o94O. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'db' (ED25519) to the list of known hosts. learner@db's password: Welcome to Ubuntu 24.04 LTS (practice database server db) This is a pretend machine inside your browser. Type exit to go back to sandbox. Last login: Mon Jan 12 06:39:27 2026 from 10.0.0.5
sudo ufw status[sudo] password for learner: Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 5432/tcp ALLOW 10.0.0.10 22/tcp (v6) ALLOW Anywhere (v6)
Delete a rule by its number
On web (ssh web first): status numbered shows the numbers, and delete asks before removing one.
sudo ufw status numbered[sudo] password for learner:
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 22/tcp (v6) ALLOW IN Anywhere (v6)
[ 4] 80/tcp (v6) ALLOW IN Anywhere (v6)
sudo ufw delete 2Deleting: allow 80/tcp Proceed with operation (y|n)? y Rule deleted
Options
| status | on or off, and the rules (numbered: with numbers for delete) |
|---|---|
| allow PORT | accept connections, for example 80/tcp, http or OpenSSH |
| deny PORT | drop them |
| reject PORT | refuse them straight away |
| allow from IP to any port N | only from one address |
| delete RULE | remove a rule, written as it was added, or by number |
| default deny incoming | the policy for everything else |
| enable, disable | switch the firewall on or off |
Questions about ufw
What is the difference between ufw deny and reject?
deny drops the packets without a word, so the other side waits until it times out. reject answers at once that the port is closed. deny gives less away; reject fails faster.
Is ufw the same as iptables?
ufw (uncomplicated firewall) writes the iptables or nftables rules for you, in simpler words. Ubuntu comes with it; Fedora and Red Hat use firewalld instead.
Why does my website time out after I enabled ufw?
The default is to deny incoming connections, so every service needs its own allow rule, such as sudo ufw allow 80/tcp for a web server.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.