Linux Iptables block incoming access to selected or specific ip address

by on June 24, 2005 · 14 comments· LAST UPDATED September 25, 2007

in , ,

Sometime it is necessary to block incoming connection or traffic from specific remote host. iptables is administration tool for IPv4 packet filtering and NAT under Linux kernel. Following tip will help you to block attacker or spammers IP address.

How do I block specific incoming ip address?

Following iptable rule will drop incoming connection from host/IP

iptables -A INPUT -s -j DROP
iptables -A OUTPUT -d -j DROP

A simple shell script to block lots of IP address

If you have lots of IP address use the following shell script:

A) Create a text file:

# vi /root/ip.blocked
Now append IP address:

# Ip address block  file

B) Create a script as follows or add following script line to existing iptables shell script:

IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
iptables -A INPUT -s $i -j DROP
iptables -A OUTPUT -d $i -j DROP

C) Save and close the file.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 14 comments… read them below or add one }

1 Dave Richardson June 1, 2007 at 1:22 pm

Thanks for the script!

I’m using it to block some traffic that was comment spam in my wordpress installation. Akismet was catching the spam itself, but now I’m blocking a handful of IPs at the firewall and don’t have to moderate as much garbage!

Much appreciated!

D.E.R. Management, Inc – IT Project Management consulting


2 unix dude November 3, 2007 at 7:24 am

Cool, I’ve been running my own custom drop list I have honed over the years. Too much hacking. I block international access to domestic business servers whenever possible. I’m tired of those log files filled up with password guessing Asia. It works fantastic if one does not need access to international.


3 unix dude November 3, 2007 at 7:54 am

My blockiplist is text exactly like yours, but my script is different and I can’t recall why I did it this way, I hope it’s right, but it seems to work.
for i in `cat /etc/blockiplist|grep -v “#”`
/sbin/iptables -t filter -I INPUT -s $ADDR -j DROP
/sbin/iptables -t filter -I OUTPUT -s $ADDR -j DROP
/sbin/iptables -t filter -I FORWARD -s $ADDR -j DROP
/sbin/iptables -t filter -I INPUT -d $ADDR -j REJECT
/sbin/iptables -t filter -I OUTPUT -d $ADDR -j REJECT
/sbin/iptables -t filter -I FORWARD -d $ADDR -j REJECT
echo “Block ALL INPUT from ” $ADDR ” net DROPPED.”


4 ajay December 28, 2007 at 2:42 pm

i can not connect a linux system from a squid proxy server for internet uses than what will be do


5 Ip blocking & allowing in Linux December 8, 2008 at 7:42 am


In my company we are using Redhat Linux(15 EL) server for proxy settings, i need the command for blocking some particular ip’s not to use the internet & the same thing i need to release them to use internet, pls help me…………


6 zhys9 July 20, 2009 at 6:46 am

Remove an entry:
You can either delete by number or by recreating the rule. “iptables -D INPUT 3″ will remove the 3rd (counting from 1) rule. Or “iptables -D INPUT -s -j DROP” will remove the corresponding entry independent of location. The rules must match exactly though or you’ll get a “Bad rule” error.


7 Ben Chapman May 20, 2010 at 2:35 pm

Thanks for this – it was very helpful. For people who cut and paste, you may need to remove the quote marks around the BLOCKDB variable value in order to get it to work properly.




8 John Jameson November 7, 2010 at 3:28 am

Excellent. Just the instructions I was looking for to block some malicious users from accessing my mail server.


9 webman December 11, 2010 at 8:58 pm

Thanks for the information. I want to ask if it is possible to block absolutely EVERY single IP on the web except for a particular one on my home network ( ?!

How to do that ?!

Apache can do this (for the http protocol) but it looks like the ftp server allows everyone in.

I want to block all IP’s except that one mentioned above.


10 Sander May 21, 2011 at 3:20 pm

Maybe a silly question, but why have an INPUT *and* and OUTPUT rule? Isn’t the OUTPUT rule overkill? If somebody already can’t reach your server (packets are dropped), your server is not going to send any packets to them…, right?


11 Moritz2112 September 12, 2013 at 4:53 pm

Sander, I think it might be because some scanners will continue to try even if incoming traffic is dropped, should they get any traffic back at all from attempts to brute-force entry into a system. Mayhap? Prithee? Perchance? Bueller?


12 hajdano October 10, 2013 at 2:40 am


This does not block samba access?


13 Emmanux February 18, 2014 at 12:54 am

Improved for blocking ip ranges:

IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
iptables -A INPUT -p tcp --destination-port 22 -m iprange --src-range $i -j DROP
#iptables -A OUTPUT -d $i -j DROP

The /root/ip.blocked looks like:

I got it from

This is our first day without login attempts from China. Cheers!


14 Andrew February 23, 2014 at 5:37 am

“or add following script line to existing iptables shell script:”
how do I do that? I’m on ubuntu, iptables are in /sbin and appear encrypted…..

any help appreciated as this looks to be a simple way to restrict a number of ip’s but I’m stuck! thanks.


Leave a Comment

Tagged as: , , , ,

Previous post:

Next post: