Skip to content

Securing ssh by iptables rules

Securing ssh by iptables rules published on 3 Comments on Securing ssh by iptables rules

I secured my ssh server in simple way – with iptables rules which will be blocking attackers. I setup my iptables in such way, that it is allowing only one tcp syn packet to ssh port per minute from one ip address. With aditional configuration of sshd daemon the rules will allowing for once login attempt per minute.
iptables rules:

Another example with this iptables rules:

We allows up to three connections per hour. After we reaches this three connections per hour, the hashlimit-htable-expire rule starts to counting 10 minutes (600000ms). In this time you can not connect again to ssh.

/etc/ssh/sshd_config – this is important, with this, sshd will be closing ssh connections after authentication failure, thus attacker will have to create new ssh connection (and tcp connection) to try again. This fact (new syn packet) will by noticed by iptables

You can check the blocked addresses:

This rules very limited strength of attacks on my ssh.

Please test this first with another server access!



NEVER set “MaxAuthTries 1” !!!
This (after sshd reload/restart) will reach to LOST OF ACCESS to your server!
I haven’t tried this with key authentication, but it blocks password-based access for sure!

Dear author, have you tried this setting yourself before writing this post?!

I’m sorry that you lost access to your server 🙁

Yes, I checked this before I described it here and I’m using it with MaxAuthTries 1 constantly on my severs.
Maybe it depends on the ssh client.
You can try with –hashlimit-burst 2. Of course, if you have other methods of accessing to server.
I added a warning in the post to test this first with another server access, and I extended the describe of iptables rules.


Thank you for the reply.
I had a console access to the VPS 🙂

By the way, I also tested MaxuthTries setting on the OpenBSD 6.2, and the behavior was the same – lost of access when setting this options to “1”.
Very strange that there’s no any warning in the sshd_config and in the man page.

Best regards, Alexey

Leave a Reply

Your email address will not be published. Required fields are marked *