$ sudo ufw status
Status: active
To Action From
-- ------ ----
Nginx Full ALLOW Anywhere
22/tcp ALLOW Anywhere
Nginx Full (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
How can I allow SSH (22/tcp) requests only from a certain region, country or continent?
This question is not related to allowing only requests from localhost, for example, because the IP of locahost is already known. I want something like to have all the IP ranges of a certain country in a text file and embed them into ufw allow rule.
To add an IP block list to Uncomplicated Fire Wall (UFW) use the following instructions from this Gihub script the contents of which are reproduced below.
With all acknowledgements to poddmo the author of UFW-blocklist which is an IP blocklist extension for Ubuntu ufw.
INSTALLATION
Install the ipset package
sudo apt install ipset
Backup the original ufw after.init example script
sudo cp /etc/ufw/after.init /etc/ufw/after.init.orig
Install the ufw-blocklist files
Download an initial IP blocklist from IPsum
Start ufw-blocklist
NOTE: It takes time to load the blocklist entries into the ipset. Watch the progress with:
USAGE
The blocklist is automatically started and stopped by ufw using the enable, disable and reload options. Ubuntu UFW wiki Page
There are 2 additional after.init commands available: status and flush-all
sudo /etc/ufw/after.init flush-all
From this state you can manually add IP addresses to the list like this:
sudo ipset add ufw-blocklist-ipsum a.b.c.d
This is useful for testing. Use
/etc/cron.daily/ufw-blocklist-ipsum
to download the latest list and fully restore the blocklist.STATUS
Calling
after.init
with the status option displays the current count of the entries in the blocklist, the hit counts on the firewall rules (column 1 is hits, column 2 is bytes) and the last 10 log messages. A sample output can be seen in the Github extension post linked.Further references:
Geo Blocking
Block Countries
IP Addresses Ranges by Country