02.03.2026

UFW Commands for Ubuntu: Firewall Setup, Rules, IPv4/IPv6, Best Practices

Introduction

This guide explains how to configure and manage UFW (Uncomplicated Firewall) on Ubuntu servers. You will learn how to enable the firewall, create and delete rules, manage service profiles, and secure both IPv4 and IPv6 network traffic.

sudo apt update && sudo apt install ufw

Checking UFW Firewall Status on Ubuntu

To check whether the firewall is active:

sudo ufw status

If you see Status: inactive, it means the firewall is currently disabled.

Enabling UFW

Important: If you are connected via SSH, allow SSH access before enabling UFW to avoid being locked out.

To enable UFW:

sudo ufw allow OpenSSH
sudo ufw enable

To see the list of current rules:

sudo ufw status verbose

Disabling UFW

To temporarily disable the firewall:

sudo ufw disable

Note: This completely turns off your firewall—use with caution!

Blocking IP Addresses and Subnets

sudo ufw deny from 91.198.174.190
sudo ufw deny from 91.198.174.0/24
sudo ufw deny in on eth0 from 91.198.174.192

Allowing IP Addresses

sudo ufw allow from 91.198.174.192
sudo ufw allow in on eth0 from 91.198.174.22

Deleting Rules

sudo ufw delete allow from 91.198.174.192
sudo ufw status numbered sudo ufw delete 1

Application Profiles

UFW can use predefined profiles for common services:

sudo ufw app list
sudo ufw allow OpenSSH
sudo ufw delete allow "Nginx Full"

Opening Common Ports

sudo ufw allow 22
sudo ufw allow http
sudo ufw allow https
sudo ufw allow proto tcp from any to any port 80,443

Allowing Database Connections

sudo ufw allow from 91.198.174.33 to any port 3306
sudo ufw allow from 91.198.174.33 to any port 5432
sudo ufw allow from 91.198.174.0/24 to any port 3306

FAQ: UFW (Uncomplicated Firewall) on Ubuntu

UFW Best Practices for Ubuntu Servers

UFW best practices for Ubuntu servers:

Conclusion

UFW is a simple and reliable way to harden an Ubuntu server by controlling inbound and outbound traffic without dealing directly with raw iptables rules. In this guide, you installed UFW, checked status, safely enabled the firewall without losing SSH access, and learned how to allow or block ports, IPs, subnets, and service profiles.
For a secure setup, start with a restrictive default policy, expose only required services (SSH/HTTP/HTTPS), limit access to sensitive ports like databases, and regularly review rules and logs — especially if your server uses both IPv4 and IPv6.