UFW Guide

Basic UFW Firewall Commands on Ubuntu

UFW (Uncomplicated Firewall) provides a simple way to manage firewall rules on Ubuntu.

Common Commands

TaskCommand
Check firewall statussudo ufw status
Show detailed statussudo ufw status verbose
Enable UFWsudo ufw enable
Disable UFWsudo ufw disable
Allow a portsudo ufw allow 22
Allow a servicesudo ufw allow OpenSSH
Deny a portsudo ufw deny 80
Delete a rulesudo ufw delete allow 22
Allow a port from a specific IPsudo ufw allow from 192.168.1.100 to any port 22
Show numbered rulessudo ufw status numbered
Delete a rule by numbersudo ufw delete 3

Recommended Basic Server Setup

Set the default firewall policies:

sudo ufw default deny incoming
sudo ufw default allow outgoing

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:

sudo ufw allow OpenSSH

Or, if SSH is running on the standard port:

sudo ufw allow 22/tcp

Then enable UFW:

sudo ufw enable

Check the result:

sudo ufw status verbose

Web Server Rules

For a normal web server, HTTP and HTTPS can be allowed with:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

If Nginx application profiles are installed, you can instead use:

sudo ufw allow 'Nginx Full'

Nginx Full normally allows both HTTP port 80 and HTTPS port 443.

List Available Application Profiles

To see the application profiles available to UFW:

sudo ufw app list

Example:

Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

These profiles are normally defined under:

/etc/ufw/applications.d/

To see details about a particular profile:

sudo ufw app info OpenSSH

or:

sudo ufw app info 'Nginx Full'

Check Rules with Numbers

For easier rule management:

sudo ufw status numbered

Example:

[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 443/tcp ALLOW IN Anywhere

A rule can then be removed using its number:

sudo ufw delete 2

Important

When configuring UFW on a remote server, always allow the SSH port before running:

sudo ufw enable

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.

Contact

Want to get in touch?

If you want to talk about a project, a software or IT role, something you have built, or a system you want to improve, send me a message.

I read the messages myself.

Thank you. Your message has been sent.