Skip to content

Instantly share code, notes, and snippets.

@magicstone1412
Forked from jasonruyle/UFW_ban_country.md
Created June 2, 2021 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magicstone1412/b42770fbdf44d9301931d63b8711a40e to your computer and use it in GitHub Desktop.
Save magicstone1412/b42770fbdf44d9301931d63b8711a40e to your computer and use it in GitHub Desktop.
UFW to block countries

#Country ban with UFW#

Grab your different country ip addresses and save as Linux IPTables

http://www.ip2location.com/free/visitor-blocker

##Add country## Run the following command

while read line; do sudo ufw deny from $line; done < all.txt

Where the filename is the country.

##Remove country## To remove or revert these rules, keep that list of IPs! Then run a command like so to remove the rules:

while read line; do sudo ufw delete deny from $line; done < all.txt

##Suggestion## What I did was exported each individual country as their own country.txt file. But then realized that I wanted to run this thing one time, so I ran the following command:

cat *.txt >> all.txt

Then you can run your rule against all of the files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment