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

By: (plus.google.com) +David Herron; 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? Installing MacOSX when the installer says: OS X could not be installed on your computer. No packages were eligible for install. »
2016 Election Acer C720 Ad block AkashaCMS Amiga Android Anti-Fascism Apple Apple Hardware History Apple iPhone Apple iPhone Hardware April 1st Arduino ARM Compilation Astronomy Asynchronous Programming Authoritarianism Automated Social Posting Bells Law Big Brother Big Finish Black Holes Blade Runner Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Citizen Journalism Civil Liberties Clinton Cluster Computing Command Line Tools Computer Hardware Computer Repair Computers Cross Compilation Crouton Curiosity Rover Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Developers Editors Digital Photography DIY DIY Repair DNP3 Docker Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Energy Efficiency Enterprise Node ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News Fedora VirtualBox File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Heat Loss Home Automation HTTPS I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick InfluxDB Infrared Thermometers Insulation Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Journalism Joyent Kindle Marketplace Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X MacOS X setup Make Money Online MariaDB Mars Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications MODBUS Mondas MongoDB Mongoose Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea Online advertising Online Fraud Online Journalism Online Video Open Media Vault Open Source Governance Open Source Licenses Open Source Software OpenAPI OpenVPN Personal Flight Peter Capaldi Photography PHP Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Remote Desktop Republicans Retro-Technology Reviews Right to Repair River Song Rocket Ships RSS News Readers rsync Russia Russia Troll Factory SCADA Scheme Science Fiction Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spring Spring Boot SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Ubuntu UDOO Virtual Private Networks VirtualBox VLC VNC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Web Marketing Website Advertising Weeping Angels WhatsApp Window Insulation Wordpress YouTube