UFW Firewall on Ubuntu VPS: Safe Setup Without Lockouts

  • Home
  • Linux
  • UFW Firewall on Ubuntu VPS: Safe Setup Without Lockouts
UFW firewall Ubuntu VPS networking with connected Ethernet cables

UFW firewall Ubuntu VPS networking with connected Ethernet cables
Illustrative photograph by Scott Rodgerson / Unsplash.

Exposing a new VPS to the internet does not mean that every installed service should be reachable from everywhere. Firewalls help make that distinction. Ubuntu’s Uncomplicated Firewall, usually shortened to UFW, provides a comparatively approachable interface for common packet-filtering tasks. Its simplicity is valuable, but the consequences of an incorrect rule can be immediate: one misplaced command may disconnect an administrator from the server.

This guide focuses on safe ordering, minimal public exposure and sensible verification. It assumes a conventional Ubuntu VPS with authorised administrative access; unusual networking designs, containers and cloud-provider firewalls can require additional work.

What UFW does

UFW manages rules that determine whether particular network connections may enter or leave a host. On Ubuntu it operates as a frontend to Linux’s packet-filtering system. The Ubuntu Server firewall documentation provides the official introduction and common command patterns. A host firewall is distinct from any network firewall the infrastructure provider might place in front of the virtual machine.

Rules can refer to a protocol, port, source network and destination. For instance, a public website may accept TCP connections on ports 80 and 443, while its database stays accessible only to local applications. That is a more defensible starting point than opening every port and hoping that each service is configured perfectly.

Step 1: check the existing firewall state

First identify which services actually listen on the network, and inspect the UFW configuration without changing it:

sudo ss -tulpen
sudo ufw status verbose
sudo ufw app list

If UFW is not installed, it may be installed through the distribution’s package manager after checking provider-specific image notes. Do not blindly introduce a second firewall-management framework into an existing complex network policy. Some VPS images already have rules configured through other tools, or rely on upstream filtering.

Step 2: establish the SSH exception before enabling UFW

Determine which TCP port your SSH server currently accepts. For the standard OpenSSH application profile, an example is:

sudo ufw allow OpenSSH
sudo ufw status numbered

If SSH listens on a custom port, permit that specific TCP port instead of assuming the OpenSSH profile matches it. Keep your working session open, inspect your provider’s network firewall and verify that the intended administrative path will remain available. This is the critical ordering principle for any remote firewall change.

Our SSH security guide explains how to confirm authentication before withdrawing existing login methods. Firewalls and SSH authentication address different threats and should be managed together.

Step 3: allow the services the application actually needs

For a conventional public website serving HTTP and HTTPS, the common permissions are:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Port 80 may be necessary for HTTP-to-HTTPS redirects and some certificate-verification methods. Port 443 normally carries encrypted HTTPS traffic. If your application runs behind a reverse proxy, you generally should not expose the application’s private backend port to the public internet.

Other services require case-by-case decisions. A database that serves an application on the same machine often needs no publicly reachable port at all. Administrative dashboards should ideally be restricted by network or identity controls rather than made openly accessible.

Step 4: set a suitable default policy

For many standalone web servers, a default-deny policy for incoming connections and a default-allow policy for outgoing connections is a reasonable starting point:

sudo ufw default deny incoming
sudo ufw default allow outgoing

This example is not universally correct. Systems acting as routers, VPN gateways or container hosts may need forwarding rules, carefully scoped outbound restrictions or additional interfaces. Confirm the intended network architecture before adopting default rules.

Step 5: enable UFW and test from a second session

Only after the administrative access rule is in place should you enable UFW:

sudo ufw enable
sudo ufw status verbose

Do not close the original SSH connection. Open a separate terminal and verify that you can authenticate normally. Then test the website from an independent network if possible. Check both IPv4 and IPv6 access where those address families are in use; it is easy to secure one while overlooking the other.

Step 6: restrict sensitive services by source address

When an administrative service is required only from a stable office IP, a narrowly scoped rule can be preferable to global exposure. For example, a specific source address may be permitted to reach a particular TCP port. Be meticulous with the address range and subnet notation, and never use an invented IP in a live rule.

If your organisation has changing public IP addresses, consider a secure VPN or other documented access system rather than relying on brittle single-address allowlists. Restricting an address you can no longer use can lead to loss of access despite perfectly functioning SSH keys.

Step 7: review and remove redundant rules

Run sudo ufw status numbered to inspect the resulting permissions. Remove obsolete rules deliberately and recheck the numbering after each change, because rule indices can shift. Do not delete a broad rule until you understand whether another rule, a provider firewall or the application depends on it.

Rule ordering and overlapping permissions can be surprising. The goal is a short, reviewable policy that clearly expresses who should reach which service, rather than a pile of historical exceptions that no one can explain.

Step 8: use logging when investigating blocked traffic

UFW supports logging, which can help investigate dropped or rejected connections. Enable a suitable log level temporarily if necessary, inspect the relevant journal or log destination on your Ubuntu installation, and disable excessive logging when troubleshooting is finished. Heavy logging can consume disk capacity and obscure useful events.

Do not interpret every dropped packet as an attack. Public IP addresses receive routine scanning, misrouted traffic and automated requests. Investigate patterns in context with application logs and authorised access attempts.

Why UFW and Docker need special attention

Published container ports can interact with Linux packet-filtering rules in ways that surprise administrators relying only on UFW status. Docker may create its own forwarding and network address translation rules. Review the Docker Engine firewall documentation before exposing containers, and validate the resulting behaviour with tests from outside the server. Never assume that a UFW deny rule alone has secured every published container service.

Where possible, bind internal tools to a private address, use an authenticated reverse proxy and document the intended network boundary. Security depends on the effective traffic path, not just a list of UFW commands.

What if you lock yourself out?

Use the hosting provider’s recovery console to examine UFW status and correct the mistaken rules. On some systems, provider firewalls and guest firewalls must both permit the traffic. Do not repeatedly reset random settings: first distinguish a daemon that is not listening from a firewall that is dropping the connection.

If you are deploying a new server, start with the Ubuntu VPS setup checklist, then apply only the ports your particular service requires. FyreHost’s VPS hosting configurations offer the infrastructure foundation; maintaining an appropriate application firewall policy depends on the management arrangement and the workload.

Checklist before considering the job complete

Confirm that SSH still works through a new session, the public website responds on the intended ports, no unnecessary database or admin endpoints are exposed, and your recovery procedure is documented. Recheck rules after installing control panels, containers, or major network services. A firewall configuration is a maintained policy rather than a one-time command.

Frequently asked questions

Should I enable UFW on every Ubuntu VPS?

A host firewall is a useful security control for many VPS workloads, but the appropriate implementation depends on existing firewall frameworks, provider policies and network roles. Avoid conflicting configurations.

Will enabling UFW disconnect SSH?

It can if the current SSH port is not permitted. Verify the active listening port and add the correct allow rule before enabling UFW, then test from another session.

Which ports does a normal website need?

Typical public HTTP and HTTPS services listen on TCP ports 80 and 443. The application may require additional private connections, but these should not automatically be opened to everyone.

Is UFW enough to protect a Docker server?

Not by itself. Container engines can manipulate packet-filtering and forwarding rules. Review the effective Docker and host firewall configuration and test access from an external network.

Should MySQL port 3306 be open publicly?

Usually not for an application and database running on the same host. Use local connections or carefully designed private networking and least-privilege access when remote database connectivity is necessary.

Can I use a provider firewall together with UFW?

Often, yes. Layered filtering can be beneficial, but both rule sets must be coordinated. A connection can fail if either the upstream firewall or the guest firewall blocks it.

Need Help?

Contact FyreHost 24/7/365 Award-Winning Support to help grow your online business!

FyreHost Help

24/7/365 Through the Chat Widget and WhatsApp

Customer Support Chat

24/7/365 Through the Chat Widget and WhatsApp

Customer Support Email

24/7/365 Through the Panel via Support Tickets

FyreHost Logo White

Get reliable hosting, domains, VPS services. Trust us to manage all your online needs from domain registration to VPS hosting.

Connect with us:

Copyright © 2026 FyreHost LTD Registered in UK with Company Number: 17242968, All Rights Reserved.