Friday, July 13, 2007

TIP: block an IP address

Here is a basic way to block a particular IP address using iptables

Say the IP address is, just launch your terminal editor and make sure you have these line

# iptables -I INPUT -s -j DROP

To view them and other rules

# iptables -L -n

Alternatively, you might want to use the route command like so:

# route add -host reject

to block the same IP address from conncting to your host.

These comes very hand when used inside shell scripts by batches, let's say you want to block non-consecutive 512 sets of IP address from the server, you can just create file and throw it to the script that process them, like so

for i in $(< bad-ip.lst) ; do
iptables -I INPUT -i eth1 -s "$i" -j DROP
# OR
route add -host $i reject

File bad-ip.lst filr contains the list of IP addresses separated by lines like so:


Basically, that's it.


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy