Location Based SSH Access Using GeoIP

To control SSH access based on location, I will use the GeoIP free database to determine the source IP address. Each time an external (non-local network) connection is initiated, the script will be executed by the SSH daemon to identify the region. If the region is not in the allowed list the connection will be dropped.

Install the geoiplookup utility

Verify it is working by pinging the Google public DNS server

Create the following script in /usr/local/bin/ssh_geoip.sh as root

Make the script executable

Add the following to /etc/hosts.deny as root (create the file if needed)

Add the following to /etc/hosts.allow as root (create the file if needed)

Create an update script in /usr/local/bin/geoip_update.sh as root

Make the script executable. If this is the first time installing the GeoIP database, run the script manually and check that the database is fairly recent.

Add a cron entry for root to run the script monthly (at 2:05am on the first day of each month in this example).