How Do I Block an IP Address on My Linux server?

How do I block an IP address or subnet under Linux operating system?

In order to block an IP on your Linux server you need to use iptables tools (administration tool for IPv4 packet filtering and NAT) and netfilter firewall. First you need to log into shell as root user. To block IP address you need to type iptables command as follows:

Syntax to block an IP address under Linux

iptables -A INPUT -s IP-ADDRESS -j DROP

Replace IP-ADDRESS with actual IP address. For example if you wish to block ip address for whatever reason then type command as follows:
# iptables -A INPUT -s -j DROP

If you have IP tables firewall script, add above rule to your script.
If you just want to block access to one port from an ip to port 25 then type command:
# iptables -A INPUT -s -p tcp --destination-port 25 -j DROP

The above rule will drop all packets coming from IP to port mail server port 25.
You can write a shell script to block lots of IP address and subnets.

Leave a Reply

Your email address will not be published. Required fields are marked *