UFW (Uncomplicated Firewall) is the simplest way to manage firewall rules on Ubuntu and Debian.
Install and Enable
apt install ufw -y
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw enableCommon Rules
# Allow HTTP and HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
# Allow specific port
ufw allow 3306/tcp
# Allow from specific IP
ufw allow from 203.0.113.50
# Allow port from specific IP
ufw allow from 203.0.113.50 to any port 22
# Delete a rule
ufw delete allow 3306/tcpCheck Status
ufw status verbose
ufw status numberedImportant
Always allow SSH (port 22) before enabling UFW, or you'll lock yourself out. Use the VNC console to recover if this happens.