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 OpenSSHslows 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, thensudo ufw delete NUMBER.
Ports you might need
| Service | Port |
|---|---|
| Web | 80, 443 |
| SSH | 22 (or your custom port) |
| Mail (if you run a mail server) | 25, 465, 587, 993 |
| Control panels | Check 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.



