Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I ran the following iptables commands to create a blacklist rule but used the wrong port:

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j SSH_WHITELIST
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --rttl --name SSH -j ULOG --ulog-prefix SSH_brute_force
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --rttl --name SSH -j DROP

How can I undo the above and then redo it for a different port?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

use iptables -D ... to delete the entries.

iptables -D INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -D INPUT -p tcp --dport 22 -m state --state NEW -j SSH_WHITELIST
iptables -D INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --rttl --name SSH -j ULOG --ulog-prefix SSH_brute_force
iptables -D INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --rttl --name SSH -j DROP
share|improve this answer

Run the same commands with -D instead of -A.

Alternatively, if you want to completely reset the iptables, flush everything with iptables -F.

share|improve this answer

As long as you didnt run the iptables save command, all you should have to do is restart or reload the iptables service. Or you can do what Bonsi Scott said and delete the rules with a caveat. First run

#iptables -L -n -v --line-numbers

to get the rule number, then all you would have to do is delete the rule by number with

#iptables -D INPUT <rule number>

Hope this helps

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.