Ssh – allow only specific users to login via sshd, but refuse connect to non-listed users

hostspamsshd

I'm running a CentOS server (7.0) and I'd like to login via sshd as a user, not root. So I set PermitRootLogin no in the config file and su - after login. I've received lots of hacking activities and I decided to allow only one user to login via sshd. Since the username is not my real name or any common name, I think it would be good enough. Let's say it's 'hkbjhsqj'.

I've tried both ways introduced on nixCraft: AllowUsers in sshd_config or pam_listfile.so in PAM. The only problem to me is that anyone else still has chances to type in passwords and that leaves records in /var/log/secure. I assume these actions consumes my server's resources to run password checking and other stuff.

Let's say I try to login with the username 'admin':

www$ ssh admin@0.0.0.0
admin@0.0.0.0's password:
Permission denied, please try again.
admin@0.0.0.0's password:
Permission denied, please try again.
admin@0.0.0.0's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

and in the secure log:

Aug  8 08:28:40 www sshd[30497]: pam_unix(sshd:auth): check pass; user unknown
Aug  8 08:28:40 www sshd[30497]: pam_listfile(sshd:auth): Refused user admin for service sshd
Aug  8 08:28:43 www sshd[30497]: Failed password for invalid user admin from 192.168.0.1 port 52382 ssh2
Aug  8 08:28:47 www sshd[30497]: pam_unix(sshd:auth): check pass; user unknown
Aug  8 08:28:47 www sshd[30497]: pam_listfile(sshd:auth): Refused user admin for service sshd
Aug  8 08:28:50 www sshd[30497]: Failed password for invalid user admin from 192.168.0.1 port 52382 ssh2
Aug  8 08:28:52 www sshd[30497]: pam_unix(sshd:auth): check pass; user unknown
Aug  8 08:28:52 www sshd[30497]: pam_listfile(sshd:auth): Refused user admin for service sshd
Aug  8 08:28:55 www sshd[30497]: Failed password for invalid user admin from 192.168.0.1 port 52382 ssh2
Aug  8 08:28:55 www sshd[30497]: Connection closed by 192.168.0.1 [preauth]
Aug  8 08:28:55 www sshd[30497]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.0.1

While all this will not happen if I add the IP in /etc/hosts.deny:

www$ ssh admin@0.0.0.0
ssh_exchange_identification: Connection closed by remote host

and in the secure log:

Aug  8 08:35:11 www sshd[30629]: refused connect from 192.168.0.1 (192.168.0.1)
Aug  8 08:35:30 www sshd[30638]: refused connect from 192.168.0.1 (192.168.0.1)

So my question would be, is there a way I can refuse all irrelevant users' ssh requests from anywhere without password checking, like I put them in the hosts.deny list? But at the same time I do need allow all ssh requests with the username 'hkbjhsqj' from anywhere and check the password then.

Best Answer

I don't think it is possible to do what you are asking. If you could, someone could "brute force" to find valid usernames on your server. I am also pretty sure that the username and the password are sent simultaneously by the client, you could verify this by capturing packets using Wireshark on an unencrypted SSH connection.

By "hacking activities" I assume you are talking about brute force attempts at passwords. There are many ways to protect yourself from this, I will explain the most common ways.

Disable root login By denying root login with SSH the attacker has to know or guess a valid username. Most automated brute force attacks only try logging in as root.

Blocking IPs on authentication failure Daemons like fail2ban and sshguard monitor your log files to detect login failures. You can configure these to block the IP address trying to log in after a number of failed login attempts. In your case this is what I would recommend. This reduces log spam and strain on your server, as all packets from this IP would be blocked before they reach the sshd daemon. Your could, for example, set fail2ban to block IPs with 3 login failures in the last 5 minutes for 60 minutes. You will in the worst cases see three failed logins in your log every 60 minutes, assuming the attacker does not give up and move on.

Public key authentication You can disable password authentication entirely and only allow clients with specific keys. This is often considered to be the most secure solution (assuming the client keeps his key safe and encrypted). To disable password authentication, add your public key to ~/.ssh/authorized_keys on the server and set PasswordAuthentication to no in sshd_config. There are numerous tutorials and tools to assist with this.

Related Question