# IP TABLES A Beginner’s Tutorial

Listen to this Post

This paper shows how to use iptables to set up a secure firewall on your Linux home computer(s). It contains plenty of configuration examples and command output. If you follow the examples, you will be able to build and deploy a robust and flexible firewall of your own.

You Should Know:

Basic iptables Commands

1. List Current Rules

sudo iptables -L -v -n 

Lists all active rules with verbose output.

2. Block a Specific IP Address

sudo iptables -A INPUT -s 192.168.1.100 -j DROP 

Drops all incoming traffic from `192.168.1.100`.

3. Allow SSH (Port 22)

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT 

4. Allow HTTP/HTTPS Traffic

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT 
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT 

5. Enable NAT (Network Address Translation)

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

6. Save iptables Rules

sudo iptables-save > /etc/iptables.rules 

Restore them on reboot:

sudo iptables-restore < /etc/iptables.rules 

Advanced iptables Configurations

  • Rate Limiting Connections
    sudo iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/min --limit-burst 3 -j ACCEPT 
    

  • Block ICMP (Ping) Requests

    sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP 
    

  • Log Dropped Packets

    sudo iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROP: " --log-level 4 
    sudo iptables -A INPUT -j DROP 
    

Troubleshooting Commands

  • Check Packet Counters

    sudo iptables -L -v 
    

  • Flush All Rules

    sudo iptables -F 
    

  • Delete a Specific Rule

    sudo iptables -D INPUT 2 
    

(Deletes the 2nd rule in the INPUT chain)

What Undercode Say

A well-configured `iptables` firewall is essential for securing Linux systems. Always test rules before applying them in production. Use logging to monitor suspicious activity and regularly update rules to adapt to new threats. Combining `iptables` with tools like `fail2ban` enhances security further.

Expected Output:

A functional firewall setup with logged and secured traffic, preventing unauthorized access while allowing legitimate services.


<h1>Example of a secure baseline iptables configuration</h1>

sudo iptables -P INPUT DROP 
sudo iptables -A INPUT -i lo -j ACCEPT 
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT 
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT 
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT 
sudo iptables -A INPUT -j LOG --log-prefix "DENIED: " 
sudo iptables -A INPUT -j DROP 

For persistent rules, install `iptables-persistent` (Debian/Ubuntu):

sudo apt install iptables-persistent 
sudo netfilter-persistent save 

Further Reading:

References:

Reported By: Ahmed Ali – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image