Understanding UFW (Uncomplicated Firewall)
UFW is Ubuntu's default firewall configuration tool that provides an easy-to-use interface for managing netfilter firewall rules. It uses iptables under the hood but simplifies the process of creating and managing firewall rules.
How UFW Works
- Default Policy: By default, UFW denies all incoming connections and allows all outgoing connections
- Rule Evaluation: Rules are evaluated in order from first to last
- State Tracking: UFW uses connection state tracking to allow return traffic automatically
- IPv6 Support: UFW manages both IPv4 and IPv6 rules simultaneously
Essential System Ports
1sudo ufw enable2
3sudo ufw allow 804sudo ufw allow 4435sudo ufw allow 226#ntp7sudo ufw allow 1238#DNS9sudo ufw allow 535310sudo ufw allow 5311#postgresql12sudo ufw allow 543213#mysql14sudo ufw allow 3306Port Explanations
- Port 80 (HTTP): Standard unencrypted web traffic. Used by web servers like Apache, Nginx
- Port 443 (HTTPS): Encrypted web traffic using TLS/SSL. Required for secure websites
- Port 22 (SSH): Secure Shell for remote server administration. Critical for remote management
- Port 123 (NTP): Network Time Protocol (UDP). Keeps system clock synchronized
- Port 53 (DNS): Domain Name System. Standard DNS queries (TCP/UDP)
- Port 5353 (mDNS): Multicast DNS. Used for local network service discovery (Avahi, Bonjour)
- Port 5432 (PostgreSQL): Default port for PostgreSQL database server
- Port 3306 (MySQL/MariaDB): Default port for MySQL and MariaDB database servers
Allow MongoDB (port 27017)
MongoDB is a NoSQL document database. Port 27017 is the default port for MongoDB instances.
Security Note: Only expose MongoDB to trusted networks. Consider using UFW to limit access to specific IP addresses:
1sudo ufw allow 27017Restricting MongoDB Access to Specific IPs
1# Allow MongoDB access only from specific IP2sudo ufw allow from 192.168.1.100 to any port 270173
4# Allow MongoDB access from a subnet5sudo ufw allow from 192.168.1.0/24 to any port 27017Allow Email Ports
Email servers require multiple ports for different protocols:
- Port 25 (SMTP): Simple Mail Transfer Protocol - Used for sending email between mail servers
- Port 143 (IMAP): Internet Message Access Protocol - Unencrypted email retrieval
- Port 993 (IMAPS): IMAP over SSL/TLS - Encrypted email retrieval
- Port 110 (POP3): Post Office Protocol v3 - Unencrypted email download
- Port 995 (POP3S): POP3 over SSL/TLS - Encrypted email download
1sudo ufw allow 252sudo ufw allow 1433sudo ufw allow 9934sudo ufw allow 1105sudo ufw allow 995Additional Email Server Ports
1# SMTP Submission (modern email submission)2sudo ufw allow 5873
4# SMTPS (SMTP over SSL - legacy)5sudo ufw allow 465Advanced Email Server Configuration
For production email servers, consider restricting port 25 to prevent spam:
1# Allow SMTP only from localhost (for local apps sending email)2sudo ufw allow from 127.0.0.1 to any port 253
4# Allow SMTP from specific trusted servers5sudo ufw allow from 203.0.113.10 to any port 25Reload UFW
After adding or modifying firewall rules, reload UFW to apply changes:
1sudo ufw reloadCheck UFW status to confirm rules
1sudo ufw statusDetailed Status View
For more verbose output showing rule numbers and details:
1# Show numbered rules (useful for deleting specific rules)2sudo ufw status numbered3
4# Show verbose status with more details5sudo ufw status verboseExample Output
1Status: active2
3To Action From4-- ------ ----522/tcp ALLOW Anywhere680/tcp ALLOW Anywhere7443/tcp ALLOW Anywhere83306/tcp ALLOW Anywhere95432/tcp ALLOW AnywhereEnable Logging for Monitoring
UFW logging helps track connection attempts and troubleshoot firewall issues:
1sudo ufw logging onLogging Levels
1# Set logging to low (minimal logging)2sudo ufw logging low3
4# Set logging to medium (moderate detail)5sudo ufw logging medium6
7# Set logging to high (maximum detail)8sudo ufw logging high9
10# Set logging to full (logs all packets)11sudo ufw logging fullView UFW Logs
1# View recent UFW log entries2sudo tail -f /var/log/ufw.log3
4# Search for blocked connections5sudo grep -i "block" /var/log/ufw.log6
7# Count blocked connection attempts8sudo grep -c "BLOCK" /var/log/ufw.logUnify Network Server ports
UniFi Controller is Ubiquiti's network management software. It requires multiple ports for different services:
Port Categories:
- Controller Management: Web interface and device communication
- Device Discovery: L2 and L3 network discovery protocols
- Streaming Services: Camera feeds and real-time video
- VoIP Services: UniFi Talk voice communications
- Database: MongoDB for storing configuration and statistics
1# Allow UniFi Controller HTTP and HTTPS ports2sudo ufw allow 8080 # Device communication3sudo ufw allow 8443 # UniFi controller management interface (HTTPS)4
5# Allow UniFi Discovery6sudo ufw allow 10001 # Device discovery on the network7
8# Allow MongoDB (UniFi Database)9sudo ufw allow 27017 # Default MongoDB port for UniFi services10
11# Allow UniFi Video / Protect ports12sudo ufw allow 7443 # Web interface for UniFi Protect13sudo ufw allow 554 # Camera streaming (RTSP)14sudo ufw allow 8000 # Camera streaming15sudo ufw allow 7444 # Camera streaming16
17# Allow SNMP (for monitoring purposes)18sudo ufw allow 161 # SNMP monitoring19
20# Allow UniFi Talk (VoIP - SIP and RTP)21sudo ufw allow 5060 # SIP (Session Initiation Protocol) for voice22sudo ufw allow 5061 # Secure SIP (SIP over TLS)23sudo ufw allow 16384:32767/udp # RTP (Real-Time Protocol) for voice streaming24sudo ufw allow 347825sudo ufw allow 551426sudo ufw allow 678927sudo ufw allow 2711728sudo ufw allow 5656:5699/udp29sudo ufw allow 190030sudo ufw allow 755031sudo ufw allow 7442UniFi Port Details
| Port | Protocol | Service | Description |
|---|---|---|---|
| 8080 | TCP | Device Inform | Device communication with controller |
| 8443 | TCP | HTTPS | Controller web interface (admin) |
| 8880 | TCP | HTTP | Portal HTTP redirect |
| 8843 | TCP | HTTPS | Portal HTTPS redirect |
| 6789 | TCP | Speed Test | UniFi mobile speed test |
| 3478 | UDP | STUN | Session traversal for NAT |
| 10001 | UDP | Device Discovery | AP discovery |
| 1900 | UDP | SSDP | Simple Service Discovery Protocol |
| 5514 | UDP | Syslog | Remote logging |
Security Recommendations for UniFi
1# Restrict controller access to local network only2sudo ufw allow from 192.168.1.0/24 to any port 84433
4# Allow device inform from managed network only5sudo ufw allow from 10.0.0.0/8 to any port 80806
7# Restrict MongoDB to localhost (if DB is on same server)8sudo ufw delete allow 270179sudo ufw allow from 127.0.0.1 to any port 27017Advanced UFW Management
Allowing Specific IP Addresses
1# Allow all traffic from specific IP2sudo ufw allow from 192.168.1.503
4# Allow specific port from specific IP5sudo ufw allow from 192.168.1.50 to any port 226
7# Allow from IP range (CIDR notation)8sudo ufw allow from 192.168.1.0/24Denying Connections
1# Deny specific port2sudo ufw deny 233
4# Deny from specific IP5sudo ufw deny from 203.0.113.516
7# Deny to specific port from IP8sudo ufw deny from 203.0.113.51 to any port 22Deleting Rules
1# Delete by rule specification2sudo ufw delete allow 80803
4# Delete by rule number (use 'ufw status numbered' first)5sudo ufw delete 56
7# Delete rule for specific IP8sudo ufw delete allow from 192.168.1.100Application Profiles
UFW includes predefined application profiles in /etc/ufw/applications.d/:
1# List available application profiles2sudo ufw app list3
4# Show application profile info5sudo ufw app info 'Nginx Full'6
7# Allow application profile8sudo ufw allow 'Nginx Full'9sudo ufw allow 'OpenSSH'10sudo ufw allow 'Apache Full'Port Ranges
1# Allow TCP port range2sudo ufw allow 6000:6007/tcp3
4# Allow UDP port range5sudo ufw allow 6000:6007/udp6
7# Allow port range from specific IP8sudo ufw allow from 192.168.1.100 to any port 6000:6007 proto tcpInterface-Specific Rules
1# Allow on specific network interface2sudo ufw allow in on eth0 to any port 803
4# Allow from specific interface to specific port5sudo ufw allow in on eth1 to any port 33066
7# Deny on specific interface8sudo ufw deny in on eth0 from 203.0.113.0/24Common Web Application Ports
1# Node.js development2sudo ufw allow 30003
4# React development5sudo ufw allow 30016
7# Vue.js/Vite development8sudo ufw allow 51739
10# Next.js development11sudo ufw allow 300012
13# Django development14sudo ufw allow 800015
16# Flask development17sudo ufw allow 500018
19# Ruby on Rails20sudo ufw allow 300021
22# Tomcat23sudo ufw allow 808024sudo ufw allow 800925
26# Jenkins27sudo ufw allow 808028
29# Grafana30sudo ufw allow 300031
32# Prometheus33sudo ufw allow 909034
35# Elasticsearch36sudo ufw allow 920037sudo ufw allow 930038
39# Redis40sudo ufw allow 637941
42# RabbitMQ43sudo ufw allow 567244sudo ufw allow 1567245
46# Kafka47sudo ufw allow 909248
49# MinIO (S3 compatible)50sudo ufw allow 900051sudo ufw allow 9001Container and Orchestration Ports
1# Docker2sudo ufw allow 2375 # Docker daemon API (unencrypted)3sudo ufw allow 2376 # Docker daemon API (TLS)4sudo ufw allow 2377 # Docker Swarm cluster management5
6# Kubernetes7sudo ufw allow 6443 # Kubernetes API server8sudo ufw allow 2379:2380 # etcd server client API9sudo ufw allow 10250 # Kubelet API10sudo ufw allow 10251 # kube-scheduler11sudo ufw allow 10252 # kube-controller-manager12sudo ufw allow 10255 # Read-only Kubelet API13
14# Kubernetes NodePort range15sudo ufw allow 30000:32767/tcp16
17# Portainer18sudo ufw allow 9443 # Portainer HTTPS19sudo ufw allow 9000 # Portainer HTTP20sudo ufw allow 8000 # Portainer Edge AgentTroubleshooting
Check if UFW is Running
1sudo ufw status verbose2sudo systemctl status ufwReset UFW to Default
1# WARNING: This removes all rules2sudo ufw --force reset3sudo ufw default deny incoming4sudo ufw default allow outgoing5sudo ufw enableTest Connection to Port
1# Test if port is open from another machine2nc -zv server_ip 803telnet server_ip 804
5# Check listening ports on server6sudo ss -tlnp7sudo netstat -tlnpCommon Issues
Issue: Can't connect after enabling UFW
1# Check if SSH is allowed before enabling UFW2sudo ufw allow 223sudo ufw enableIssue: Rules not working
1# Reload UFW2sudo ufw reload3
4# Check rule order (first match wins)5sudo ufw status numberedIssue: IPv6 connections not working
1# Verify IPv6 is enabled in UFW config2sudo nano /etc/default/ufw3# Set IPV6=yes4sudo ufw reloadBest Practices
- Principle of Least Privilege: Only open ports that are absolutely necessary
- SSH Security: Change default SSH port and use key-based authentication
- Use IP Restrictions: Limit administrative ports to known IP addresses
- Regular Audits: Periodically review open ports with
sudo ufw status numbered - Enable Logging: Monitor suspicious activity with
sudo ufw logging medium - Rate Limiting: Protect against brute force attacks
- Document Changes: Keep track of why each port is opened
Rate Limiting SSH
1# Limit SSH connections to prevent brute force2sudo ufw limit 22/tcp3
4# This allows maximum 6 connections per 30 seconds from single IPBackup and Restore UFW Rules
1# Backup UFW rules2sudo cp /etc/ufw/user.rules /etc/ufw/user.rules.backup3sudo cp /etc/ufw/user6.rules /etc/ufw/user6.rules.backup4
5# Restore UFW rules6sudo cp /etc/ufw/user.rules.backup /etc/ufw/user.rules7sudo cp /etc/ufw/user6.rules.backup /etc/ufw/user6.rules8sudo ufw reloadQuick Reference
1# Enable/Disable UFW2sudo ufw enable3sudo ufw disable4
5# Default policies6sudo ufw default deny incoming7sudo ufw default allow outgoing8sudo ufw default reject routed9
10# Allow/Deny11sudo ufw allow <port>12sudo ufw deny <port>13sudo ufw reject <port>14
15# Delete rules16sudo ufw delete allow <port>17sudo ufw status numbered18sudo ufw delete <number>19
20# Reset all rules21sudo ufw reset22
23# Status and logging24sudo ufw status25sudo ufw status verbose26sudo ufw status numbered27sudo ufw logging on28sudo ufw logging offUniFi Port Details
| Port | Protocol | Service | Description |
|---|---|---|---|
| 8080 | TCP | Device Inform | Device communication with controller |
| 8443 | TCP | HTTPS | Controller web interface (admin) |
| 8880 | TCP | HTTP | Portal HTTP redirect |
| 8843 | TCP | HTTPS | Portal HTTPS redirect |
| 6789 | TCP | Speed Test | UniFi mobile speed test |
| 3478 | UDP | STUN | Session traversal for NAT |
| 10001 | UDP | Device Discovery | AP discovery |
| 1900 | UDP | SSDP | Simple Service Discovery Protocol |
| 5514 | UDP | Syslog | Remote logging |
Security Recommendations for UniFi
1# Restrict controller access to local network only2sudo ufw allow from 192.168.1.0/24 to any port 84433
4# Allow device inform from managed network only5sudo ufw allow from 10.0.0.0/8 to any port 80806
7# Restrict MongoDB to localhost (if DB is on same server)8sudo ufw delete allow 270179sudo ufw allow from 127.0.0.1 to any port 27017