A properly configured firewall is one of the most important layers of security for any internet-facing server. UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules that ships pre-installed on Ubuntu. Its defaults are sensible: block all incoming connections, allow all outgoing connections. No outside traffic reaches your server unless you explicitly open a port.
This guide covers how to configure a UFW firewall on Ubuntu 18.04, from setting default policies and application profiles to writing allow and deny rules for specific ports, IPs, and subnets.
<strong>Prerequisite:</strong> You need sudo access.
If UFW is not installed, add it with:
bashsudo apt install ufw
Check the current status:
bashsudo ufw status verbose
Application profiles. When you install a service with apt, it registers a profile in /etc/ufw/applications.d/. List all available profiles:
bashsudo ufw app list
Use a profile name or a port number interchangeably when writing rules. To see what ports a profile covers:
bashsudo ufw app info 'Nginx Full'
Allow SSH before enabling UFW. This is the most critical step. If you enable UFW without an SSH rule and you are on a remote server, you lose access immediately:
bashsudo ufw allow ssh
If your SSH daemon runs on a non-standard port, use the port number instead, for example sudo ufw allow 4422/tcp.
Enable UFW:
bashsudo ufw enable
Type y at the prompt to confirm.
Common service ports:
bashsudo ufw allow httpsudo ufw allow httpssudo ufw allow 8080/tcp
Port ranges. When writing range rules, you must specify tcp or udp — UFW will not accept a range without a protocol. This is useful when a service needs a block of consecutive ports:
bashsudo ufw allow 7100:7200/tcpsudo ufw allow 7100:7200/udp
Specific IP and port. Allow a single trusted IP to access a specific port only:
bashsudo ufw allow from 64.63.62.61 to any port 22
Subnet. Allow a whole IP range to reach a specific port:
bashsudo ufw allow from 192.168.1.0/24 to any port 3306
Specific network interface. Restrict access to a port on one interface only:
bashsudo ufw allow in on eth2 to any port 3306
Deny rules use the same syntax as allow rules — just replace allow with deny. To block an entire subnet from reaching your server:
bashsudo ufw deny from 23.24.25.0/24
Delete rules by number. List active rules with their index:
bashsudo ufw status numbered
Then delete by number, for example rule 3:
bashsudo ufw delete 3
Delete by rule specification:
bashsudo ufw delete allow 8069
Disable UFW without deleting your rules — they reload the next time you enable it:
bashsudo ufw disable
Reset UFW to wipe all rules and start fresh:
bashsudo ufw reset
Your UFW firewall on Ubuntu 18.04 is now configured with allow and deny rules matched to your server’s needs. Run sudo ufw status numbered regularly to audit active rules and remove any that are no longer needed. Leave a comment below if you run into any issues.
VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…
PostgreSQL (also called Postgres) is a free, open-source, object-relational database management system with a strong reputation…
VMware Workstation Player is a mature, stable virtualization platform that lets you run multiple isolated operating…
UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules on Ubuntu. It ships pre-installed…
Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…
Rocket.Chat is a free, open-source team communication platform built with the Meteor framework. It is a…