fbpx
Skip to content

What is IPtables Command in Linux and How To Use It

    Last Updated on: 15th April 2023, 05:39 pm

    According to Wikipedia, IPTables is a command-line tool used to configure the Linux kernel’s Netfilter firewall and ip6tables is used for IPv6 packets. It can be used to manage the tables provided by the Linux kernel firewall (Netfilter) and IPv6 packet filtering. The ruleset is stored in /etc/sysctl.conf file. The syntax of iptables command is as follows.

    What is Iptables?

    Iptables is a command line tool for configuring the Linux kernel packet filtering ruleset. It allows you to create firewalls, NAT, and other network address translation devices and provides ways of controlling network traffic.

    It can also be used to configure the tables provided by Netfilter (ip_tables).

    • IPv6: The IPv6 table contains all information related to IPv6 packets, including their header information and so forth.
    • IPv4: The IPv4 table contains all information related to IPv4 packets, including their header information and so forth

    How to work with iptables command in the Linux operating system.

    How to work with iptables command in the Linux operating system.

    The iptables command is a tool that can be used to configure the firewall on your Linux server. The iptables command allows you to create, delete and modify rulesets for your system. It also provides a way to control traffic passing through it, similar to what Netfilter does in systems like BSD-based ones or Mac OS X and iOS devices

    Iptables commands syntax in Linux operating system

    • Iptables command syntax in Linux operating system is as follows:

    iptables -L [--list-rules] -n (to get all rules)

    • Iptables command syntax for filtering packets with a particular destination IP address. This example allows only packets from 192.168.* range to pass through iptables:

    $ iptables -A INPUT -s 192.168.* --dport 22 -j ACCEPT

    Basic rules structure in iptables command

    In this article we will discuss about iptables command in Linux and how to use it. Iptables is used to create and manage rules for IPv4 and IPv6 packets. The basic rules structure of iptables command is as follows:

    • #!/bin/sh
    • firewall_cmd="iptables-restore" (this script should be saved in /etc/init.d folder)

    Basic command of iptables command

    The iptables command is used to configure the tables provided by the Linux kernel firewall. The syntax of this command is:

    • [options] [...]
    • Options that can be used with the iptables command are: -A, -D, -I, -L and -P.

    List all rules of a particular table in iptables

    To list all rules of a particular table, use the following command:

    • iptables -L -n -v
    • iptables -L -n -v -t nat
    • iptables -L -n -v -t mangle
    • iptables --list-mangle or --list-raw or --list-filter

    Display chains of a particular table in iptables

    The iptables command can be used to display the state of all tables in your firewall.

    The following example shows how to display the table called filter:

    • iptables -L -n -v

    This will show you a list of all the chains that are currently active on your system. You can use this command to find out about all rules that are applied in this chain, including what traffic is being filtered or passed through it, who created it, and when it was created as well as other useful information like its priority level (low/medium/high) and also information about whether there are any existing links between two rulesets from different chains which would otherwise cause problems if left unmanaged by mistake!

    Display running rules (counters) of a particular chain in iptables

    This command displays the running rules (counters) of a particular chain in iptables. let’s see an example:

    ```$ iptables -L -n

    Chain INPUT (policy ACCEPT)

    num target prot opt source destination

    0 DROP all -- 0.0.0.0/0 0/0 state RELATED,ESTABLISHED PUSH 1st out-of-order packet; drop if any; discard others ...```

    Clean all rules from a specific chain or table within the iptables firewall.

    Here is how you can clean all rules from a specific chain or table within the iptables firewall.

    # iptables -L -n -t nat

    # iptables-save > /etc/iptables/firewall Ruleset Name: default Chain(s): INPUT ACCEPT [0:0] ACCEPT [0:1] DROP [1:23267] ROUTE_MULTI Marks: 191667

    To list all chains, use this command:

    Iptables is an application that allows users to configure the tables provided by Linux kernel firewall(Netfilter) and ip6tables is used for IPv6 packets

    Iptables is a command line interface program that allows users to configure the tables provided by the Linux kernel firewall(Netfilter). It works in conjunction with ip6tables, which is used for IPv6 packets.

    Conclusion

    We hope this article helped you understand better about iptables. With the help of this article, you will be able to use iptables command in Linux operating system easily.

    Share this post on social!

    Comment on Post

    Your email address will not be published. Required fields are marked *