If your auth log is full of failed SSH logins from countries you never do business in, cut most of that noise at the firewall. Learning how to block SSH login attempts by country with GeoIP means building a list of IP ranges per country, then dropping port 22 traffic from the countries you block before it ever reaches sshd. It takes about 20 minutes on Ubuntu 24.04, and it works alongside key-only login and fail2ban.
Why country-based SSH blocking works (and where it fails)
Brute-force traffic hitting a small VPS tends to come from a handful of countries. Blocking those countries at the firewall will not make your server unhackable, but it shrinks the attack surface: fewer login attempts reach sshd, your logs get quieter, and fail2ban has less work to do.
Be honest about the limits. GeoIP data is approximate, IP ranges get reassigned, and VPNs let attackers rent an exit node in your allowed country. Country blocking is a filter, not a lock. The lock is still SSH keys, so set up key-only login first if you have not: read our guide on hardening SSH on Ubuntu 24.04 with key-only login and fail2ban, then come back here.
Also think about who you might lock out: if you travel, or a teammate works from abroad, they will simply time out. Keep a fallback before you turn any of this on, such as console access through your VPS provider.
What you need before you start
You need root or sudo on an Ubuntu 22.04 or 24.04 VPS. Decide the approach first:
- Blocklist: allow the world except a few countries. Simpler, but a botnet in an allowed country walks straight in.
- Allowlist: allow only your home country and drop the rest. Stronger, but it breaks the moment someone travels.
For a personal VPS or a small team in one country, the allowlist is the better choice. For a server with users scattered across the map, a blocklist aimed at the worst offenders is the practical option. Back up your current firewall rules first, and keep a second SSH session open while you test.
If you have never written firewall rules on this server, start with our guide on setting up UFW firewall rules that do not lock you out of your VPS first.
How to block SSH login attempts by country with GeoIP
The classic xt_geoip iptables module is effectively dead: its database format is outdated and most distributions no longer ship it. The approach that works in 2026 is simpler: download per-country IP lists, load them into an ipset, and reference that set from iptables or nftables.
Step 1: Install ipset and the tools
sudo apt update && sudo apt install -y ipset iptables-persistent curl
ipset holds the country lists in the kernel, so lookups stay fast even with tens of thousands of ranges.
Step 2: Download country IP lists
Public zone files list every IPv4 range assigned to a country, one per line, in CIDR notation. ipdeny.com publishes free files like cn.zone for China and ru.zone for Russia. Decide which countries to block and fetch their zone files:
sudo mkdir -p /etc/ipset cd /etc/ipset sudo curl -sO https://www.ipdeny.com/ipblocks/data/countries/cn.zone sudo curl -sO https://www.ipdeny.com/ipblocks/data/countries/ru.zone
For an allowlist, download the zone files for the countries you want to allow instead, and drop everything else.
Step 3: Load the ranges into an ipset
sudo ipset create blockcountries hash:net
for f in /etc/ipset/*.zone; do
while read -r net; do
sudo ipset add blockcountries "$net"
done < "$f"
done
A single country can hold thousands of ranges, so two or three countries can easily pass 10,000 entries. This ipset country block lives in kernel memory, so matching an incoming IP is one fast hash lookup.
Step 4: Add the firewall rule
With iptables, drop SSH traffic from the set before your normal accept rules:
sudo iptables -I INPUT -p tcp --dport 22 -m set --match-set blockcountries src -j DROP sudo netfilter-persistent save
On nftables, the equivalent is a named set fed from the same zone files, with a rule that drops port 22 traffic from addresses in the set. Test by temporarily adding a test IP to the set and confirming the connection times out, then removing it. Never test by blocking your own real IP while your only session is the one you are testing from.
Step 5: Make it survive a reboot
ipset contents vanish on reboot unless you save them:
sudo ipset save > /etc/ipset/ipset.rules
Restore the set early in the boot sequence, before the firewall rules that reference it load. If the set does not exist when iptables loads, the SSH rule fails. Test the reboot path once, while you still have provider console access.
Some tutorials describe an sshd geoip deny setup via PAM. It works, but the connection reaches sshd first, which defeats the point of filtering at the firewall.
Blocklist vs allowlist: which one to pick
| Approach | Best for | Main risk | Maintenance |
|---|---|---|---|
| Blocklist (drop a few countries) | Servers with users in many countries | Attackers move to an allowed country | Low: update lists monthly |
| Allowlist (allow a few countries) | Personal VPS, small local team | Legit users get locked out while travelling | Medium: add countries when the team changes |
| GeoIP plus fail2ban | Any server already running fail2ban | Two systems to keep in sync | Low: fail2ban handles the rest |
| Cloudflare Tunnel / VPN only | Servers with no public SSH at all | Depends on a third-party service | Low: no port 22 exposed |
The strongest setup is an allowlist plus fail2ban. A fail2ban geoip pairing covers both angles: GeoIP drops whole countries, fail2ban bans individual IPs that still get through. Our fail2ban setup guide for SSH and nginx on Ubuntu walks through the jail configuration, and the two tools do not conflict as long as fail2ban runs after your GeoIP rules are in place.
Keep the country lists updated
IP allocations change every week, so a zone file from three months ago will miss new ranges and still block ranges that moved elsewhere. Refresh the lists with a weekly cron job:
0 3 * * 0 /usr/local/bin/update-geoip-blocklist.sh
The script should build a new temporary set from the fresh zone files, swap it with the live set, and destroy the old one. Swapping instead of flushing means the set is never empty while the firewall rule runs. Log the entry count each run, so you can spot a failed download before it silently empties your blocklist.
GeoIP ssh blocking is only useful if you actually check what is hitting your server. Our guide on auditing who logged into your VPS and spotting suspicious logins shows which log lines matter, so you can tell whether the country filter is doing its job or just moving the noise somewhere else.
Common mistakes that break SSH access
- Blocking your own country by accident. Double-check the two-letter country codes; a typo in a zone filename is all it takes.
- Applying the rule before the set exists. iptables fails to load a rule that references a missing set. Always create the set first, then the rule.
- Forgetting IPv6. The zone files above cover IPv4 only. If sshd listens on IPv6, attackers can walk around the filter on the v6 address. Download the IPv6 zone files too.
- No fallback access. If the rules lock you out, the provider console is the only way back. Confirm that access works before you enable the drop rule.
- Treating GeoIP as the only defense. It is a filter: password login, an old OpenSSH version, or a leaked key still gets an attacker in, no matter which country they connect from.
What to do when a legit user travels
Sooner or later someone on your allowlist flies somewhere you blocked. The simplest fix is a temporary allow rule for their hotel or office IP, removed when they leave. A cleaner option for frequent travellers is a small WireGuard VPN on the same VPS: they connect to the VPN first, then SSH through the tunnel from an internal address your GeoIP rule never sees. Whatever you choose, write the exception down: a temporary rule nobody removes becomes a permanent hole.
FAQ
Does GeoIP blocking stop all SSH brute-force attacks?
No. It stops attempts from the countries you block. Bots using VPNs or compromised machines inside allowed countries still reach sshd. Pair it with key-only login and fail2ban.
Is it legal to block SSH by country?
For your own server, yes. You decide who may connect to it.
How much does GeoIP blocking slow down the server?
Practically nothing. ipset stores ranges in a kernel hash table, so matching an incoming IP is a single fast lookup.
Set up the ipset once, automate the weekly refresh, and check the auth log a week later. If failed logins from blocked countries drop to near zero, the filter is doing its job.