Tags: Ubuntu »»»» Security »»»» SSH »»»» Linux Hints »»»» Linux
A moment ago I was checking the system logs on my Ubuntu server and found that in auth.log reports that someone was repeatedly trying to SSH login as root. The "root" entry in /etc/passwd is setup so that no password will ever be matched, so perhaps this potential cracker would never get into my server anyway. But the requests are using bandwidth - and what if they were able to figure out a password that would work? Why not just block remote SSH login access to logging in as root in the first place? It's a bad idea to login directly as root - instead the recommended best practice is to login as a regular user then use sudo to perform things requiring super user access.
The general goal is - how do you block certain user ID's from logging in using SSH? Fortunately this is an easy sshd_config edit.
Let's start with the symptom:
Nov 20 07:35:24 nuc1 sshd[1981]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root
Nov 20 07:35:26 nuc1 sshd[1981]: Failed password for root from 116.31.116.43 port 30094 ssh2
Nov 20 07:35:31 nuc1 sshd[1981]: message repeated 2 times: [ Failed password for root from 116.31.116.43 port 30094 ssh2]
Nov 20 07:35:32 nuc1 sshd[1981]: Received disconnect from 116.31.116.43 port 30094:11: [preauth]
Nov 20 07:35:32 nuc1 sshd[1981]: Disconnected from 116.31.116.43 port 30094 [preauth]
Nov 20 07:35:32 nuc1 sshd[1981]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root
Nov 20 07:36:03 nuc1 sshd[1986]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root
Nov 20 07:36:05 nuc1 sshd[1986]: Failed password for root from 116.31.116.43 port 59727 ssh2
Nov 20 07:36:10 nuc1 sshd[1986]: message repeated 2 times: [ Failed password for root from 116.31.116.43 port 59727 ssh2]
Nov 20 07:36:10 nuc1 sshd[1986]: Received disconnect from 116.31.116.43 port 59727:11: [preauth]
Nov 20 07:36:10 nuc1 sshd[1986]: Disconnected from 116.31.116.43 port 59727 [preauth]
Nov 20 07:36:10 nuc1 sshd[1986]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root
My auth.log has lots of these entries - about once every 30 seconds.
In /etc/passwd the password field is just "x" which (I think) is impossible to match by entering a password. The script that's targeting my server probably doesn't care that it's failing a zillion times in a row, however. Instead it has a list of likely passwords or is generating random passwords or something and will keep trying until whenever.
On Ubuntu the SSHD configuration is at: /etc/ssh/sshd_config .. on other systems the path may be different.
You can execute "man sshd_config" to learn about the available options.
The option I chose in this case is "AllowUsers" -- this particular server is used ONLY by myself, and therefore I entered:
AllowUsers my-login-name
A companion directive, DenyUsers, is used to list user names that are NOT allowed. You could use this to list accounts like "root" which aren't to be used by remote SSH login. In this case, since I'm the only person using this server, it works to strictly limit only my ID to login.
These directives take a space-separated list of user names. You can also specify user@host, to further limit the scope of who can login. This would limit a given user to only login from certain IP addresses.
Another directive to consider is PermitRootLogin, if "root" is the only ID you wish to control. The argument must be “yes”, “prohibit-password”, “without-password”, “forced-commands-only”, or “no”.
With the above configuration change, the login requests are still occurring, but they are explicitly denied access:
Nov 25 23:54:10 nuc1 sshd[25991]: User root from 116.31.116.43 not allowed because not listed in AllowUsers
Nov 25 23:54:10 nuc1 sshd[25991]: input_userauth_request: invalid user root [preauth]
Nov 25 23:54:10 nuc1 sshd[25991]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root
Nov 25 23:54:13 nuc1 sshd[25991]: Failed password for invalid user root from 116.31.116.43 port 38881 ssh2
Nov 25 23:54:17 nuc1 sshd[25991]: message repeated 2 times: [ Failed password for invalid user root from 116.31.116.43 port 38881 ssh2]
Nov 25 23:54:18 nuc1 sshd[25991]: Received disconnect from 116.31.116.43 port 38881:11: [preauth]
Nov 25 23:54:18 nuc1 sshd[25991]: Disconnected from 116.31.116.43 port 38881 [preauth]
Nov 25 23:54:18 nuc1 sshd[25991]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.43 user=root