Linux Iptables block incoming access to selected or specific ip address

Posted on in Categories Debian Linux, Gentoo Linux, Iptables, Linux, Networking, RedHat/Fedora Linux, Security, Suse Linux, Ubuntu Linux last updated June 24, 2005

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.