Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Pretty basic question.. How do you PREPEND an iptables rather than APPEND?

I have DROP statements at the bottom of my rules. I have software to add new rules but adding rules after DROP statements isn't good. Every time I want to add a new rule I have to flush the table which is inefficient. Is there a way to prepend a rule i.e. add a rule to the top of the table rather than the bottom?

Many thanks.

share|improve this question

2 Answers

up vote 4 down vote accepted

Use the -I switch:

sudo iptables -I INPUT 1 -i lo -j ACCEPT would insert a rule at position #1 in the INPUT chain.

share|improve this answer
1  
Dang! You beat me by 18 seconds... I wasn't quick enough :( – Yanick Girouard Mar 29 '12 at 19:28
Hehehehehehehe :) – cjc Mar 29 '12 at 19:30
@YanickGirourad, hah, you got the checkmark! – cjc Mar 29 '12 at 21:56

-I will insert. You're probably using -A to append.

You can also do iptables -I chain rulenum to insert a rule as number "rulenum" in chain "chain". -R chain rulenum can be used to replace a specific rule at number "rulenum" in chain "chain". iptables -L -n --line-numbers will show the rule numbers in the left-most column.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.