Security13 min read•Updated November 2026

How to Set Up a UFW Firewall on a Linux VPS

Quick answer

UFW (Uncomplicated Firewall) is the standard host firewall on Ubuntu. The safe order is: allow SSH first (sudo ufw allow OpenSSH), set the defaults to deny incoming and allow outgoing, allow only the ports your services need (for example sudo ufw allow 'Nginx Full'), then run sudo ufw enable and check with sudo ufw status verbose. Restrict admin and database ports to trusted IP addresses instead of opening them to everyone.

A freshly provisioned VPS with a public IP address starts receiving automated port scans and login attempts within minutes. A host firewall reduces what those scans can reach: only the services you deliberately expose stay open, and everything else is dropped before it reaches an application. On Ubuntu and Debian the simplest tool for this is UFW, a front end that writes the underlying netfilter (iptables/nftables) rules for you.

This guide shows a safe, repeatable UFW setup for a typical VPS running a website, an API or a database, including the mistakes that lock people out and how to recover if it happens.

What UFW Does (and Doesn't Do)

UFW filters packets arriving at your server based on rules: port, protocol, source address and interface. It is a host firewall, so it protects that one machine. Keep its limits in mind:

  • It does not stop volumetric DDoS attacks. Those must be filtered upstream, before traffic reaches your server's network port.
  • It does not fix vulnerable applications. An open port running outdated software is still a risk.
  • It does not replace SSH hardening, updates or backups. It is one layer in the checklist covered in our VPS security checklist.

Before You Start: Avoid Locking Yourself Out

The classic mistake is enabling UFW with a default-deny policy before allowing SSH. Your current session usually survives, but the next connection is refused. Before running ufw enable:

  1. Know which port SSH listens on (sudo ss -tlnp | grep ssh). If you changed it from 22, allow the custom port, not OpenSSH.
  2. Keep a second SSH session open while you test.
  3. Make sure you have out-of-band console access from your hosting control panel (VNC or serial console) in case you need to undo a rule.

Step 1: Install and Check UFW

UFW is preinstalled on Ubuntu. On Debian, install it:

sudo apt update
sudo apt install -y ufw
sudo ufw status verbose

A new install reports Status: inactive. If your VPS uses IPv6, confirm IPV6=yes in /etc/default/ufw so every rule is created for both IPv4 and IPv6. Otherwise your IPv6 address could stay wide open.

Step 2: Set Default Policies

sudo ufw default deny incoming
sudo ufw default allow outgoing

This is the right starting point for almost every server: nothing comes in unless a rule allows it, and the server can still reach package mirrors, APIs and DNS. Tighten outgoing traffic later only if you have a specific compliance requirement, because an outbound deny breaks updates in surprising ways.

Step 3: Allow SSH

# Default port 22
sudo ufw allow OpenSSH
# Or a custom port
sudo ufw allow 2222/tcp

If you always administer the server from a fixed IP (an office line or a VPN), allow SSH only from that address. This removes almost all brute-force noise:

sudo ufw allow from 198.51.100.25 to any port 22 proto tcp

Only do this if the address is genuinely static; otherwise a change at your ISP locks you out.

Step 4: Allow Your Services

Open only what you actually run. Common examples:

ServiceCommandNotes
Website (Nginx)sudo ufw allow 'Nginx Full'Opens 80 and 443
Website (Apache)sudo ufw allow 'Apache Full'Opens 80 and 443
HTTPS onlysudo ufw allow 443/tcpKeep 80 open if you use Let's Encrypt HTTP-01
Minecraft Javasudo ufw allow 25565/tcpBedrock uses 19132/udp
WireGuardsudo ufw allow 51820/udpMatch your ListenPort
Port rangesudo ufw allow 30000:30100/tcpRanges need an explicit protocol

See the available application profiles with sudo ufw app list. Profiles are created by installed packages, so the list grows as you install software.

Step 5: Restrict Databases and Admin Panels

Database ports (MySQL 3306, PostgreSQL 5432, Redis 6379, MongoDB 27017) should almost never be open to the internet. Two good patterns:

  • Same server: the application connects over 127.0.0.1. Bind the database to localhost and add no UFW rule at all.
  • Separate app server: allow only that server's IP.
sudo ufw allow from 203.0.113.20 to any port 5432 proto tcp

Treat control panels, monitoring dashboards and metrics exporters the same way: allow your IP only, or place them behind a VPN.

Step 6: Enable and Verify

sudo ufw enable
sudo ufw status numbered

Open a new SSH session to prove you can still log in before closing the old one. Then check from outside that only the intended ports answer, for example with our port checker or from another machine:

nc -zv your-server-ip 22
nc -zv your-server-ip 443
nc -zv your-server-ip 3306   # should fail

Rate-Limiting SSH

UFW has a built-in rate limit that denies an address after six connection attempts within 30 seconds:

sudo ufw delete allow OpenSSH
sudo ufw limit OpenSSH

This is a coarse control. For targeted bans based on failed logins, combine it with Fail2ban or switch SSH to key-only authentication, which makes password guessing pointless.

Managing Rules

sudo ufw status numbered        # list with numbers
sudo ufw delete 3               # delete rule 3
sudo ufw delete allow 8080/tcp  # delete by specification
sudo ufw insert 1 deny from 192.0.2.50   # put a block above existing allows
sudo ufw reload
sudo ufw logging low            # log blocked packets to /var/log/ufw.log

Rule order matters: UFW applies the first matching rule. To block one address that would otherwise match a broad allow, insert the deny at position 1.

UFW and Docker: The Important Caveat

Docker writes its own iptables rules for published ports, and those rules are evaluated before UFW's. A container started with -p 5432:5432 is reachable from the internet even if UFW has no allow rule for 5432. This surprises many people.

Safer options:

  • Publish container ports on localhost only: -p 127.0.0.1:5432:5432, or in Compose "127.0.0.1:5432:5432". Then put Nginx in front for anything that must be public.
  • Do not publish internal services at all; let containers talk over a Docker network.
  • If you must filter published ports, add rules to Docker's DOCKER-USER chain, which Docker evaluates before its own rules.

After any change, verify from outside with a port scan rather than trusting ufw status.

SkyRainCloud Plans for This Workload

A host firewall like UFW runs on every plan with negligible overhead. Pick the plan for your workload; every SkyRainCloud VPS gives you full root access, which UFW requires.

Intel Xeon Platinum 8269CY VPS

KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — Intel28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthBusiness websites, small apps, scheduled jobs and botsOrder
VPS 2 — Intel416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
VPS 3 — Intel832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
VPS 4 — Intel1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,299/monthHigh-memory workloads and consolidated production serversCheck stock

AMD EPYC 7C13 VPS

Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — AMD28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthWebsites, small APIs, bots, staging and VPN serversOrder
VPS 2 — AMD416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,299/monthWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
VPS 3 — AMD832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,499/monthProduction databases, high-traffic sites, multi-container appsOrder
VPS 4 — AMD1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,899/monthLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

Prices are monthly in INR as listed on the SkyRainCloud store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.

Troubleshooting

SymptomCauseFix
Locked out of SSHSSH not allowed before enabling, or wrong port allowedUse the provider's web console, run sudo ufw allow OpenSSH (or your port), or sudo ufw disable
Port still reachable after denyDocker published port, or IPv6 not coveredBind to 127.0.0.1, check IPV6=yes, rescan
Website down after enabling80/443 not allowedsudo ufw allow 'Nginx Full'
Let's Encrypt renewal failsPort 80 closedAllow 80/tcp or switch to DNS-01 validation
Rule seems ignoredAn earlier rule matches firstsudo ufw status numbered, then insert or reorder

A Sensible Baseline for a Web Server

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status verbose

That is the whole firewall for many websites and APIs: SSH (rate-limited), HTTP and HTTPS, and nothing else.

Frequently Asked Questions

Is UFW enough to secure a VPS?
No single tool is enough. UFW limits which ports are reachable; you still need key-based SSH, regular updates, least-privilege users, backups and application security. Volumetric DDoS attacks must be filtered upstream of the server.
Will enabling UFW disconnect my SSH session?
Your current session normally stays up, but new connections are refused unless SSH is allowed. Always run sudo ufw allow OpenSSH (or your custom SSH port) before sudo ufw enable, and test with a second session.
How do I recover if I locked myself out?
Log in through your hosting provider's out-of-band console (VNC or serial console), then run sudo ufw allow OpenSSH or sudo ufw disable. This is why you should confirm console access before changing firewall rules.
Does UFW protect IPv6?
Yes, when IPV6=yes is set in /etc/default/ufw (the default on current Ubuntu). Rules you add then apply to both IPv4 and IPv6. Check this on any server with an IPv6 address.
Why can people reach my Docker container even though UFW blocks the port?
Docker inserts iptables rules that are evaluated before UFW's. Bind published ports to 127.0.0.1, avoid publishing internal services, or filter in the DOCKER-USER chain.
Should I open my database port in UFW?
Usually not. If the app runs on the same server, connect over localhost and leave the port closed. If the app is on another server, allow only that server's IP address.
What is the difference between ufw allow and ufw limit?
allow accepts all matching connections. limit accepts them but denies an address that opens six or more connections within 30 seconds, which slows down brute-force attempts on SSH.
Does UFW slow down the server?
No measurable amount for normal workloads. Packet filtering happens in the kernel, and a handful of rules costs essentially nothing compared to your application.
How do I see what UFW is blocking?
Enable logging with sudo ufw logging low and read /var/log/ufw.log (or journalctl -k | grep UFW). Blocked packets appear with [UFW BLOCK].
Can I use UFW on a SkyRainCloud VPS?
Yes. Every SkyRainCloud Linux VPS comes with full root access, so you can install and configure UFW yourself on Ubuntu or Debian. Keep the console in the client area handy while you set it up.
Instant UPI • ₹649/mo
Fast KVM Cloud Provisioning