iptables is Linux administration tool for IPv4 packet filtering and NAT. One can use iptables/ip6tables to set up, manage, and examine the tables of IPv4 and IPv6 packet filter rules in the Linux kernel. This page shows how to use Iptables to insert the rule at top of tables.
To list iptables rules with line numbers
sudo iptables -t filter -L INPUT --line-numbers -n
sudo iptables -t filter -L OUTPUT --line-numbers -n
sudo iptables -t filter -L FORWARD --line-numbers -n
sudo iptables -t nat -L --line-numbers -n
Iptables insert the rule at top of tables Linux syntax
The iptables allows you to APPEND or INSERT or REPLACE firewall rules as follows.
Iptables append firewall rules to the end of the selected chain
The syntax is:
iptables -A chain firewall-rule
Iptables prepend firewall rules to the end of the selected chain
You need to use the following syntax:
iptables -I chain [rule-number] firewall-rule
Linux Iptables insert/prepend rule at top of tables command summary
You need to use the following syntax:
sudo iptables -I chain [rule-number] firewall-rule