How Do I Block An IP Address On Linux Server ?

by lifeLinux on January 31, 2012

I run CentOS on my server, and I often find that my server is being attacked by other computers. Brute force SSH attacks, port scanning, viruses scanning for the ability to spread, things like that. In this article, I’ll show you how to block an IP address on Linux server using IPTables.

The First, I’ll assume you are already using iptables. If you need help setting that up, read this article.

How do I block an IP address ?

Example I want to block incoming request from IP, login as root and type the following command

# iptables -I INPUT -s -j DROP

– I: Inserts the chain at the top of the rules.
– s: Match source IP address.
– j: Jump to the specified target chain when the packet matches the current rule.

To drop packets coming in on interface eth0 from, type the following command

# iptables -I INPUT -i eth0 -s -j DROP

How do I block a subnet ?

Use the following syntax to block

# iptables -I INPUT -s -j DROP

How do I save blocked IP address ?

To save blocked IP address to iptables config file, type the following command

# service iptables save


# /etc/init.d/iptables save

How Do I Unblock An IP Address?

First, you need to display blocked IP address along with line number and other information, type the following command

# iptables -L INPUT -n --line-numbers
# iptables -L INPUT -n --line-numbers | grep

Sample outputs:

Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    DROP       all  --    
2    LOCALINPUT  all  --  
3    ACCEPT     all  --  
4    ACCEPT     udp  --           udp spts:1024:65535 dpt:53

To unblock you must delete line number 1, enter:

# iptables -D INPUT 1

Related Posts:

Previous post:

Next post: