HostSlim

How do I set up a firewall with UFW on Ubuntu or Debian?

UFW (Uncomplicated Firewall) is the easiest way to protect a Linux server. sudo apt install ufw sudo ufw default deny incoming sudo ufw default allow…

  • VPS

UFW (Uncomplicated Firewall) is the easiest way to protect a Linux server.

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp      # SSH – do this first!
sudo ufw allow 80/tcp      # HTTP
sudo ufw allow 443/tcp     # HTTPS
sudo ufw enable
sudo ufw status verbose

Important: always allow your SSH port before enabling the firewall, otherwise you lock yourself out. If that happens, use the console in your control panel to fix the rules.

Only open the ports you really need, and restrict sensitive services (databases, admin panels) to your own IP address: sudo ufw allow from YOUR.IP to any port 3306.

More from the knowledge base

Need something that is not on the list?

Custom builds, private VLANs, BGP sessions, multi-rack deployments. Tell us what you are running and an engineer replies, usually within the hour.