We hardly ever think about the time keeping mechanisms of our server. But think about how much depends on it! If it were to shut down even for a moment, chaos would ensue. Processes wouldn’t be able to coordinate, no planned schedules can occur, all the careful multitasking and background processes that make our machines hum along smoothly would crash. It’s so crucial, that we need to have access to the time no matter what else fails. A server needs to have a clock when the Internet is down, when the hard drive is not found, and even when it has no power!
Servers have their own hardware clocks with their own batteries that persist between hard and soft reboots. These clocks can be adjusted via the command line. They can also be synchronized with the system time (not the same), as well as outside time servers. Let’s see how that works.
Difference Between Hardware Time and System Time
In addition to a hardware clock, servers also maintain the concept of “system time”, which is a different entity. The system time is generated by the operating system, and not by any hardware. But why have two time sources on the same machine? The reason is efficiency. For example, I can read the hardware clock using the command “hwclock”. Let’s track how long it takes to execute:
We can see that we can retrieve the hardware time in 0.289 seconds. That’s almost 1/3rd of a second just to read the time! When processes are consulting the time on a regular basis, this is unacceptably slow. The system time on the other hand is much faster to query. Here’s an example using the “date” command:
As you can see, we get the same time in just 0.006 seconds. That’s 48 times faster than reading the hardware clock. You can now see why it’s a good idea to have a system time as well as the hardware time.
Synchronizing the System Time and Hardware Time
The system time and the hwclock can be manually synchronized. Usually, you won’t have to do this. But if you want to copy the system time to the hardware clock (one time), you can run the command:
And if you want the hardware clock to update the system time, run:
But there’s really no need to do this. Because in the next section, we’ll see how to make the system clock update itself from outside reliable sources.
Updating the System Clock Automatically from Global Time Servers
Instead of relying on our estimation of time, we can consult reliable time servers across the globe with a precision far beyond anything we can come up with.
Ideally we want the following to happen:
- System time gets updated from an accurate time server;
- Hardware clock gets updated from the system time.
To do this, we’re going to use a package called “chrony” on CentOS 7. Install it using yum:
yum install chrony
Once done, the command “timedatectl” will tell us whether or not our system time is synchronized:
As of now, NTP (Network Time Protocol) is not synchronized because we haven’t enabled the chrony daemon yet. We do this with:
systemctl enable chronyd systemctl start chronyd
Now that the chrony daemon (chronyd) has started, we can run timedatectl again:
As you can see, the NTP is now synchronized! This means that our system time is guaranteed to be correct. The good news is that chrony automatically updates our hardware clock as well, every 11 minutes! This is done via the service “rtcsync” and is specified in the chrony configuration file:
Opening it up, you will find this line by default:
Which means we don’t have to do anything more. Chrony takes care of all the difficult work for us automatically. It updates our system time and ensures that our hardware clock is up to date as well. There’s another software called “NTP”, but I’ve found it more cumbersome to use than chrony, and you can see why. With chrony, you just need to install, enable, and you’re ready to go!