Allow Web Traffic Through ufw Without Locking Out SSH
Goal
A server firewall that denies incoming traffic by default and opens only SSH, HTTP and HTTPS, set up without cutting off the SSH session you are using.
Prerequisites
ufwinstalled (sudo apt install ufw) and SSH access withsudo.- Console access from the provider, in case something goes wrong.
Steps
- Set the defaults:
sudo ufw default deny incoming sudo ufw default allow outgoing - Allow SSH before enabling the firewall (use the port your SSH daemon listens on):
sudo ufw allow OpenSSH # or: sudo ufw limit 22/tcp to slow brute force - Allow web traffic:
sudo ufw allow 80/tcp sudo ufw allow 443/tcp - Enable it:
sudo ufw enable
Verify
-
sudo ufw status verboseshowsdeny (incoming)and the three allow rules. - A second SSH connection opens before you close the first one.
Troubleshooting
- Locked out: use the provider's web console, then
sudo ufw allow OpenSSHorsudo ufw disable. - Duplicate rules:
sudo ufw status numbered, thensudo ufw delete <number>(allow httpandallow 80/tcpare the same port).
References
- Ubuntu Server documentation: Firewall - ufw defaults, rules and application profiles.
- ufw man page -
allow,limit,status numbered. - Linux Administration MOC - hub for Linux notes.