This guide covers essential UFW commands and best practices for securing your server.
-
Show all active rules (with details):
sudo ufw status verbose
-
Show rules with numbering (for easy deletion):
sudo ufw status numbered
- Delete a specific rule by its number:
Replace
sudo ufw delete <RULE_NUMBER>
<RULE_NUMBER>with the number shown inufw status numbered.
-
Allow SSH (port 22) from anywhere:
sudo ufw allow proto tcp to any port 22
Tip: For better security, consider restricting SSH to your IP if possible.
-
Allow HTTP (port 80):
sudo ufw allow proto tcp to any port 80
-
Allow HTTPS (port 443):
sudo ufw allow proto tcp to any port 443
-
Deny all incoming connections by default:
sudo ufw default deny incoming
-
Allow all outgoing connections by default:
sudo ufw default allow outgoing
- Enable the firewall (after setting your rules):
sudo ufw enable
- By default, allowing SSH from anywhere is convenient, but less secure.
If your IP is static, restrict SSH access to your IP:sudo ufw allow from <YOUR.IP.ADDRESS> to any port 22 proto tcp
- If your IP changes frequently, consider using Fail2ban to protect SSH by blocking repeated failed login attempts.
Remember:
Always double-check your rules before enabling UFW, especially if you are connected via SSH, to avoid locking yourself out.