1. Inventory required services first
sudo ss -tulpn sudo ufw status verbose sudo ip route
List every port required for administration, websites, monitoring and application traffic. Do not copy a generic allow-list without comparing it with the services actually running on your host.
2. Install and define a baseline
sudo apt update sudo apt install ufw sudo ufw default deny incoming sudo ufw default allow outgoing
These defaults deny unsolicited inbound traffic and allow outbound connections. They are a starting point, not a complete policy.
3. Allow your actual management port
If SSH uses the standard port 22:
sudo ufw allow 22/tcp comment 'SSH administration'
If SSH uses a custom port, allow that exact TCP port instead. If you administer from a stable public IP, consider restricting the source:
sudo ufw allow from ADMIN_PUBLIC_IP to any port 22 proto tcp
Replace the placeholder with your actual trusted source address. Do not paste the literal placeholder into UFW.
4. Allow required application ports
For a typical public web server, HTTP and HTTPS may be required:
sudo ufw allow 80/tcp sudo ufw allow 443/tcp
Only open UDP or additional application ports when the service genuinely requires them and you understand the exposure.
5. Enable and verify
sudo ufw status numbered sudo ufw enable sudo ufw status verbose
6. Manage rules deliberately
sudo ufw status numbered sudo ufw delete 3 sudo ufw reload sudo ufw disable
Rule numbers change after deletions. Re-run status numbered before deleting a rule. Disabling UFW removes its filtering policy and should be a deliberate recovery action, not routine troubleshooting.
7. Verify externally
sudo ss -tulpn sudo ufw status verbose
From a separate trusted machine, test only the ports you expect to be reachable. A local listener and a reachable public port are different conditions.
Checklist
- Document required TCP and UDP ports.
- Allow management access before enabling.
- Use source restrictions where operationally practical.
- Review numbered rules before deletion.
- Test from outside the server.
References & further reading
Commands are examples for a representative Ubuntu/Linux environment. Review paths, versions, access requirements and maintenance impact before applying them to production systems.