How to Configure the UFW Firewall on Linux Step by Step
Enable and configure the UFW firewall to protect your Linux system without dealing with iptables.
UFW (Uncomplicated Firewall) is the simplified way to manage the firewall on Ubuntu and derivatives — a readable interface over the real system (iptables/nftables) without having to learn its full syntax.
Check the current status
sudo ufw status verbose
If it isn't installed: sudo apt install ufw.
Set the default policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
This blocks anything trying to connect to you from outside, but lets your machine keep browsing and downloading normally — a reasonable safe baseline for most home uses.
Before enabling, the most important step: if you connect to this machine via SSH from another device (for example, a home server or a remote PC), allow SSH BEFORE enabling the firewall:
sudo ufw allow OpenSSH
If you skip this step and enable the firewall with the deny-all- incoming policy, you will lock yourself out of your own machine with no remote way to fix it — you would need physical access to the machine to correct it.
Enable the firewall
sudo ufw enable
Allow other specific services
sudo ufw allow 80/tcp # HTTP web server
sudo ufw allow 443/tcp # HTTPS web server
Tip: for services that should only be accessible
from your own local network (like file sharing), use
sudo ufw allow from 192.168.1.0/24 to any port 445
instead of opening it to anyone — it restricts access to devices in
your own home.
View and delete rules
sudo ufw status numbered
sudo ufw delete [number]
Note: if you need to disable it completely and
temporarily: sudo ufw disable. Your rules are kept, not
deleted — sudo ufw enable reactivates them exactly as
they were.
Frequently asked questions
What happens if I enable UFW without allowing SSH first and I connect remotely?
You will lock yourself out of your own machine, with no way to connect remotely to fix it — you would need physical access to the machine. That's why you should always allow SSH before enabling the firewall, if you use it.
Does disabling UFW delete the rules I configured?
No, the rules are kept saved — turning it back on with 'ufw enable' applies them again exactly as they were configured.
Do I need UFW on a normal desktop PC, not a server?
It is advisable as an extra layer of security, although the risk is lower than on a server exposed to the internet. The basic setup (deny incoming, allow outgoing) is enough for most home uses.