As a first line of defense against brute-force attacks, I've restricted SSH login attempts with TCP wrappers (denying access by default, and maintaining an IP whitelist in /etc/hosts.allow).

However, I sometimes need to access my server from an IP address that's not in the whitelist (and my system ends up blocking my own attempts).

Is it possible to permit SSH keyed logins for all IP addresses, but prohibit password-only logins from IP's not in a given whitelist? I know I can configure SSH to only allowed keyed logins, but I'd like to keep the ability to log in with a password on a range of IP addresses.

PasswordAuthentication no

Match Address
    PasswordAuthentication yes

Match Address 2001:470:1f0b:915::/64
    PasswordAuthentication yes

I also set different banner files using Banner to make it clear when password logins are accepted.

  • 1
    This is perfect, thank you. To future viewers, just to be clear, this belongs in sshd_config. – David Cain May 12 '13 at 17:48
  • And what if I do not have an sshd_config? I'm using fail2ban but want to set it up for whitelisting. Guess I ought to make a new question – Jonathan Aug 22 '15 at 20:39
  • @Jonathan: I'm quite sure you have a sshd_config if you use OpenSSH. And yeah, if you're asking about fail2ban, it has absolutely nothing to do with this question. – user1686 Aug 22 '15 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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