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.
Terminal
sudo apt update
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing2
Allow required services
Allow SSH before enabling UFW. For a web server, then open HTTP and HTTPS.
Terminal
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpCustom SSH port
If SSH listens on another port, explicitly allow it before enabling the firewall.
3
Enable and verify
Terminal
sudo ufw enable
sudo ufw status numbered
sudo ss -tulpnFinal check
Keep your session open and test a new SSH connection after enabling the firewall.