Skip to main content

How to Enable and Use The Linux Firewall (UFW)

UFW (Uncomplicated Firewall) controls network traffic between your Ubuntu machine and other devices. It primarily filters incoming and outgoing connections to and from your machine, NOT internal communication on the machine.

Before enabling UFW, identify the services and ports running on your machine:

sudo ss -lntup

This lists ports in use (in the local address: port column) which you may need to allow in UFW.

To avoid losing SSH access, allow OpenSSH first:

sudo ufw allow OpenSSH
sudo ufw enable

Confirm UFW is active:

sudo ufw status

Common UFW Commands

CommandDescriptionCommandDescription
sudo ufw enableEnable the firewallsudo ufw disableDisable the firewall
sudo ufw statusShow firewall statussudo ufw status verboseShow detailed firewall status
sudo ufw status numberedShow rules with numberssudo ufw allow [port]Allow traffic on a port
sudo ufw deny [port]Deny traffic on a portsudo ufw allow [port]/[proto]Allow port with protocol (e.g., tcp)
sudo ufw allow from [IP]Allow all traffic from an IPsudo ufw allow from [IP] to any port [port] proto [proto]Allow from IP to specific port/proto
sudo ufw allow in on [iface] to any port [port]Allow a port on a specific interfacesudo ufw delete allow [port]Delete allow rule for a port
sudo ufw delete [number]Delete the rule by numbersudo ufw default deny incomingSet default policy to deny incoming
sudo ufw default allow outgoingSet default policy to allow outgoingsudo ufw reloadReload UFW to apply changes
sudo ufw resetReset UFW and remove all rulessudo ufw logging onEnable UFW logging
sudo ufw show addedShow added rulessudo ufw --helpShow UFW help and commands

How you configure UFW depends on your services and security needs. UFW adds an important layer of security to your infrastructure.

Follow Us on Social Media

YouTube
Discord
Patreon
Reddit
Rumble