Configure a firewall with ufw
Updated 27 Nov 2026 ubuntu firewall ufw
Allow only what you need on Ubuntu and Debian without locking yourself out.
Allow SSH first
sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable
Open application ports
sudo ufw allow 20002/tcp
sudo ufw allow from 198.51.100.7 to any port 5432 proto tcp
On shared-IPv4 plans, open the server port shown beside each forwarded port.
Review and remove
sudo ufw status numbered
sudo ufw delete 3
Slow down password guessing
sudo ufw limit 22/tcp
Docker is different
Ports published by Docker bypass ufw rules. Publish private services on localhost only, for example -p 127.0.0.1:6379:6379.
Two layers
Network rules applied on the host, such as blocked mail ports, cannot be changed from inside the server.