This article is outdated and may not work correctly for current operating systems or software.
Welcome to another Vultr tutorial. Today we'll be learning how to use IPTables and IP2Location to block IP addresses from certain countries. IP2Location offers databases of country IP address blocks as a free service.
Before we do anything, we must make sure to have
yum install iptables ipset -y service iptables start
Visit the IP2Location visitor blocker service. In this example, we'll be blocking connections from Iceland, so we'll select Iceland from the list.
After doing so, select "Linux IPTables" as the output format. It should look like:
Upload the file to your server with your favorite FTP client. I use FileZilla.
Once the previous step is complete, access SSH on your server and head to the directory where the file was uploaded.
Rename the file to
Now we will process the file of IP blocks. Create a shell script.
Paste the following into the script:
#!/bin/bash #Script to process ip ranges to ban using IPSet and IPTables ipset create countryblock hash:net while read line; do ipset add countryblock $line; done < (block.txt) iptables -I INPUT -m set --match-set countryblock src -j DROP
Save the file, and exit the text editor.
Run the script.
Finally, save IPTables and reload:
service iptables save service iptables reload
You have now successfully blocked a country from accessing your Vultr server. If you want to remove the block, simply remove the rules from IPTables.