Mastering Linux Security: A Step-by-Step Guide to UFW and iptables

Learn how to secure your Linux server by understanding the core differences between UFW and iptables, complete with hands-on setup instructions for both.

proxmox

Building Your First Line of Defense

Linux

What are UFW and iptables?

To understand how Linux handles network security, it is crucial to know that neither UFW nor iptables are the actual firewalls doing the packet filtering; instead, they are management tools that interface with Netfilter. Netfilter is a framework built directly into the Linux kernel that inspects, routes, drops, or modifies network packets as they enter or leave your system. Because interacting with the Netfilter hooks directly is highly complex, system administrators use user-space utilities like UFW and iptables to translate human-readable firewall rules into the strict logic that the Linux kernel requires.

iptables is the traditional, battle-tested utility used to configure these Netfilter rules. It provides absolute, granular control over your network traffic by organizing rules into specific tables (like filter, nat, and mangle) and chains (such as INPUT, OUTPUT, and FORWARD). When a network packet arrives, iptables inspects it sequentially against these chains from top to bottom, making it a powerful tool for complex routing, network address translation (NAT), and deep packet inspection. However, this immense power comes with a steep learning curve, as a single out-of-order rule or syntax error can easily lock an administrator out of their own server.

UFW, which stands for Uncomplicated Firewall, was created specifically to solve the usability issues of iptables. Rather than replacing iptables, UFW acts as a user-friendly front-end that sits on top of it. When you execute a simple UFW command, the program automatically translates it into the complex, underlying iptables rules required by Netfilter. UFW abstracts away the complex concepts of chains and tables, allowing administrators to secure a server with simple commands like "allow http" or "deny port 22." It is the ideal choice for developers and system administrators who need to establish a secure, baseline firewall quickly without needing to become network security experts.

Prerequisites

  • A Linux Server: A machine running a modern Linux distribution (Ubuntu and Debian are highly recommended for this guide, as UFW is native to them).
  • Root or Sudo Privileges: You must have administrative access to configure networking rules. All commands will require sudo.
  • Terminal Access: Basic familiarity with navigating the Linux command-line interface.
  • An Active SSH Connection: If you are configuring this server remotely, you must know your SSH port (default is 22). Warning: You must be extremely careful to allow SSH traffic before enabling your firewall, or you will permanently sever your own connection to the server.

Part 1: Step-by-Step UFW Setup Guide

UFW is the easiest way to get a baseline firewall running on a server.
Critical Warning: If you are configuring a remote server via SSH, you must allow SSH traffic before enabling UFW, or you will permanently lock yourself out of the server.
1

Install UFW (If not already installed)

UFW comes pre-installed on Ubuntu, but if you are using Debian or need to install it manually:
BASH
sudo apt update
sudo apt install ufw
2

Set Default Policies

The best practice for any firewall is to deny all incoming traffic you didn't request, and allow all outgoing traffic from your server.
BASH
sudo ufw default deny incoming
sudo ufw default allow outgoing
3

Allow Essential Connections (SSH)

Before turning the firewall on, explicitly allow SSH so your connection isn't dropped.
BASH
# You can use the service name
sudo ufw allow ssh

# Or you can specify the port and protocol directly (useful if you use a custom SSH port like 2222)
sudo ufw allow 22/tcp
4

Allow Other Required Services

Add rules for any other services your server provides. For example, if you are running a web server:
BASH
# Allow HTTP (Port 80)
sudo ufw allow http

# Allow HTTPS (Port 443)
sudo ufw allow https
5

Enable UFW

Once your SSH rule is in place, activate the firewall.
BASH
sudo ufw enable
(You will see a prompt warning you that this may disrupt existing SSH connections. Press y and Enter to proceed.)
6

Verify UFW Status

Check to ensure your rules are applied correctly.
BASH
sudo ufw status verbose

Part 2: Step-by-Step iptables Setup Guide

Setting up iptables manually requires careful ordering. The safest approach is to accept established connections, allow your current SSH connection, and then drop everything else.
1

Prepare and Flush Existing Rules

First, ensure your default policies are temporarily set to ACCEPT so you don't lock yourself out when flushing old rules. Then, clear (-F) the existing chains.
BASH
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -F
2

Allow Loopback Traffic

The server needs to be able to talk to itself for internal services (like a local database) to function.
BASH
sudo iptables -A INPUT -i lo -j ACCEPT
3

Allow Established and Related Connections

This rule is crucial. It tells iptables to accept incoming traffic that is part of an ongoing connection your server already initiated (e.g., allowing a remote server to reply when your server downloads a file).
BASH
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
4

Allow SSH and Other Services

Append (-A) rules to allow new incoming connections on specific ports.
BASH
# Allow SSH (Port 22)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Allow HTTP (Port 80)
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# Allow HTTPS (Port 443)
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
5

Set the Default Drop Policies

Now that your safe rules are at the top of the chain, you can safely change the default policies to drop any unapproved traffic.
BASH
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
6

Verify the Rules

List out your active rules to confirm they are set correctly. The -v flag provides verbose output, and -n displays numeric ports instead of names.
BASH
sudo iptables -L -v -n
7

Make iptables Rules Persistent (Save Rules)

By default, iptables rules are wiped from memory if the server reboots. To make them permanent on Ubuntu/Debian, you need the iptables-persistent package.
BASH
# Install the persistent package
sudo apt install iptables-persistent

# Save your current rules so they load on boot
sudo netfilter-persistent save
sudo netfilter-persistent reload
(On RHEL/CentOS systems, you would typically use sudo iptables-save > /etc/sysconfig/iptables instead).

Discover CTCservers Dedicated Server Locations

CTCservers servers are available around the world, providing diverse options for hosting websites. Each region offers unique advantages, making it easier to choose a location that best suits your specific hosting needs.

Limited Time
Special Offers
Server upgrades & more.
UK Region London
15%
OFF
Asia Pacific Tokyo
10%
OFF