Basic Firewall: Perimeter Defense

Your system features 65,535 network ports. Implement protocols to secure them. | By JR Nation Infrastructure

Conceptualize your workstation as a massive, secure facility. Within network architecture, this facility features exactly 65,535 distinct entry points, officially designated as Ports. Whenever you interface with the internet, applications utilize specific ports to transmit and receive data packets. A standard web browser exclusively utilizes Port 80 for HTTP and Port 443 for HTTPS. Email clients route through Port 25. Background services and external servers dynamically claim thousands of remaining ports.

A Firewall functions as the perimeter access controller for this facility. Its sole directive is to inspect every incoming and outgoing data packet, cross-reference it against an established administrative rulebook, and determine whether to authorize transmission or silently discard the packet. On a newly deployed Linux environment, the firewall daemon is generally dormant, meaning all ports are theoretically accessible if a background service is actively listening. It is necessary to initialize the firewall and establish an explicit access control list.

💡 Administrative Protocol: Practice Safely. Configuring network firewalls carries significant administrative risk. A single erroneous rule can completely sever your network connection. Access the JR Nation Terminal Sandbox to practice initializing ufw commands in an isolated virtual environment prior to modifying your live infrastructure.

1. Architectural History: Iptables vs. UFW

To properly configure access, you must understand the Linux network stack. The Linux kernel features a highly robust, integrated packet filtering framework named Netfilter. Historically, system administrators interacted with Netfilter utilizing a complex command-line utility known as iptables.

While exceptionally powerful, iptables possesses a notoriously steep learning curve. Authoring a baseline rule to restrict an IP address required extensive knowledge of syntax, protocol chains, and target jumps. A typographical error could instantly sever a remote administrative connection. To resolve this, Canonical engineered a streamlined frontend wrapper designated as UFW (Uncomplicated Firewall).

UFW is not an independent firewall; it is a syntax translator. Administrators input standard, human-readable commands into UFW, and the utility automatically compiles and applies the complex iptables (or modern nftables) background rules. It provides enterprise-grade security configuration with significantly reduced syntax complexity.

2. Deploying Uncomplicated Firewall (UFW)

If you operate Ubuntu, Linux Mint, or Pop!_OS, UFW is likely pre-installed and dormant within your system architecture. If operating Debian, Arch, or Fedora environments, you must deploy the binary via your package manager.

Deploying UFW Infrastructure

sudo apt install ufw


# For Arch Linux environments: sudo pacman -S ufw

# For Fedora environments: sudo dnf install ufw

3. The Standard Baseline: Deny Inbound, Allow Outbound

Prior to enabling the firewall service, administrators must establish the baseline security posture. The absolute standard policy for workstations and enterprise servers is: Deny all unsolicited incoming connections, but authorize all outbound traffic initiated by the local host.

Execute the following syntax to permanently encode this default behavior:

Establishing Baseline Policies

sudo ufw default deny incoming

sudo ufw default allow outgoing

By defaulting to "deny," the workstation effectively achieves network invisibility. If an automated scanning script initiates a sequential port scan across your subnet to identify vulnerabilities, your system will not return an "Access Denied" or "Connection Refused" status. It will simply drop the packets silently, leading the scanner to assume the IP address is unassigned.

4. Authorizing Specific Ports (TCP vs. UDP)

With a strict "deny" baseline established, hosting network services requires establishing explicit access exceptions. For example, hosting a game server or managing a workstation remotely via SSH requires selectively authorizing traffic through the perimeter.

To authorize a port, utilize the allow directive. UFW contains an internal registry of standard application ports, often eliminating the need to memorize specific numerical designations.

Configuring Port Exceptions

sudo ufw allow ssh

# UFW automatically resolves the 'ssh' string to Port 22.


sudo ufw allow 80/tcp

# Explicitly authorizes Port 80 for standard HTTP web traffic.


sudo ufw allow 19132/udp

# Explicitly authorizes Port 19132 for UDP traffic (frequently required by gaming services).

Note the explicit /tcp and /udp protocol tags. TCP (Transmission Control Protocol) guarantees ordered, error-checked packet delivery (vital for web loading and file transfers). UDP (User Datagram Protocol) transmits data continuously without verification (vital for live video streams and gaming, where latency takes precedence over packet loss). Specifying the exact protocol tightens security by preventing unauthorized traffic on the unused protocol layer.

⚠️ Administrative Lockout Risk: Remote Servers
If you are configuring a remote Virtual Private Server (VPS) via an SSH connection, you MUST execute sudo ufw allow ssh PRIOR to initializing the firewall. If you activate a "deny incoming" baseline without explicitly authorizing the SSH port, the firewall will instantly drop your active connection, resulting in a permanent administrative lockout from the remote infrastructure.

5. Advanced Protocols: Rate Limiting & IP Denial

UFW is capable of advanced traffic management beyond binary allow/deny states. If Port 22 is exposed for SSH, automated scripts will continuously attempt brute-force authentication against the server. UFW features native protocols to mitigate these threats.

Rate Limiting Implementation

Rather than utilizing allow, administrators can deploy the limit command. This establishes a conditional threshold: "Authorize traffic on this port, but if an external IP address initiates more than 6 connections within a 30-second window, automatically drop further traffic." This effectively neutralizes brute-force dictionary attacks.

Executing Rate Limits

sudo ufw limit ssh

Explicit Denials & IP Blocking

If server logs identify malicious traffic originating from a specific IP address, you can permanently restrict that address from communicating with your system across all ports.

IP Denial Command

sudo ufw deny from 203.0.113.50


# Permanently drops all packets originating from the specified IP address.

6. Initialization & Auditing (Logging)

Once baseline policies are established and critical ports (e.g., SSH) are explicitly authorized, you are prepared to initialize the firewall daemon.

Activating UFW

sudo ufw enable

Firewall is active and enabled on system startup

To verify successful deployment and audit the currently active ruleset, utilize the verbose status command. Executing this immediately after initialization is an administrative best practice.

Auditing Active Rules

sudo ufw status verbose

Finally, for comprehensive security auditing, administrators can activate UFW logging. The firewall will silently record all dropped connection attempts to /var/log/ufw.log, providing valuable telemetry for future threat analysis.

Enabling Security Logs

sudo ufw logging on

The network perimeter is now secure. In the subsequent module, we will establish user management protocols to govern internal access controls.