Best Practices for Securing a Linux Server

By: Tommy Craddock

At Hivelocity we sell a few hundred Linux dedicated servers or Virtual Private Servers (VPS) every month.  As a hosting provider and data center with thousands of dedicated and virtual servers our techs are on site 24/7 assisting our customers as issues arise.  Many of the dedicated servers we deploy are "self-managed" which leaves the responsibility of maintaining securing a Linux server up to each customer.  Over the last decade plus of providing hosting services we have seen a wide range of security practices employed.  We thought it was a good idea to list a handful of "best practices" that we recommend for every Linux dedicated server.  If you employ each of the following security practices you will greatly reduce the risk of having your data compromised.  The other option is to add managed services to your server and allow us to handle things for you.

There are several Linux server “best practices” that Linux server administrators can follow to protect information from intruders. Unfortunately not all professionals are aware of these fairly simple tasks which include:

· Analyzing the system with root kit checker

· Creating a root login detector and emailer

· Setting an SSH MOTD

· Using secure SSH Logins

· Identifying nuke PHP shells

· Encrypting data

· Enable a firewall

 

Analyzing the System with Root Kit Checker

A common practice among hackers is to install a rootkit on Linux dedicated servers without the administrator being alerted. Some root kit programs are even coded to run at a specific time. Luckily there is a fairly easy method of checking for this issue. First, open up a terminal and type in the following:

Cd ~

wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz

tar xvzf chkrootkit.tar.gz

cd chkrootkit-0.44

make sense

To execute, type:

~/chkrootkit-0.44/chkrootkit

This is an activity that should be run on a daily basis.

 

Creating a Root Login Detector and Emailer

This is a simple yet effective trick that warns the administrator if an intruder manages to use the admin login password on a server. The first step is to edit .bash_profile for the root. In a text editor, add the following line at the end of the file:

echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | awk '{print $6}'`" your@email.com

Replace your@email.com with the administer email address. Do not forget to the save the file prior to exiting.

 

Setting an SSH MOTD

Another excellent Linux server “best practices” method is to set up an SSH MOTD. This practice will provide a message to those entering the system that permissions are only granted to those authorized by the administrator. To add this message, first open a text editor and /etc/motd. Save the following message (or similar content) to the file and exit:

“This system is restricted to authorized access only. All activities on this system are recorded and logged. Unauthorized access will be fully investigated and reported to the appropriate law enforcement agencies.”

 

Using Secure SSH Logins

For servers running more than one IP, it is important to bind an SSHD to one of the IP addresses and do not use it for any other tasks. This practice will add an additional layer of security to the server.

First, edit /etc/ssh/sshd_config. Next, find the section of code that resembles the following:

#Port 22

#Protocol 2, 1

#ListenAddress 0.0.0.0

#ListenAddress ::

Remove the comment from the Port section and input any other number than 22. Typically a number above 40,000 is best. Also, change Protocol 2, 1 to only Protocol 2. Additionally, when using more than one IP, update the listen address accordingly.

The next step is to disable the root login using SSH. Search for #PermiteRootLogin YES and alter it to read PermitRootLogin NO. Finally, type restart SSHD and confirm functionality.

 

Identifying Nuke PHP Shells

Intruders often add nuke PHP shells to a server for easy access at a later time. To remove suspicious looking files, run the following commands:

locate irc

locate BNC

locate ptlink

locate BitchX

locate guardservices

locate psyBNC

locate eggdrop

locate bnc

locate .rhosts

As a word of caution, this method will often highlight legitimate files. Therefore, be cognizant prior to removing any from the server.

 

Encrypting Data

Encrypting all data is one of the easiest and safest protection methods for any server. This will essentially transform the data leaving and entering the server into an unreadable code. The intruder will only be able to view data as a jumbled mess of computer language.

 

Firewall

The final protective method is to add or enable the firewall on the server. This can usually be accomplished in control panels such as Cpanel. The firewall will block incoming requests and will secure the FTP and eliminate malicious files such with malware and viruses.