Basic UFW Firewall Commands on Ubuntu
UFW (Uncomplicated Firewall) provides a simple way to manage firewall rules on Ubuntu.
Common Commands
| TaskCommand | |
| Check firewall status | sudo ufw status |
| Show detailed status | sudo ufw status verbose |
| Enable UFW | sudo ufw enable |
| Disable UFW | sudo ufw disable |
| Allow a port | sudo ufw allow 22 |
| Allow a service | sudo ufw allow OpenSSH |
| Deny a port | sudo ufw deny 80 |
| Delete a rule | sudo ufw delete allow 22 |
| Allow a port from a specific IP | sudo ufw allow from 192.168.1.100 to any port 22 |
| Show numbered rules | sudo ufw status numbered |
| Delete a rule by number | sudo ufw delete 3 |
Recommended Basic Server Setup
Set the default firewall policies:
This blocks incoming connections unless they have been explicitly allowed, while still allowing the server to make outgoing connections.
Allow SSH Before Enabling UFW
If the server is accessed remotely through SSH, allow SSH before enabling the firewall:
Or, if SSH is running on the standard port:
Then enable UFW:
Check the result:
Web Server Rules
For a normal web server, HTTP and HTTPS can be allowed with:
If Nginx application profiles are installed, you can instead use:
Nginx Full normally allows both HTTP port 80 and HTTPS port 443.
List Available Application Profiles
To see the application profiles available to UFW:
Example:
These profiles are normally defined under:
To see details about a particular profile:
or:
Check Rules with Numbers
For easier rule management:
Example:
A rule can then be removed using its number:
Important
When configuring UFW on a remote server, always allow the SSH port before running:
Otherwise, the current SSH connection may continue working temporarily, but new SSH connections can be blocked and you may lose remote access to the server.