Skip to content
Free website migration on every plan, done by our team.Learn more ›
Servers

UFW firewall guide for Ubuntu servers

Set up UFW on an Ubuntu VPS safely: allow SSH first, open only the ports you need, rate-limit SSH, check rules and avoid locking yourself out.

Short answer: UFW (Uncomplicated Firewall) makes Linux firewall rules simple. Allow SSH first, then only the ports your services need (usually 80 and 443), enable UFW, and check the rules. Keep your provider's console access handy in case you lock yourself out.

The safe order

sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

Always allow SSH before enabling UFW, or you can disconnect yourself.

Useful extras

  • Rate-limit SSH: sudo ufw limit OpenSSH slows down password-guessing.
  • Allow from one IP only: sudo ufw allow from 203.0.113.5 to any port 22.
  • Delete a rule: sudo ufw status numbered, then sudo ufw delete NUMBER.

Ports you might need

ServicePort
Web80, 443
SSH22 (or your custom port)
Mail (if you run a mail server)25, 465, 587, 993
Control panelsCheck your panel's documentation

Combine with other protections

Use SSH keys, fail2ban and automatic updates too. See first 10 things to do on a VPS and brute-force attacks.

Frequently asked questions

Does UFW work with Docker?

Docker can bypass UFW rules for published ports; follow Docker's guidance on firewall configuration. See Docker on a VPS.

What if I lock myself out?

Use your provider's web console to log in and fix the rule.

Ready when you are

Host it where it performs

NVMe speed, offshore privacy and a written SLA — live in minutes on AuroxHost.