How to Block International Access to a VPS

VPS provides such services web service . serve databaseR, and many more are accessible from within and abroad. The service could only be accessed by users from within, for security reasons that the server manager blocked access to the Internet from abroad so that servers could not be accessed and reduced the risk of hacking from abroad.

The following is a guide for your server to be accessible only from Indonesia. Please type the following command:

vi allow-indonesia.sh

Then enter the following script in the file allow-indonesian.sh as follows:

#!/bin/sh

 

#allow specific countys from hitting your server (CentOS)

 

Install an IPset if you dont have it

 

yum install ipset -y

 

♪Country to allow

 

# Indonesia

 

#Create the initial rules inipset based on hast:net

 

for a in Indonesian; doipset -N $a hash:net; done

 

#Download the necessary files from England

 

Wget-P. http://www.ipdeny.com/ipblocks/data/countries/id.zone

 

# Add each IP address from the downloaded list into theipset you've created

 

for a in $#cat /root/id.zone ); do ipset -A i Indonesia $a; done

 

#Back up the iptables

 

/sbin/iptables-save > /root/iptables.$#date '+%Y%m%d')

 

# Add the following loop statement into your iptables to allow the countries you want

 

for a in Indonesian; do iptables -I INPUT -p tcp -m set --match-set $a src -j ACCEPT; done

 

for a in Indonesian; do iptables -I INPUT -p udp -m set --match-set $a src -j ACCEPT; done

 

for a in Indonesian; do iptables -I INPUT -p icmp -m set --match-set $a src -j ACCEPT; done

 

#drop all except indo

 

iptables -A INPUT -i eth1 -j DROP

 

# Save the current configuration for iptables

 

service iptables save

 

#remove zone

 

rm -rf /root/*.zone*

 

Next, type the command to create a script file update-allow-indonesian.sh

vi update-allow-indonesia.sh

Then enter the following script in the file update-allow-indonesian.sh

#!/bin/sh

 

#allow specific countries from hitting your server (CentOS)

 

#Create the initial rules inipset based on hast:net

 

for a in Indonesian; doipset -N $a hash:net; done

 

#Download the necessary files from England

 

Wget-P. http://www.ipdeny.com/ipblocks/data/countries/id.zone

 

# Add each IP address from the downloaded list into theipset you've created

 

for a in $#cat /root/id.zone ); do ipset -A i Indonesia $a; done

 

#remove zone

 

rm -rf /root/*.zone*

 

When you're done, you type in the following command next.

chmod +x allow-indonesia.sh

cd /root/

./allow-indonesia.sh

If successful, it will display like this


Next, enter the following command 

chmod +x update-allow-indonesia.sh

echo "@monthly /root/update-allow-indonesia.sh" >> /var/spool/cron/root

 

The guide can be run using Linux CentOS Operation System. The Guide Opens the VPS International Access Block

 

Note : eth1 is a publicip interface, if you use a server other than Cloudmatika please adjust

How to Unblock International Access to a VPS
How to Log In to a Server via SSH on Linux and macOS