How to ban/unban ips in linux

In case you’re not an iptables guru, you might want to create a couple scripts and put em somewhere on your $PATH. I’ve created two scripts called ban_ip and unban_ip.

Create a file called ban_ip

touch ban_ip
chmod +x ban_ip

Edit it and copy the following code inside:

sudo iptables -A INPUT -s $1 -j DROP
echo IP Address $1 has been banned

To ban an IP, you must invoke

ban_ip <someIpAddressHere>



And the IP will be banned.

Do the same now for the unban_ip script

touch unban_ip
chmod +x unban_ip

Open your fav. text editor and copy the following code inside:

iptables -D INPUT -s $1 -j DROP
echo Unbanned ip $1

Save it, and use it.

To unban an IP, you must invoke

unban_ip <someIpAddressHere>



Have sudo access, have iptables installed.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.