A new server responds on every port that programs open. Allow only what you need.
Ubuntu and Debian: ufw allow OpenSSH, for websites ufw allow 80,443/tcp, then ufw enable.
AlmaLinux and Rocky Linux: firewall-cmd --permanent --add-service=ssh, add http and https the same way, then firewall-cmd --reload.
Always allow SSH before turning on the firewall. Otherwise you will not be able to connect to your server.
Check the rules before you log out. Ubuntu and Debian: ufw status verbose lists every open port. AlmaLinux and Rocky Linux: firewall-cmd --list-all shows the active zone with its services and ports.
Open a port only for the programs that need it. A database such as MySQL (3306) or PostgreSQL (5432) usually serves only your own apps, so allow it from one address: ufw allow from 203.0.113.10 to any port 5432 proto tcp. To close a port again, run ufw delete allow 80/tcp, or firewall-cmd --permanent --remove-service=http followed by firewall-cmd --reload.
If SSH runs on a custom port, allow that port first, for example ufw allow 2222/tcp, and keep your current session open while you test a new login from a second terminal. For attacks that flood the network itself, add DDoS protection when you create the server: the server firewall decides which ports answer, and DDoS protection filters traffic before it reaches the server.
