Block an entire Country with iptables

Here is a pretty fancy “trick” for the ones who wants to block certain parts of the world from accessing your VPS.

first of all you need to know the netblocks for the country you want to block, this information can be found at this page, each country has their own file in CIDR format.

Then we can add those netblocks to IPTABLES and  “problem solved”.
Save below script as root user to “country.block.iptables.sh” and change the ISO variable to match the country name using ISO country codes.

When you are done, make sure it updates atleast weekly so it inludes recently added IP, this will be done by adding the following as a cron job.

first execute:

add:

close and save.

To start blocking immediately type:

12 comments for “Block an entire Country with iptables

Leave a Reply to Nenad Marjanovic Cancel 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.