How To Allow Only One Country For SSH / SFTP
If so; how to achieve anything like this ?
4 Replies
@Nuvini:
It's definitely better than nothing - though the best would be to just allow your own IP. You can do this with iptables. Or you can use CSF Firewall which also has some extra features.
Yes but won't work for dynamic IPs.
Going back to your original inquiry, there isn't a reliable way to do this unfortunately. You could potentially get it down to blocks operated by your country's RIR. In theory these should all be delegated out, and I don't personally see them changing much. But you should have a script that runs periodically to make sure you have the right exceptions in your firewall. You could even go a level lower and only add firewall exceptions for your ISP, but they may get a few more blocks of IP addresses moving forward so you'll need a script to check this. And if you are not connecting via your ISP, you would need to use Lish to add a one-time exception.
I'd recommend the first option…it's served me well so far.
-Tim
Because you can block by country doesn't make it a good idea. Disable all password authentication and use keypair authentication and you are already as secure as it gets. You can add fail2ban, connection rate limiting in IPtables, or move SSH to a non-standard port but these techniques don't gain you any real security improvements. They will reduce the size of your logfiles though.