Browse guides

Configure the UFW firewall

Only expose required ports and keep SSH reachable while enabling the firewall.

Beginner8 min readUpdated July 18, 2026
1

Install and set the policy

UFW is a simple interface to the system firewall. Deny incoming connections by default and allow outgoing traffic.

sudo apt update
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
2

Allow required services

Allow SSH before enabling UFW. For a web server, then open HTTP and HTTPS.

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Custom SSH port

If SSH listens on another port, explicitly allow it before enabling the firewall.

3

Enable and verify

sudo ufw enable
sudo ufw status numbered
sudo ss -tulpn

Final check

Keep your session open and test a new SSH connection after enabling the firewall.