Home How ToInstall and Configure UFW Firewall on Ubuntu 22.04

Install and Configure UFW Firewall on Ubuntu 22.04

by Knowledge Base Linux
Install and Configure UFW Firewall on — Install and Configure UFW Firewall on Ubuntu 22.04

What Is UFW and Why Use It on Ubuntu? – Install and Configure UFW Firewall on

UFW (Uncomplicated Firewall) is the default firewall management tool on Ubuntu. It provides a simple command-line interface for managing iptables rules without requiring deep knowledge of netfilter syntax. If you run a VPS or dedicated server, learning how to install UFW on Ubuntu and configure it correctly is one of the first hardening steps you should take after initial server setup.

By default, a fresh Ubuntu installation accepts all incoming connections. That means every open port on your server is reachable from the internet — including services you never intended to expose. UFW lets you flip that model: deny everything, then explicitly allow only the traffic you need (SSH, HTTP, HTTPS).

Prerequisites – Install and Configure UFW Firewall on

  • A server running Ubuntu 22.04 (also works on 20.04 and 24.04)
  • Root access or a user with sudo privileges
  • An active SSH connection — do not close it until the firewall is verified
UFW firewall rules terminal on Ubuntu
Warning: If you enable UFW without allowing SSH first, you will lock yourself out of the server. Always allow SSH before running ufw enable.

Step 1 – Install UFW on Ubuntu 22.04

UFW is usually pre-installed on Ubuntu Server. Check first, then install if missing:

sudo apt update
sudo ufw --version

If the command is not found, install it:

sudo apt install ufw -y

Step 2 – Set Default Policies

Before enabling the firewall, define the default behavior. The recommended baseline for a server is deny all incoming, allow all outgoing:

sudo ufw default deny incoming
sudo ufw default allow outgoing

This ensures that only connections you explicitly permit can reach your server, while the server itself can still reach out (for updates, DNS, API calls).

Step 3 – Allow SSH (Critical)

Allow SSH access before enabling UFW. If you changed the default SSH port (see our guide on how to change the SSH port on Linux), allow that port instead of 22:

sudo ufw allow ssh
# or for a custom port, e.g. 2222:
sudo ufw allow 2222/tcp

You can also restrict SSH to your own IP address for extra security:

sudo ufw allow from 203.0.113.25 to any port 22 proto tcp

Step 4 – Allow Web Traffic (HTTP/HTTPS)

If the server hosts websites, open ports 80 and 443:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# or in one command:
sudo ufw allow 'Nginx Full'

UFW ships with application profiles (/etc/ufw/applications.d/). List them with sudo ufw app list.

Step 5 – Enable UFW

Now enable the firewall. You will be asked to confirm because this may disrupt existing SSH connections:

sudo ufw enable

Type y and press Enter. Verify the status:

sudo ufw status verbose

Expected output:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443/tcp                    ALLOW IN    Anywhere

Step 6 – Manage Rules (Allow, Deny, Delete)

Allow a specific port or range:

sudo ufw allow 3306/tcp
sudo ufw allow 8000:8100/tcp

Deny traffic explicitly:

sudo ufw deny 23/tcp

List rules with numbers, then delete by number:

sudo ufw status numbered
sudo ufw delete 3

Rate-limit SSH to mitigate brute-force attacks (an alternative to tools like CSF firewall):

sudo ufw limit ssh/tcp

Step 7 – Enable Logging and Check Blocked Traffic

UFW logging helps you spot port scans and brute-force attempts:

sudo ufw logging on
sudo tail -f /var/log/ufw.log

Blocked packets appear with the [UFW BLOCK] prefix, including source IP and destination port.

Step 8 – Configure IPv6 Support

UFW supports IPv6 out of the box, controlled by /etc/ufw/ufw.conf. Confirm it is enabled:

grep IPV6 /etc/ufw/ufw.conf
# expected: IPV6=yes

When active, every rule you add automatically applies to both Anywhere (IPv4) and Anywhere (v6) (IPv6) in ufw status output. If your provider does not route IPv6, you may set IPV6=no and reload, but leaving it on is harmless and future-proof.

Backing Up and Restoring UFW Rules

Before making major changes, back up your rule set. UFW stores rules in /etc/ufw/ and /lib/ufw/ufw-init:

sudo cp -r /etc/ufw /root/ufw-backup-$(date +%F)

To reset everything and start clean:

sudo ufw --force reset
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable

Troubleshooting Common UFW Issues

Locked out of SSH

If you enabled UFW without allowing SSH, use your hosting provider’s VNC/console access to run ufw allow ssh && ufw reload, or temporarily disable with ufw disable.

Rule order matters

UFW processes rules top-down; the first match wins. A broad allow placed before a specific deny will override it. Use ufw insert 1 deny ... to place a rule at the top.

Docker bypasses UFW

Docker inserts its own iptables rules that bypass UFW chains. If you run Docker containers, be aware that published ports may remain reachable despite UFW deny rules — bind container ports to 127.0.0.1 or manage Docker’s iptables integration explicitly.

UFW vs iptables vs firewalld

Beginners often wonder how UFW relates to other Linux firewall tools. The underlying packet filter on modern Linux is netfilter, controlled via iptables (or nftables on newer kernels). UFW is simply a friendlier front-end: every ufw allow command translates into iptables rules under the hood — you can inspect them with sudo iptables -L -n -v.

firewalld is the default on RHEL-family distros (AlmaLinux, Rocky, CentOS) and uses the concept of zones. UFW is the default on Debian/Ubuntu and uses a simpler numbered-rule model. For a single Ubuntu VPS, UFW is the right choice; its learning curve is measured in minutes, and it covers 95% of real-world server firewall needs. When you outgrow it — for example, complex NAT or per-interface policies — you can always drop down to raw iptables or nftables later without reinstalling anything.

Conclusion

You now know how to install UFW on Ubuntu 22.04 and configure a secure baseline: default-deny incoming, SSH allowed, web ports open, and logging enabled. UFW is lightweight and ideal for single servers; for more advanced needs such as connection tracking and login-failure detection, consider pairing it with iptables extensions or Fail2Ban. Review ufw status verbose periodically and remove rules you no longer need.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.