Networking

How to ensure your Linux servers are on the right time

Many pieces of server software require the hosting servers have exact time. To get that on a Linux server, you need to install ntp. Here's how.

linuxhero.jpg
Image: Jack Wallen

I've experienced, many a time, when a particular piece of server software wouldn't function properly for what seemed like inexplicable reasons. After digging through the log files, at least in some instances, the culprit turned out to be out of sync server time. This type of issue can rear its ugly head even if the server clock is off by a few short minutes. Although you can change the time and date from the command line (using the date command), the problem is that time, once set, can still shift.

The solution to this problem is to add the ntp service to your server and have it sync with ntp servers to keep yours right on time. I want to show you how to install ntp on a Linux server and how to enable time synchronization. I will be demonstrating this on a Ubuntu 16.04 server, but the process is very similar on all distributions. The process is quite simple and the rewards can be significant.

Installing ntp

As you might expect, the installation of ntp is quite simple on Ubuntu. To get this piece of software installed, issue the command sudo apt-get install ntp. Once installed, ntp will automatically start. You can query ntp with the command sudo ntpd -q. This should report the ntp information, indicating the server pools queried and other pertinent information. However, I've found on recent Ubuntu installations, an error where ntp reports:

unable to bind to wildcard address :: - another process may be running - EXITING

That error (Figure A) indicates the ntp daemon is failing to start.

Figure A

Figure A

An out of the box error with ntp.

Resolving the error

If you're having this error appear in the output of ntpd -q, you must make sure ntp is stopped with the command:

sudo systemctl stop ntp

With ntp certainly stopped, you can then successfully start ntp with the command:

sudo ntpd -qgd

The above options are:

  • d - debug
  • g - allows the time to be set to any value without restriction
  • q - no quit

At this point, you can then issue the command sudo ntpd -q and the report will show success (Figure B).

Figure B

Figure B

The ntp service is now running correctly.

The problem now is, when you reboot the server, ntp will still fail to start (because it is not attempting to start with the necessary options). How do you solve this? Simple. Open the file /etc/default/ntp and then make the following change:

NTPD_OPTS='-g'

Changes to:

NTPD_OPTS='-gqd'

Save and close that file and ntp will now restart successfully, either at boot or by issuing the command sudo systemctl start ntp.

Your server will now be synchronized with the ntp pool and all of your server software should work (should it happen to fall prey to a time issue).

A simple solution to a complex problem

There are instances where knowing the problems that plague your server software can be a massive challenge. Because of this, it is very important that you should have all of your Linux servers synchronized with ntp time pool servers. Once all of your server clocks are in sync, you can then continue your troubleshooting efforts without concern to time.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox