Firewall
UFW (Uncomplicated Firewall)
Section titled “UFW (Uncomplicated Firewall)”Basic Setup
Section titled “Basic Setup”# Installsudo apt install ufw
# Default policiessudo ufw default deny incomingsudo ufw default allow outgoing
# Allow SSH (important!)sudo ufw allow ssh# or: sudo ufw allow 22
# Allow HTTP/HTTPSsudo ufw allow httpsudo ufw allow https# or: sudo ufw allow 80,443/tcp
# Enable firewallsudo ufw enableCheck Status
Section titled “Check Status”sudo ufw status verbosesudo ufw status numberedCommon Rules
Section titled “Common Rules”# Allow specific portsudo ufw allow 8000
# Allow from specific IPsudo ufw allow from 192.168.1.100
# Allow port from specific IPsudo ufw allow from 192.168.1.100 to any port 22
# Deny specific portsudo ufw deny 3306
# Delete rulesudo ufw delete allow 8000# or by number: sudo ufw delete 3Application Profiles
Section titled “Application Profiles”# List available profilessudo ufw app list
# Allow Nginxsudo ufw allow 'Nginx Full'
# Allow OpenSSHsudo ufw allow 'OpenSSH'Recommended Rules for SnapCode
Section titled “Recommended Rules for SnapCode”# Reset (careful!)sudo ufw reset
# Basic setupsudo ufw default deny incomingsudo ufw default allow outgoing
# Essentialsudo ufw allow sshsudo ufw allow httpsudo ufw allow https
# Optional: Allow app port (if not using reverse proxy)# sudo ufw allow 8000
# Enablesudo ufw enableiptables (Advanced)
Section titled “iptables (Advanced)”# View rulessudo iptables -L -n -v
# Allow established connectionssudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow SSHsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow HTTP/HTTPSsudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Drop all other incomingsudo iptables -A INPUT -j DROPBest Practices
Section titled “Best Practices”- ✅ Always allow SSH before enabling
- ✅ Use reverse proxy (Nginx) instead of exposing app port
- ✅ Limit SSH to specific IPs if possible
- ✅ Regularly audit rules
- ✅ Log denied connections