Block remote root login via ssh on Ubuntu/etc to keep your server safe

By: ; Date: 2016-11-25 23:56

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
« Why did Christopher Eccleston leave after just one season? Make your own Raspberry Pi git repository server with Gogs and Docker »
2016 Election Acer C720 Ad block Android Apple Hardware History Apple iPhone Hardware April 1st ARM Compilation Authoritarianism Big Brother Blade Runner Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks ChromeOS CIA CitiCards Civil Liberties Clinton Cluster Computing Computer Hardware Computer Repair Cross Compilation Crouton Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant Detect Adblocker Digital Photography DIY Repair Docker Doctor Who Drobo Drupal Drupal Themes DVD Emdebian ESP8266 Eurovision Facebook Fake News FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Government Spying Great Britain Home Automation HTTPS InfluxDB Internet Internet of Things Internet Privacy iPad iPhone iPhone hacking Iron Man Iternet of Things JDBC John Simms Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Mac OS MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Missy Mobile Applications Mondas Monty Python MQTT Music Player Music Streaming MySQL NanoPi Node Web Development Node.JS Online advertising Online Fraud Open Media Vault Open Source Software OpenVPN Personal Flight Peter Capaldi Photography Plex Media Server Political Protest Power Control Privacy Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Republicans Retro-Technology Right to Repair River Song Rocket Ships Russia Russia Troll Factory Science Fiction Season 1 Season 10 Season 11 Security Security Cameras Silence Simsimi Skype Social Media Warfare Software Development Space Flight Space Ship Reuse Space Ships SpaceX SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Ubuntu Virtual Private Networks VOIP Web Developer Resources Web Development Tools Weeping Angels WhatsApp Wordpress