How to keep the correct time and keep your computers synchronized without abusing time servers, using NTP and systemd.
Learn how you can sync your Linux time online with NTP servers and keep it on time. No, run the following command to enable NTP time synchronization. Sudo timedatectl set-ntp true. That’s all you have to do on these distributions. Sudo update-rc.d ntpd enable sudo service ntpd start. 1 u 57 64 377 71.712 -0.109 0.249 ntp.mcast.net. 18:21:48 UTC 2013 (1)', processor='x8664', system='Linux/3.13.3-1.el6.elrepo.x8664',.
What Time is It?
Linux is funky when it comes to telling the time. You might think that the
time
tells the time, but it doesn’t because it is a timer that measures how long a process runs. To get the time, you run the date
command, and to view more than one date, you use cal
. Timestamps on files are also a source of confusion as they are typically displayed in two different ways, depending on your distro defaults. This example is from Ubuntu 16.04 LTS:Some display the year, some display the time, which makes ordering your files rather a mess. The GNU default is files dated within the last six months display the time instead of the year. I suppose there is a reason for this. If your Linux does this, try
ls -l --time-style=long-iso
to display the timestamps all the same way, sorted alphabetically. See How to Change the Linux Date and Time: Simple Commands to learn all manner of fascinating ways to manage the time on Linux.Check Current Settings
NTP, the network time protocol, is the old-fashioned way of keeping correct time on computers.
ntpd
, the NTP daemon, periodically queries a public time server and adjusts your system time as needed. It’s a simple lightweight protocol that is easy to set up for basic use. Systemd has barged into NTP territory with the systemd-timesyncd.service
, which acts as a client to ntpd
.Before messing with NTP, let’s take a minute to check that current time settings are correct.
There are (at least) two timekeepers on your system: system time, which is managed by the Linux kernel, and the hardware clock on your motherboard, which is also called the real-time clock (RTC). When you enter your system BIOS, you see the hardware clock time and you can change its settings. When you install a new Linux, and in some graphical time managers, you are asked if you want your RTC set to the UTC (Coordinated Universal Time) zone. It should be set to UTC, because all time zone and daylight savings time calculations are based on UTC. Use the
hwclock
command to check:“Hardware clock is kept in UTC time” confirms that your RTC is on UTC, even though it translates the time to your local time. If it were set to local time it would report “Hardware clock is kept in local time.”
You should have a
/etc/adjtime
file. If you don’t, sync your RTC to system time:This should generate the file, and the contents should look like this example:
The new-fangled systemd way is to run
timedatectl
, which does not need root permissions:“RTC in local TZ: no” confirms that it is on UTC time. What if it is on local time? There are, as always, multiple ways to change it. The easy way is with a nice graphical configuration tool, like YaST in openSUSE. You can use
timedatectl
:Or edit
/etc/adjtime
, replacing UTC with LOCAL.systemd-timesyncd Client
Now I’m tired, and we’ve just gotten to the good part. Who knew timekeeping was so complex? We haven’t even scratched the surface; read
man 8 hwclock
to get an idea of how time is kept on computers.Systemd provides the
systemd-timesyncd.service
client, which queries remote time servers and adjusts your system time. Configure your servers in /etc/systemd/timesyncd.conf
. Most Linux distributions provide a default configuration that points to time servers that they maintain, like Fedora:You may enter any other servers you desire, such as your own local NTP server, on the
NTP=
line in a space-delimited list. (Remember to uncomment this line.) Anything you put on the NTP=
line overrides the fallback.![Command Command](/uploads/1/2/6/3/126350282/109117099.png)
What if you are not using systemd? Then you need only NTP.
![Updating Updating](/uploads/1/2/6/3/126350282/968280993.png)
Setting up NTP Server and Client
It is a good practice to set up your own LAN NTP server, so that you are not pummeling public NTP servers from all of your computers. On most Linuxes NTP comes in the
ntp
package, and most of them provide /etc/ntp.conf
to configure the service. Consult NTP Pool Time Servers to find the NTP server pool that is appropriate for your region. Then enter 4-5 servers in your /etc/ntp.conf
file, with each server on its own line:The
driftfile
tells ntpd
where to store the information it needs to quickly synchronize your system clock with the time servers at startup, and your logs should have their own home instead of getting dumped into the syslog. Use your Linux distribution defaults for these files if it provides them.Now start the daemon; on most Linuxes this is
sudo systemctl start ntpd
. Let it run for a few minutes, then check its status:I have no idea what any of that means, other than your daemon is talking to the remote time servers, and that is what you want. To permanently enable it, run
sudo systemctl enable ntpd
. If your Linux doesn’t use systemd then it is your homework to figure out how to run ntpd
.Now you can set up
systemd-timesyncd
on your other LAN hosts to use your local NTP server, or install NTP on them and enter your local server in their /etc/ntp.conf
files.NTP servers take a beating, and demand continually increases. You can help by running your own public NTP server. Come back next week to learn how.
Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX.