Skip to content

Block and Allow IP with iptables - simple script

Nov 25, 2009 1 min read #linux #scripting

This post is more than 18 months old. Technology changes quickly, so some of this content may be out of date (but that's not always the case). Please verify any technical or programming information against the current release.

As most developers are lazy, I’m a huge fan of scripts. I’ve found myself lately having to add entries to iptables to block a single IP or a small subnet, so I made a quick script to make the job easier on myself.

Usage for both of these is of course really simple. Say 123.1.2.3 is the IP in question:

sudo ./blockip.sh 123.1.2.3
sudo ./allowip.sh 123.1.2.3
bash

Block and allow the IP using iptables with these scripts:

blockip.sh
bash
#!/bin/bash

#blocking iptables
/sbin/iptables -A INPUT -s $1 -j DROP

#saving iptables
/sbin/iptables-save > /etc/sysconfig/iptables
allowip.sh
bash
#!/bin/bash

#allowing iptables
/sbin/iptables -D INPUT -s $1 -j DROP

#saving iptables
/sbin/iptables-save > /etc/sysconfig/iptables