USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Firewall Setup: Iptables/Nftables Basics and Advanced Rule Creation

Lesson 7/31 | Study Time: 25 Min

Firewall configuration is a fundamental aspect of securing Linux systems by filtering network traffic and controlling access. Linux traditionally used iptables as the primary firewall tool, which organizes rules to filter packets based on various criteria. More recently, nftables has emerged as a modern, flexible replacement that simplifies rule management and boosts performance. =

Basics of iptables

iptables organizes firewall logic into tables and chains that process packets at defined stages. The list below introduces these components along with practical command examples and best practices.


Structure of iptables


1. Tables: iptables organizes rules into tables, each for a specific type of processing:


filter (default): handles packet filtering (accept/drop)

nat: for network address translation (port forwarding, masquerading)

mangle: modifies packet headers for QoS and routing

raw: handles packets exempt from connection tracking


2. Chains: Within tables, chains represent sequences of rules applied to network packets at certain hooks:


INPUT (incoming packets)

OUTPUT (outgoing packets)

FORWARD (packets routed through the system)


3. Rules: Chains contain rules specifying criteria (protocol, ports, IP addresses) and actions (ACCEPT, DROP, REJECT, LOG).


Basic iptables Command Examples


List rules: iptables -L -v -n

Accept SSH on port 22: iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Drop traffic from a specific IP: iptables -A INPUT -s 192.168.1.100 -j DROP


Important Considerations: When configuring firewall rules, the order of rules is critical because the first matching rule is applied and subsequent rules are ignored. Additionally, separate rule sets must be defined for IPv4 and IPv6 traffic, as they are managed independently using iptables and ip6tables respectively.

Introduction to nftables

nftables provides a unified firewall framework that supports IPv4, IPv6, ARP, and bridged traffic within a single system, eliminating the need to manage separate tools and rule sets. Its clearer and more consistent syntax simplifies firewall configuration and maintenance, making rules easier to read and manage.

Additionally, nftables improves performance by optimizing rule evaluation and reducing duplication, resulting in more efficient and scalable firewall management.


nftables Structure


Tables: containers for rules, e.g., inet table supports both IPv4 & IPv6.

Chains: similar to iptables, but defined per table with specified hook and policy.

Rules: composed of expressions to match packets and verdict (accept, drop).


Basic nftables Commands


1. Create a table: nft add table inet filter


2. Add chains (e.g., input chain):

text
nft add chain inet filter input { type filter hook input priority 0\; policy drop\; }


3. Allow SSH traffic on port 22:

text
nft add rule inet filter input tcp dport 22 accept


4. Drop traffic from an IP:

text
nft add rule inet filter input ip saddr 192.168.1.100 drop


Advantages Over iptables

The ability to define a single rule that applies to both IPv4 and IPv6 traffic, reducing duplication and configuration effort. Its structured and more readable ruleset makes firewall policies easier to understand, manage, and maintain over time.

Additionally, nftables supports advanced features such as set-based matching, enabling efficient bulk operations and improved performance when handling large numbers of rules or addresses.

Advanced Rule Creation

Advanced rule creation enables fine-grained traffic control and optimized firewall behavior. The techniques below demonstrate how state tracking, rate limiting, and rule management are implemented.


Common Advanced Filtering Techniques


1. Stateful Filtering: Allow established and related packets to pass, blocking others:


iptables: iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

nftables: nft add rule inet filter input ct state established,related accept


2. Rate Limiting: Prevent brute force or flooding attacks:


nftables example: limit SSH connections

text
nft add rule inet filter input tcp dport 22 limit rate 3/minute accept


3. Port Forwarding and NAT


Setting up NAT with iptables requires configuring the nat table.

nftables supports NAT hooks for modern address translation.


Rule Management Commands


1. List all ruleset:


iptables-save (iptables)

nft list ruleset (nftables)


2. Delete specific rules:


iptables -D with rule specification or number

nft delete rule with handle number


3. Flush all rules for reset:


iptables -F

nft flush chain

Andrew Foster

Andrew Foster

Product Designer
Profile

Class Sessions

1- Linux Security Model Overview 2- Kernel-Level Security Features (Namespaces, Capabilities, SELinux, AppArmor) 3- Linux File System Permissions and Extended Attributes (Xattr) 4- Secure User and Group Management Fundamentals 5- Best Practices for Sudo Configuration and Privilege Escalation Control 6- Disabling Unneeded Services and Configuring Secure Boot 7- Firewall Setup: Iptables/Nftables Basics and Advanced Rule Creation 8- Securing SSH: Key Management, Configuration, and Tunneling 9- Mandatory Access Control (SELinux/AppArmor Detailed Configuration) 10- Deployment of PAM for Enhanced Authentication 11- Linux Network Namespaces and Container Isolation Basics 12- TLS/SSL Configuration for Linux Services 13- VPN Setup for Secure Remote Access (OpenVPN, WireGuard) 14- Cryptographic Tools: GPG Encryption, Hashing Utilities, and Key Management 15- Intrusion Detection Systems and Log Monitoring Tools Overview 16- Linux Audit Framework (Auditd) Configuration and Log Analysis 17- Using Syslog, Journald, and Centralized Logging Solutions 18- File Integrity Monitoring with AIDE And Tripwire 19- Compliance Frameworks Introduction (PCI DSS, GDPR, HIPAA) 20- Incident Response Preparation and Forensic Readiness Basics 21- Bash Scripting Best Practices for Security and Automation 22- Conditional Logic, Loops, and Functions for Modular Scripts 23- Handling Errors, Signals, and Debugging Scripts Effectively 24- Automating User and Permission Audits with Scripts 25- Integrating Shell Scripts with System Tools (Cron Jobs, Systemd Timers) 26- Automating Log Analysis and Alerting Via Scripting 27- Writing Scripts for Automated Patch and Vulnerability Management 28- Automating Firewall and SSH Key Rotation Policies 29- Integrating Shell Scripts with Security Scanning Tools (Lynis, OpenVAS) 30- Case Studies on Automated Incident Detection and Response 31- Using Open-Source Tools for Orchestration with Scripting

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.