Iptables insert rule at top of tables ( PREPEND rule on Linux )

To insert an iptables rule at the top of the table (prepend a rule in Linux), you can use the -I option followed by the chain name and the position number 1.

Here’s an example of how to prepend a rule that allows incoming SSH traffic:

iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT

This command inserts a rule at the top of the INPUT chain that matches incoming TCP packets on port 22 and accepts them.

Note that the changes made to iptables are not permanent and will be lost after a reboot. To make the changes permanent, you need to save them to the iptables configuration file. The method to save the rules depends on the distribution you are using. For example, on Fedora, you can save the rules using the following command:

iptables-save > /etc/sysconfig/iptables

Leave a Comment