Allow Web Traffic Through ufw Without Locking Out SSH

In progress Linux Networking

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

  • ufw installed (sudo apt install ufw) and SSH access with sudo.
  • Console access from the provider, in case something goes wrong.

Steps

  1. Set the defaults:
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    
  2. 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
    
  3. Allow web traffic:
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    
  4. Enable it:
    sudo ufw enable
    

Verify

  • sudo ufw status verbose shows deny (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 OpenSSH or sudo ufw disable.
  • Duplicate rules: sudo ufw status numbered, then sudo ufw delete <number> (allow http and allow 80/tcp are the same port).

References