Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I want to decrease the likelihood of a successful ssh attack on a linux system. The only port that is open is 22 (I use port forwarding for everything else), and of course I made sure that ssh accepts only key-pair-based logins (no passwords).

The volume of failed ssh login attempts (output of last -f /var/log/btmp) was fairly large (as anybody opening up port 22 should expect), so for the last 2+ years I've been relying on a variation af the iptables-based solutions often mentioned to block ssh attacks, for example Hundreds of failed ssh logins.

One annoying drawback of such a scheme is that it limits the number of new connections from an ip address per amount of time, no matter whether previous connections from that ip were successful or not and whether they were trying to log in as the same user or not. Imagine a script containing half a dozen of rsync commands used from abroad to update various areas on that server: it typically hits the "new connection limit" and fails somewhere in the middle. The same goes if several users behind a router (showing up as the same IP address) do connect on my server more or less at the same time.

So, I am wondering if, without resorting to parsing the /var/log files, it is possible to implement the following strategy with iptables:

  1. accept established connections
  2. allow new connections from previously successful connections
  3. put IPs with failed connections in jail for some time

Bonus points:

  1. Same as above, but allowing/jailing specific user@ip instead of everybody @ip.

And to attempt to curb botnet attacks:

  1. Temporarily put in jail a user (no matter which IP (s)he connects from) after a failed ssh attempt.
share|improve this question

1 Answer 1

up vote 4 down vote accepted

Without parsing logfiles, you can't know if an ssh login attempt was succesful. Fortunately you don't have to parse those logfiles yourself. fail2ban can do this for you. I generally jail an IP for a week after 3 failed login attempts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.