Skip to main content

Basic Iptables Tutorial

In this tutorial, we'll cover the basics of Iptables, a powerful utility for managing network traffic and configuring firewalls on Linux-based systems.

What is Iptables?

Iptables is a command-line firewall utility that uses policy chains to allow or block traffic. It’s commonly used for filtering network traffic and can be customized to block, allow, or redirect packets based on various criteria such as IP address, port, or protocol.

Basic Concepts

Before diving into rules, let’s understand a few basic concepts:

  • Chains: Iptables operates using chains, which are a list of rules. The most common chains are:

    • INPUT: For incoming connections to your server.
    • OUTPUT: For outgoing connections from your server.
    • FORWARD: For connections passing through the server.
  • Rules: These define the action to take on packets (e.g., allow, block, log, etc.).

  • Targets: The action taken when a packet matches a rule, such as ACCEPT, DROP, or REJECT.

Basic Commands

1. Check the Current Rules

You can check the current Iptables rules with the following command:

sudo iptables -L

This will list all the active rules for each chain.

2. Allow Incoming Traffic on a Specific Port

To allow incoming traffic on a specific port (e.g., port 80 for HTTP), you can use the following command:

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

This command:

  • -A INPUT: Appends the rule to the INPUT chain.
  • -p tcp: Specifies the protocol (TCP in this case).
  • --dport 80: Specifies the destination port (port 80 for HTTP).
  • -j ACCEPT: The target is to ACCEPT the traffic.

3. Block Incoming Traffic from an IP Address

If you want to block all incoming traffic from a specific IP address, use this command:

sudo iptables -A INPUT -s 192.168.1.100 -j DROP

This will block traffic from the IP address 192.168.1.100.

4. Save the Rules

To make sure your rules persist after a reboot, you need to save them. On most Linux systems, you can use the following command:

sudo iptables-save > /etc/iptables/rules.v4

This will save the current rules to the file rules.v4.

Conclusion

Iptables is a powerful tool for securing your Linux server and controlling network traffic. The basic commands covered in this tutorial will help you start managing your firewall effectively. For more advanced usage, you can explore features such as logging, NAT (Network Address Translation), and more complex rule configurations.

For additional resources, you can refer to the official Iptables documentation.