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:
- Know which port SSH listens on (
sudo ss -tlnp | grep ssh). If you changed it from 22, allow the custom port, notOpenSSH. - Keep a second SSH session open while you test.
- 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:
| Service | Command | Notes |
|---|---|---|
| Website (Nginx) | sudo ufw allow 'Nginx Full' | Opens 80 and 443 |
| Website (Apache) | sudo ufw allow 'Apache Full' | Opens 80 and 443 |
| HTTPS only | sudo ufw allow 443/tcp | Keep 80 open if you use Let's Encrypt HTTP-01 |
| Minecraft Java | sudo ufw allow 25565/tcp | Bedrock uses 19132/udp |
| WireGuard | sudo ufw allow 51820/udp | Match your ListenPort |
| Port range | sudo ufw allow 30000:30100/tcp | Ranges 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-USERchain, 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.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — Intel | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Business websites, small apps, scheduled jobs and bots | Order |
| VPS 2 — Intel | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,169/month | Business software, mid-size databases, several sites on one server | Check stock |
| VPS 3 — Intel | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,249/month | ERP/CRM stacks, heavier databases, multiple production apps | Check stock |
| VPS 4 — Intel | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,299/month | High-memory workloads and consolidated production servers | Check 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.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — AMD | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Websites, small APIs, bots, staging and VPN servers | Order |
| VPS 2 — AMD | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,299/month | WooCommerce, Node.js/Python backends, Docker Compose stacks, CI runners | Order |
| VPS 3 — AMD | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,499/month | Production databases, high-traffic sites, multi-container apps | Order |
| VPS 4 — AMD | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,899/month | Large production workloads, SaaS platforms, analytics, in-memory caches | Order |
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
| Symptom | Cause | Fix |
|---|---|---|
| Locked out of SSH | SSH not allowed before enabling, or wrong port allowed | Use the provider's web console, run sudo ufw allow OpenSSH (or your port), or sudo ufw disable |
| Port still reachable after deny | Docker published port, or IPv6 not covered | Bind to 127.0.0.1, check IPV6=yes, rescan |
| Website down after enabling | 80/443 not allowed | sudo ufw allow 'Nginx Full' |
| Let's Encrypt renewal fails | Port 80 closed | Allow 80/tcp or switch to DNS-01 validation |
| Rule seems ignored | An earlier rule matches first | sudo 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?
Will enabling UFW disconnect my SSH session?
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?
sudo ufw allow OpenSSH or sudo ufw disable. This is why you should confirm console access before changing firewall rules.Does UFW protect IPv6?
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?
127.0.0.1, avoid publishing internal services, or filter in the DOCKER-USER chain.Should I open my database port in UFW?
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?
How do I see what UFW is blocking?
sudo ufw logging low and read /var/log/ufw.log (or journalctl -k | grep UFW). Blocked packets appear with [UFW BLOCK].