Network

The ufw command

Simple firewall: turns protection on and decides which ports open.

Syntax

sudo ufw status | sudo ufw enable | sudo ufw allow PUERTOTry it in the terminal →

Most useful options

status [verbose|numbered]Shows whether it is active and its rules. verbose adds detail and numbered numbers them so you can delete them
enableTurns the firewall on and keeps it on at boot. Allow your SSH port first
allow PUERTO[/PROTO]Allows traffic on a port or service. Without a protocol, it covers tcp and udp
deny PUERTO[/PROTO]Blocks traffic on a port or service by dropping packets
limit PUERTO[/PROTO]Allows the connection, but denies it if one IP tries 6 or more connections within 30 seconds
delete NUM|REGLADeletes a rule by its number or by repeating the full rule

Copy-ready examples

$ sudo ufw statussee whether the firewall is active and which rules it has.
$ sudo ufw allow 22/tcp && sudo ufw status verboseallows SSH and checks the rules in detail before turning anything on.
$ sudo ufw allow proto tcp from 192.168.1.0/24 to any port 3306allows MySQL only from the local network 192.168.1.0/24.
$ sudo ufw delete 3deletes rule number 3. It is destructive: first list the rules with sudo ufw status numbered, because numbering changes when you delete.

You can try them in the browser terminal: it does not touch your computer.

Typical mistakes

  • When enabling it over SSH, allow port 22 before enable. Otherwise you may lock yourself out of your own server.
  • ufw route does not protect the machine itself: its rules affect traffic forwarded through it.
  • With several ports in one rule, you cannot delete just one: the rule is changed as a whole.

Differences from similar commands

  • iptables: configures netfilter with detailed rules. ufw uses simple syntax and generates the rules for you.
  • ss: shows which ports your machine listens on. ufw decides which ones accept connections from outside.

Related commands

FAQ

Do ufw rules survive a reboot?

Yes, as long as the firewall is enabled. enable keeps it active at boot.

What is the difference between deny and reject?

deny drops the packet without replying. reject tells the sender that the traffic is blocked.

How do I allow a range of ports?

With the full syntax: ufw allow proto tcp from any to any port 8080:8090. A range counts as two of the 15-port limit per rule.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course