Home / Guides

Server guide

UFW firewall basics for an Ubuntu server

This guide is for small‑business owners, agencies, and hosting teams managing Ubuntu VPS or dedicated servers. You will get a clear overview of UFW and a practical checklist to secure your server.

8 min readUpdated November 2026 by the RootManage teamWorks on Ubuntu, Debian, AlmaLinux and Rocky unless noted

The short answer

UFW (Uncomplicated Firewall) is Ubuntu’s default front‑end for iptables, providing an easy way to allow or deny traffic. To set it up, install UFW if needed, enable it, define required ports, and verify the rules are active.

What is UFW and why it matters

UFW, short for Uncomplicated Firewall, is a front‑end for the Linux netfilter framework. It provides a simple command‑line interface that abstracts the complex iptables syntax, making it easier for administrators to define inbound and outbound traffic rules. By default, UFW blocks all incoming connections while allowing outgoing traffic, which aligns with the principle of least privilege. This default stance helps protect services that should not be publicly reachable, reducing the attack surface of an Ubuntu server.

When correctly configured, UFW becomes a gatekeeper for network traffic, allowing only the ports and protocols required by your applications. It logs denied attempts, giving you visibility into potential probing or malicious activity. Because it integrates with systemd and respects persistent configuration, the firewall rules survive reboots, ensuring continuous protection. For small businesses and hosting teams, UFW offers a balance of ease‑of‑use and sufficient control to secure server communications without deep iptables expertise.

  • Simple enable/disable commands
  • Default deny inbound, allow outbound policy
  • Easy rule syntax (e.g., allow ssh)
  • Integrated logging for blocked attempts
  • Persistent across reboots
terminal
The following commands illustrate basic UFW usage. Review them before execution:

sudo ufw enable
sudo ufw allow ssh
sudo ufw allow http
sudo ufw status verbose

After running the commands, verify that the firewall reports the expected rules and that essential services like SSH and HTTP remain reachable.

Checking the current firewall status

To check the firewall status on an Ubuntu server, start by querying UFW with the built‑in status command. Running ufw status will tell you whether the firewall is active or inactive, and it will also display a concise list of the rules that are currently enforced. The output is easy to read: allowed ports are shown with the protocol and direction, while denied traffic appears with a simple “DENY”. This quick glance helps you verify that the basic protection layer is in place before making any changes.

If you need more detail, add the verbose flag. The command ufw status verbose expands the information to include the default policies for incoming, outgoing, and routed traffic, as well as logging settings. Reviewing these defaults ensures that your server’s baseline behavior aligns with your security expectations. Always read the output carefully; it shows exactly which ports are open, which IP ranges are permitted, and whether any custom rules have been added manually or by applications.

  • Run ufw status to see active/inactive state
  • Use ufw status verbose for default policies and logging details
  • Identify allowed ports and protocols in the rule list
terminal
# Example commands (read before executing)
ufw status
ufw status verbose

Look for the word “active” in the first line to confirm the firewall is running. Then scan the rule table for any unexpected open ports or services. If defaults are set to deny incoming and allow outgoing, only the listed ports should be reachable from the network.

Installing and enabling UFW

Begin by checking whether UFW is already installed on your Ubuntu server. Use the package manager to query its status; if the package is missing, you can safely install it from the official repositories. Installing from the repository ensures you receive updates and security patches automatically. After installation, you should define a default policy that denies all incoming traffic. This default‑deny stance blocks unsolicited connections while allowing you to open only the ports your services require, reducing the attack surface from the start.

Once the default policy is set, enable the firewall. Enabling UFW activates the rules you have defined and starts the service at boot, ensuring continuous protection. Before enabling, it is a good practice to verify that essential ports such as SSH (port 22) are allowed; otherwise you may lock yourself out of the server. After activation, UFW will log any blocked attempts, which you can review later to fine‑tune your rule set.

  • Check if UFW is installed: dpkg -l ufw
  • Install UFW from the official repository if needed
  • Set a default deny policy for incoming traffic
  • Allow required services (e.g., SSH) before enabling
  • Enable UFW to start at boot
terminal
sudo apt-get update && sudo apt-get install -y ufw
sudo ufw default deny incoming
sudo ufw allow ssh
sudo ufw enable

After running the commands, UFW should report that the firewall is active and the default policy is set to deny. Verify the status with ufw status verbose to see the active rules and ensure SSH is allowed so you remain connected.

Allowing required services

When you enable UFW on an Ubuntu server, you must explicitly allow traffic for the services that your users need to reach. Start with SSH, because you will lose remote access if it is blocked. Use the default port (22) unless you have moved SSH to a non‑standard port, in which case allow that specific port. Next, open HTTP (port 80) and HTTPS (port 443) so web browsers can connect to your site. If your application uses additional ports – for example a database proxy on 3306 or a custom API on 8080 – add rules for each of those ports, limiting the source address range when possible.

After the basic services are permitted, verify that the firewall state reflects the new rules and that no unintended ports are open. Use ufw status verbose to see a clear list of allowed and denied ports, along with their protocols. If a rule is missing or too permissive, adjust it before moving the server into production. Always take a snapshot or backup before modifying firewall settings on a live system, and test connectivity from a client machine after each change.

  • Allow SSH: ufw allow ssh or ufw allow 2222/tcp for a custom port
  • Allow web traffic: ufw allow http and ufw allow https
  • Add custom application ports with ufw allow <port>/tcp
terminal
# Example commands – read them before running
ufw allow ssh
ufw allow http
ufw allow https
ufw allow 8080/tcp   # custom API port
ufw reload

Check the output of ufw status verbose to confirm that only the intended ports are listed as ALLOW and that the default policy remains set to deny incoming connections.

Testing the firewall configuration

After configuring UFW, test the rules to confirm the firewall behaves as expected. Use a client machine or a remote host to attempt connections to ports you have allowed, such as SSH on port 22 or a web server on port 80. Simple tools like nc (netcat) or telnet can open a TCP connection and report success or failure. If the connection succeeds, the rule is active and traffic is permitted. Perform the same test for ports you intend to block; a timeout or immediate refusal indicates the firewall is denying access.

For internal verification, you can also use the server itself to probe its own ports. The command sudo ufw status verbose lists all active rules, showing which ports are allowed and from which sources. Combine this with ss -tlnp to see which services are listening. By cross‑checking the UFW output with the listening sockets, you can ensure that no unintended ports are exposed. Remember to run these checks from a trusted network and to keep a snapshot before making further changes.

  • Use nc -zv <server_ip> <port> to test TCP connectivity.
  • Run telnet <server_ip> <port> for a quick connection check.
  • Check active UFW rules with sudo ufw status verbose.
  • List listening services with ss -tlnp.
terminal
sudo ufw status verbose
ss -tlnp
nc -zv 203.0.113.10 22
nc -zv 203.0.113.10 8080

Successful connections should appear as "open" or "succeeded" for allowed ports, while blocked ports will show a timeout or connection refused message. The UFW status should list the allowed ports and their source restrictions, and ss should confirm that only the intended services are listening.

Common mistakes to avoid

When configuring UFW, avoid the temptation to open every port with a single rule like allow 0.0.0.0/0. This defeats the purpose of a firewall and leaves services exposed to the internet. Instead, add rules for each service you actually need, specifying the correct protocol and source address range when possible. A narrow rule set reduces the attack surface and makes troubleshooting easier, because you know exactly which ports are intended to be reachable.

A common oversight is forgetting to allow SSH before enabling the firewall. If ufw enable runs without an existing rule for port 22 (or your custom SSH port), you will lock yourself out of the server. Always add the SSH rule first, then enable UFW. Also, never disable UFW on a production box without a recent snapshot or backup; a mis‑step could expose the system or break connectivity, and restoring from backup is the safest fallback.

  • Opening all ports with a blanket rule
  • Enabling UFW before permitting SSH access
  • Disabling UFW without a recent snapshot or backup
  • Leaving default policies set to "allow"
  • Using overly broad source IP ranges
terminal
ufw status verbose   # Shows current rules and default policies; review before adding or removing rules

After running the status command, verify that the default policy is set to "deny (incoming)" and that only the required ports, such as SSH, are listed as allowed. Adjust any unexpected entries before proceeding.

Maintaining a secure firewall over time

A firewall is only as good as its last review. Schedule a quarterly audit of all UFW rules to confirm they still match the services you run. Remove any rules that no longer apply, and tighten overly permissive entries. During the review, verify that default policies (deny incoming, allow outgoing) remain unchanged, and that any custom ports are still required. Document any adjustments in a change log so you have a clear history of what was modified and why.

Enable UFW logging and inspect the logs regularly. Look for repeated denied attempts, especially from the same source IP, which may indicate a scanning or brute‑force activity. Rotate logs to avoid excessive disk usage and keep at least a week’s worth for analysis. Before making any rule changes, create a snapshot or backup of the server configuration. This provides a quick rollback point if a new rule unintentionally blocks legitimate traffic.

  • Review rules every 90 days.
  • Check UFW log files for unusual deny patterns.
  • Remove obsolete or overly broad rules.
  • Record changes in a simple log file.
  • Take a snapshot or backup before editing rules.
terminal
ufw status verbose
ufw logging on
# Example: add a rule after review
ufw allow from 203.0.113.0/24 to any port 22 proto tcp

After running the commands, verify that the status output lists only the intended rules and that logging is enabled. Check the log file (usually /var/log/ufw.log) for recent entries to confirm that denied traffic is being recorded.

If you would rather have engineers do this for you, see Linux server management, Windows server management or emergency malware cleanup.

Questions people ask

How do I allow SSH access without locking myself out?

To keep SSH open, first allow the SSH port before enabling UFW. Run `sudo ufw allow ssh` (or `sudo ufw allow 22/tcp`). Then enable the firewall with `sudo ufw enable`. Verify the rule with `sudo ufw status`. This order prevents accidental lock‑out.

Can I use UFW with other firewall tools like iptables?

Yes. UFW is a front‑end for netfilter/iptables, so it ultimately adds rules to the iptables chains. You can run both, but avoid duplicate or conflicting rules. Keep UFW as the primary manager and let it handle iptables entries; use raw iptables only for advanced cases not covered by UFW.

What log file does UFW write to and how can I monitor it?

UFW writes logs to `/var/log/ufw.log`. Ensure the log level is set (e.g., `sudo ufw logging on`). You can watch the file in real time with `sudo tail -f /var/log/ufw.log` or pipe it to a log monitoring tool such as `journalctl -u ufw` for easier analysis.

How should I handle temporary rule changes for troubleshooting?

For temporary changes, add the rule with the `comment` option and note the purpose, then test. When done, remove it with `sudo ufw delete allow <port>` or `sudo ufw delete deny <port>`. Always document the change and verify that normal traffic resumes before making the modification permanent.

Want a person to look at it?

Tell us what you see. We reply with what we think is wrong and what it would take to fix it.