On this page
- Why run a firewall inside the server?
- How do I install UFW and check its state?
- How do I avoid locking myself out?
- Which rules should I add before enabling UFW?
- How do I enable UFW and prove SSH still works?
- How do I open web and application ports?
- How do I rate limit SSH and remove rules?
- Why do Docker ports ignore UFW?
- How do I troubleshoot and log UFW?
- Questions
Why run a firewall inside the server?
Your VPS has a public IPv4 and IPv6 address the moment it boots, and BitcoinVPS does not put a separate network firewall in front of it, so the filtering happens inside the operating system. Any service that listens on 0.0.0.0 or :: is reachable from the whole internet: a database you installed for testing, a metrics exporter, a development web server on port 3000. UFW (Uncomplicated Firewall) is a small front end that turns short commands into kernel packet filter rules. On current Ubuntu and Debian releases it writes those rules through the nftables backend of iptables, so you get modern filtering with a friendly syntax.
If you would rather write raw nftables rules, the firewall rules tutorial covers that route. Pick one tool per server: running UFW, a hand written nftables ruleset and firewalld together makes it very hard to know which rule wins.
How do I install UFW and check its state?
Ubuntu 24.04 and 22.04 images include UFW but leave it inactive. Debian 12 and 13 do not install it by default. Update the package index and install it on either system:
sudo apt update
sudo apt install ufw
ufw version
sudo ufw status verboseA fresh install reports Status: inactive. That is what you want at this point: nothing is filtered yet, so you can add rules calmly before switching it on. Work as a normal user with sudo rights (see create a sudo user) and keep your current SSH session open for the whole guide.
How do I avoid locking myself out?
Arm a timed undo before you enable anything. There is no browser console on our servers, so if the firewall blocks SSH your only way back would be a reinstall, which erases the disk. A transient systemd timer removes that risk:
sudo systemd-run --on-active=5min --unit=ufw-undo /usr/sbin/ufw disable
systemctl list-timers ufw-undo.timerThe timer will switch UFW off in five minutes no matter what happens to your session. When you have confirmed a brand new SSH login works, cancel it:
sudo systemctl stop ufw-undo.timerReuse the same pattern every time you change rules that could touch SSH.
Which rules should I add before enabling UFW?
Start with SSH, then set the defaults. The OpenSSH application profile opens TCP 22 for both IPv4 and IPv6:
sudo ufw allow OpenSSH
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw app listIf you moved SSH to another port, allow that number instead, for example sudo ufw allow 2222/tcp. Check that IPv6 filtering is on, otherwise rules only protect the IPv4 address:
grep IPV6 /etc/default/ufwThe line should read IPV6=yes, which is the default on both distributions.
How do I enable UFW and prove SSH still works?
Enable the firewall, then open a second terminal and log in again before you trust it:
sudo ufw enable
sudo ufw status numberedUFW warns that existing connections may be disrupted; answer y. Your current session normally survives because established traffic is accepted. From your own computer, in a new window, run:
ssh deploy@198.51.100.24If that login succeeds, stop the undo timer as shown above. If it hangs, do nothing: wait for the timer, which disables UFW, then review the rule list. You can also confirm the service starts at boot with systemctl is-enabled ufw.
How do I open web and application ports?
Add one rule per service you actually run. For a site served by Nginx, Apache or Caddy:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 443/udp comment "HTTP/3"Limit a port to one trusted address, such as a database reached only from your office or another server:
sudo ufw allow from 203.0.113.7 to any port 5432 proto tcp
sudo ufw allow from 2001:db8:4f2a::7 to any port 5432 proto tcpOpen a range for a game server or a WireGuard tunnel:
sudo ufw allow 27015:27030/udp
sudo ufw allow 51820/udpThe comment keyword is optional but helps a lot when you read ufw status months later.
How do I rate limit SSH and remove rules?
The limit action denies an address that opens six or more connections within thirty seconds, which slows password guessing without extra software:
sudo ufw limit OpenSSHFor stronger protection add fail2ban or CrowdSec. To delete a rule, list rules with numbers and remove by number, or repeat the rule with delete:
sudo ufw status numbered
sudo ufw delete 4
sudo ufw delete allow 8080/tcpNumbers shift after each deletion, so list again before deleting a second rule. Use sudo ufw reload after editing files under /etc/ufw by hand.
Why do Docker ports ignore UFW?
Docker writes its own NAT rules that run before the UFW chains, so a container published with -p 8080:80 is reachable even when UFW denies 8080. The safe habit is to publish container ports on the loopback address and place a reverse proxy before the app:
docker run -d -p 127.0.0.1:8080:80 nginxIn Compose files write "127.0.0.1:8080:80" the same way. The Docker Engine tutorial explains the rest of the setup. After changes, check what listens publicly with sudo ss -tulpn and compare it with your rule list.
How do I troubleshoot and log UFW?
Turn on low level logging to see what the firewall drops:
sudo ufw logging low
sudo journalctl -k --since "10 min ago" | grep UFWEach [UFW BLOCK] line shows the source address (SRC=) and destination port (DPT=). If a service still fails, test it from outside with nc -vz 198.51.100.24 443 and on the server with ss -tlnp | grep 443; a port nobody listens on looks the same as a blocked one. To start again from a clean slate, run sudo ufw reset, which disables UFW and backs up the old rule files, then rebuild the rules in the order shown above.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-1 | a firewall adds no real load, any size works | 1 vCPU, 1 GB RAM, 25 GB NVMe | $3.60/mo | $43.20 for 12 months |
Questions about UFW firewall on Ubuntu and Debian
Does UFW protect IPv6 as well?
Yes, as long as IPV6=yes is set in /etc/default/ufw. Each rule you add without a specific address then creates a matching IPv6 rule, marked (v6) in the status output.
What happens if I enable UFW without allowing SSH?
New SSH connections are dropped. Your current session usually stays alive, so add sudo ufw allow OpenSSH at once. If the session is gone, the timed undo described above switches UFW off after a few minutes.
Can I use UFW on Rocky Linux or AlmaLinux?
It is available from EPEL, but those systems ship firewalld and their tools expect it. Use the firewalld guide there instead.
Do the rules survive a reboot?
Yes. UFW stores rules in /etc/ufw and the ufw service loads them at boot when the firewall is enabled. Check with systemctl is-enabled ufw.
Should I block outgoing traffic too?
For most servers no. A default deny on outgoing traffic breaks updates, DNS and time sync unless you add many exceptions. Keep outgoing open and focus on incoming rules.
How do I allow ping?
UFW already accepts ICMP echo requests through its before.rules file, so ping works by default. Removing that permission means editing /etc/ufw/before.rules, which we do not recommend because it also hides useful diagnostics.
More in Security
- firewalld on Rocky Linux and AlmaLinux
- iptables rules that survive a reboot
- Block brute force logins with fail2ban
- CrowdSec intrusion prevention
- Two-factor SSH logins with TOTP
- Disable or restrict root login
- Alerts for every SSH login
- Advanced OpenSSH settings
- Publish SSH host keys in DNS (SSHFP)
- Audit open ports with nmap
- File integrity checks with AIDE and Tripwire
- CSF firewall (ConfigServer Security)
- First security steps on Rocky and Alma
- Free TLS certificates with Certbot
- Self-signed certificates and a private CA
- Rate and connection limits in Nginx
- Wazuh security monitoring
- Kernel and sysctl hardening