Home > Articles

  • Print
  • + Share This
This chapter is from the book

Section 3: Advanced Server-Hardening Techniques

Depending on your level of threat, you may want to add some additional hardening techniques to each of your servers. The advanced server-hardening techniques we cover include server disk encryption, secure NTP alternatives, and two-factor authentication with SSH.

Server Disk Encryption

Like many more advanced security-hardening techniques, disk encryption is one of those security practices that many administrators skip unless they are required to engage it by regulations, by the sensitivity of the data they store, or by the presence of an overall high-security environment. After all, it requires extra work to set up, it can lower your overall disk performance, and it can require that you manually intervene to enter a passphrase to unlock a disk whenever you boot. As you consider whether you should encrypt your disks, it is important to recognize what security it provides and what security it can’t provide.

  • Encryption protects data at rest. Disk encryption will encrypt data as it is written to disk but provides the data in unencrypted form while the file system is mounted. When the disk is unmounted (or the server is powered off), the data is encrypted and can’t be read unless you know the passphrase.

  • Encryption does not protect the live file system. If an attacker compromises a server while the encrypted disk is mounted (which would usually be the case for most running servers), he will be able to read the data as though it were any other unencrypted file system. In addition, if the attacker has root privileges, he can also retrieve the decryption key from RAM.

  • The encryption is only as strong as your passphrase. If you pick a weak password for your disk encryption, then an attacker will eventually guess it.

Root Disk Encryption

The examples I give next are to encrypt non-root disks. For servers, it’s simpler to segregate your sensitive data to an encrypted disk and leave the OS root partition unencrypted. That way you could set up your system to be able to boot to a command prompt and be accessible over the network in the event of a reboot without prompting you for a passphrase. That said, if your environment is sensitive enough that even the root disk must be encrypted, the simplest way to set it up is via your Linux distribution’s installer, either manually in the partitioning section of your installation disk, or through an automated installation tool like kickstart or preseed.

Non-root Disk Encryption

I’m assuming that if you chose to encrypt your root disk, you probably encrypted all the remaining disks on your server during installation. However, if you haven’t chosen to encrypt everything, you likely have a disk or partition you intend to use for sensitive information. In the examples that follow, we use the Linux Unified Key Setup (LUKS) disk encryption tools and, in particular, we use the cryptsetup script that simplifies the process of creating a new LUKS volume. If cryptsetup isn’t already installed on your server, the package of the same name should be available for your distribution.

In the following example, we will set up an encrypted volume on the /dev/sdb disk but you could also select a partition on the disk instead. All commands will require root permissions. In the end, we will have a disk device at /dev/mapper/crypt1 we can format, mount, and treat like any other disk.

The first step is to use the cryptsetup tool to create the initial encrypted drive with your chosen passphrase and format it with random data before you use it:

$ sudo cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb
This will overwrite data on /dev/sdb irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
Command successful.

At this point, you have a LUKS encrypted disk on /dev/sdb, but before you can use it you need to open the device (which will prompt you for the passphrase) and map it to a device under /dev/mapper/ that you can mount:

$ sudo cryptsetup luksOpen /dev/sdb crypt1
Enter passphrase for /dev/sdb:

The syntax for this command is to pass cryptsetup the luksOpen command followed by the LUKS device you want to access, and finally the label you want to assign to this device. The label will be the name that shows up under /dev/mapper, so in the preceding example, after the command completes, I will have a device under /dev/mapper/crypt1.

Once /dev/mapper/crypt1 exists, I can format it with a file system and mount it like any other drive:

$ sudo mkfs -t ext4 /dev/mapper/crypt1
$ sudo mount /dev/mapper/crypt1 /mnt

You will likely want to set this up so that the device shows up in the same way after every boot. Like with the /etc/fstab file you use to map devices to mount point at boot, there is an /etc/crypttab file you can use to map a particular device to the label you want to assign to it. Like with modern /etc/fstab files, it’s recommended that you reference the UUID assigned to the device. Use the blkid utility to retrieve the UUID:

$ sudo blkid /dev/sdb
/dev/sdb: UUID="0456899f-429f-43c7-a6e3-bb577458f92e" TYPE="crypto_LUKS"

Then update /etc/cryptab by specifying the label you want to assign the volume (crypt1 in our example), the full path to the disk, then “none” for the key file field, and then “luks” as the final option. The result in our case would look like this:

$ cat /etc/crypttab
# <target name> <source device>         <key file>      <options>
crypt1 /dev/disk/by-uuid/0456899f-429f-43c7-a6e3-bb577458f92e none luks

If you do set up /etc/crypttab, you will be prompted at boot for a passphrase. Note in our example we did not set up a key file. This was intentional because a key file on the unencrypted root file system would probably be available to an attacker who had access to the powered-off server, and then she would be able to decrypt the disk.

Secure NTP Alternatives

Accurate time is important on servers, not just as a way to synchronize log output between hosts, but because most clustering software relies on cluster members having an accurate clock. Most hosts use a service called Network Time Protocol (NTP) to query a remote NTP server for accurate time. You need root permissions to set the time on a server, so typically the NTP daemon (ntpd) ends up running in the background on your system as root.

I would imagine most administrators don’t think about NTP when they think about security. It is one of those protocols you take for granted; however, most administrators ultimately rely on an external accurate time source (like nist.gov) for NTP. Because NTP uses the UDP protocol, it might be possible for an attacker to send a malicious, spoofed NTP reply before the legitimate server. This reply could simply send the server an incorrect time, which could cause instability, or given that ntpd runs as root, if it didn’t validate the NTP reply in a secure way, there is potential for a man-in-the-middle attacker to send a malicious reply that could execute code as root.

One alternative to NTP is tlsdate, an open-source project that takes advantage of the fact that the TLS handshake contains time information. With tlsdate, you can start a TLS connection over TCP with a remote server that you trust and pull down its time. While the timestamps in TLS are not as precise as with NTP, they should be accurate enough for normal use. Since tlsdate uses TCP and uses TLS to validate the remote server, it is much more difficult for an attacker to send malicious replies back.

The tlsdate project is hosted at https://github.com/ioerror/tlsdate, and the general-purpose installation instructions can be found at https://github.com/ioerror/tlsdate/blob/master/INSTALL. That said, tlsdate is already packaged for a number of popular Linux distributions, so first use your standard package tool to search for the tlsdate package. If it doesn’t exist, you can always download the source code from the aforementioned site and perform the standard compilation process:

make install

The tlsdate project includes a systemd or init script (depending on your distribution) that you can start with service tlsdated start. Once running, the script will notice network changes and will periodically keep the clock in sync in the background. If you want to test tlsdate manually, you can set the clock with the following command:

$ sudo tlsdate -V
Sat Jul 11 10:45:37 PDT 2015

By default, tlsdate uses google.com as a trusted server. On the command line, you can use the -H option to specify a different host:

$ sudo tlsdate -V -H myserver.com

If you want to change the default, edit /etc/tlsdate/tlsdated.conf and locate the source section:

# Host configuration.
        host google.com
        port 443
        proxy none

Change the host to whichever host you would like to use. For instance, you may want to pick a couple of hosts in your network that poll an external source for time and have the rest of your servers use those internal trusted hosts for time. Those internal trusted servers simply need to serve some kind of TLS service (such as HTTPS).

Two-Factor Authentication with SSH

Disabling password authentication on SSH and relying strictly on keys is a great first step to hardening SSH, but it still is not without its risks. First, while you may follow my advice and protect your SSH keys with a password, you can’t guarantee that every user on the system does the same. This means if an attacker were able to access a computer for a short time, he could copy and use the keys to log in to your system. If you want to protect against this kind of attack, one approach is to require two-factor authentication for SSH.

With two-factor authentication, a user must provide both an SSH key and a separate token to log into the server. Time-based tokens are the most common, and in the past they required you to carry around an expensive device on your keychain that would update itself with a new token every 30 seconds. These days, there are a number of two-factor authentication solutions that work in software and can use your cell phone instead of a hardware token.

There are several different phone-based two-factor authentication libraries out there for ssh. Some work by the SSH client ForceCommand config option, some with a system-wide pluggable authentication modules (PAM) module. Some approaches are time-based, so they work even if your device is disconnected from a network, while others use SMS or phone calls to transfer the code. For this section, I’ve chosen the Google Authenticator library for a couple of reasons:

  • It’s been around for a number of years and is already packaged for a number of Linux distributions.

  • The Google Authenticator client is available for multiple phone platforms.

  • It uses PAM, so you can easily enable it system-wide without having to edit ssh config files for each user.

  • It provides the user with backup codes she can write down in case her phone is ever stolen.

Install Google Authenticator

The Google Authenticator library is packaged for different platforms so, for instance, on Debian-based systems you can install it with

$ sudo apt-get install libpam-google-authenticator

If it isn’t already packaged for your distribution, go to https://github.com/google/google-authenticator/ and follow the directions to download the software, build, and install it.

Configure User Accounts

Before we make PAM or SSH changes that might lock you out, you will want to at least configure Google Authenticator for your administrators. First, install the Google Authenticator application on your smartphone. It should be available via the same methods you use to install other applications.

Once the application is installed, the next step is to create a new Google Authenticator account on your phone from your server. To do this, log in to your account as your user and then run google-authenticator. It will walk you through a series of questions, and it’s safe to answer yes to every question, although since you should have already set up tlsdate on your server so its time is accurate, I recommend sticking with the default 90-second window instead of increasing it to 4 minutes. The output looks something like this:

kyle@debian:~$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y
[URL for TOTP goes here]
[QR code goes here]
Your new secret key is: NONIJIZMPDJJC9VM
Your verification code is 781502
Your emergency scratch codes are:
Do you want me to update your "/home/kyle/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) n

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

If you have libqrencode installed, this application will not only output a URL you can visit to add this account to your phone, it will also display a QR code on the console (I removed it from the preceding output). You can either scan that QR code with your phone, or enter the secret key that follows “Your new secret key is:” in the output.

The emergency scratch codes are one-time use codes that you can use if you ever lose or wipe your phone. Write them down and store them in a secure location apart from your phone.

Configure PAM and SSH

Once you have configured one or more administrators on the server, the next step is to configure PAM and SSH to use Google Authenticator. Open your SSH PAM configuration file (often at /etc/pam.d/sshd) and at the top of the file add

auth required pam_google_authenticator.so

On my systems, I noticed that once I enabled Google Authenticator and ChallengeResponseAuthentication in my SSH configuration file, logins would also prompt me for a password after I entered my two-factor authentication code. I was able to disable this by commenting out

@include common-auth

in /etc/pam.d/sshd, although if you aren’t on a Debian-based system, your PAM configuration may be a bit different.

Once the PAM file was updated, the final step was to update my SSH settings. Open /etc/ssh/sshd_config and locate the ChallengeResponseAuthentication setting. Make sure it’s set to yes, or if it’s not, in your sshd_config file add it:

ChallengeResponseAuthentication yes

Also, since we have disabled password authentication before, and are using key-based authentication, we will need to add an additional setting to the file, otherwise SSH will accept our key and never prompt us for our two-factor authentication code. Add the following line to the config file, as well:

AuthenticationMethods publickey,keyboard-interactive

Now you can restart ssh with one of the following commands:

$ sudo service ssh restart
$ sudo service sshd restart

Once SSH is restarted, the next time you log in you should get an additional prompt to enter the two-factor authentication code from your Google Authenticator app:

$ ssh kyle@web1.example.com
Authenticated with partial success.
Verification code:

From this point on, you will need to provide your two-factor token each time you log in.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020