How to Configure UFW Firewall on Ubuntu: Rules That Actually Matter

Most Ubuntu servers ship with no firewall rules at all. That means every service you install starts answering the internet the moment it starts, whether you meant to expose it or not. Learning to configure UFW firewall Ubuntu servers use is less about memorizing commands and more about deciding, port by port, what the outside world is allowed to reach. UFW, short for Uncomplicated Firewall, is a front end for iptables and nftables that turns that decision into a short list of readable rules.

This guide walks through a setup that works on a plain Ubuntu server, a VPS, or a home lab machine. You will check what is listening, set safe defaults, allow SSH before you switch anything on, open only the ports your services need, add rate limiting, and set up logging you will actually read. Follow the order here and you will not lock yourself out.

Start by seeing what is actually listening

Before you write a single rule, find out what your server already exposes. Run ss -tulpn and read the output line by line. Each line shows a service, the address it binds to, and the port it answers on. A service bound to 127.0.0.1 only answers local connections, so it is not reachable from the internet. A service bound to 0.0.0.0 or :: answers on every interface, and that is the one your firewall has to deal with.

On a fresh install you will usually see SSH on port 22 and little else. On a server that has run for a while, you may find a database or a control panel you forgot about. Write the list down. Every open port on it needs either a rule that allows it on purpose or a fix that closes it. If a service does not need to face the internet, bind it to localhost or stop it instead of hiding it behind a firewall rule. Check the current state with sudo ufw status verbose and open a second SSH session before you change anything.

How to Configure UFW Firewall Ubuntu Without Locking Yourself Out

Install UFW first if it is not there already with sudo apt update && sudo apt install ufw. Then allow SSH. The plain form is ufw allow ssh, which opens port 22, or use the profile name with sudo ufw allow OpenSSH. If you run SSH on a custom port, use sudo ufw allow 2222/tcp with your real port number instead. Only then run sudo ufw enable. Keep a console window open until a fresh SSH login works. That extra login test costs thirty seconds and saves the most common UFW rescue job there is. Readers setting up a new machine from scratch can combine this with the wider steps in our guide to hardening a fresh Ubuntu server, where the firewall is one step among several that belong together.

Set the defaults before you add rules

UFW ships with defaults that already lean the right way, but say them out loud anyway so your rule list reads clearly later. Run sudo ufw default deny incoming and sudo ufw default allow outgoing. The first blocks everything you have not explicitly allowed. The second lets your server reach out for updates and package downloads without a rule for each destination. Most servers want exactly this pair, and the ufw default deny approach is the test for every rule that follows: nothing gets in unless you wrote a rule for it.

Check whether IPv6 is on. Open /etc/default/ufw and look for the IPV6=yes line. If your server has an IPv6 address and this line says no, UFW will only filter IPv4 and leave the IPv6 door open. Set it to yes, then run sudo ufw reload. Also note that deny drops the packet quietly, so a scanner gets no answer at all, while reject sends a reply saying the port is closed.

Open only the ports your server needs

With defaults in place, add one rule per service and nothing else. A web server needs sudo ufw allow 80/tcp and sudo ufw allow 443/tcp, or the shorter sudo ufw allow 'Nginx Full' if you use the Nginx profile. Anything that only your server itself uses, like a local MySQL or Redis instance, gets no rule at all. No rule means no access from outside, which is the whole point.

Test every request to open a port the same way. Ask who needs to reach this service, from where, and over which protocol. If the answer is “only my office IP”, write the rule that way: sudo ufw allow from 203.0.113.10 to any port 3306, with your real office address in place of the example. UFW firewall rules on Ubuntu can also be tied to a single interface, which helps on servers with both a public and a private network. Run sudo ufw status numbered after every batch of changes. If a rule surprises you, remove it with sudo ufw delete 3, using the real rule number. A short rule list you understand beats a long one you inherited and never reviewed.

Rate limit SSH instead of moving it and hoping

Changing the SSH port cuts down log noise, but it is not security by itself. Anyone scanning your server will find the new port in seconds. What actually slows a brute force attack is rate limiting, and UFW has it built in. Run sudo ufw limit ssh or sudo ufw limit 2222/tcp for a custom port. The limit rule allows a small burst of connection attempts from one address and then blocks further attempts from that address for a while. Real users who type their password wrong twice never notice it. A script trying hundreds of passwords a minute hits the wall fast.

Rate limiting works best next to key based login. When passwords are off and only SSH keys work, a brute force script has nothing to guess against, and the limit rule is a second layer rather than the only one. Our walkthrough of key-only SSH login with custom ports and fail2ban covers that side in full. For services beyond SSH, fail2ban adds the same idea at the application level, and our UFW and fail2ban setup guide for Ubuntu shows how the two tools share the same firewall instead of fighting over it. Start with the UFW limit rule, confirm it works, and only then add fail2ban.

Turn on logging and read it

A firewall you never check is a habit, not a control. Turn logging on with sudo ufw logging on and look at what it records. UFW writes blocked packets to the kernel log, which you can read with sudo grep UFW /var/log/syslog or sudo journalctl -k | grep UFW depending on your Ubuntu version. The default log level records blocked packets without flooding the disk. If the volume gets noisy, sudo ufw logging low trims it back.

Read the log once a week for five minutes. You are looking for patterns, not individual lines: the same address probing many ports, a sudden rise in blocked SSH attempts, or a blocked packet to a port you thought was open. The first pattern tells you someone is scanning. The second tells you your rate limit is earning its keep. The third tells you a rule is wrong, and it is better to find that in a log review than from a user who cannot reach a service.

Test every change from a second session

Every firewall change deserves a test before you call it done. Open a new SSH session while the old one is still connected. If the new login works, your allow rules are right. Then test the service you just opened from outside the server, not from localhost. A port that answers on localhost and stays silent on the public interface is usually a bind address problem, not a firewall problem, and testing from outside tells the two apart. Test the blocks too: a closed port should time out from another machine.

Finally, review the rule set on a schedule. Once a month, run sudo ufw status numbered, compare it with the list of services from the first section, and delete rules for services that no longer run. Old rules are how forgotten test services stay exposed for months, and this is the step people skip when they configure UFW firewall Ubuntu servers once and never look at them again. For a wider monthly routine, the production checklist for Ubuntu 24.04 puts the firewall review next to updates, user accounts, and service checks in one pass.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.