Details of Linux's method of using NTP to keep accurate time

  • 2020-10-23 20:23:11
  • OfStack

preface

How to keep the right time and how to use NTP and systemd to keep your computer synchronized without abusing the time server. Without further ado, let's take a look at the details.

What is its time?

Linux is strange when it comes to telling you the time. You might think you're using the time command to tell you the time, but it's not, because time is just a timer that measures how long a process is running. To get the time, you need to run the date command, and if you want to see more dates, you can run the cal command. The timestamp on the file is also an area of confusion because it usually has two different ways of displaying it, depending on the default of your distribution. Here is an example from Ubuntu 16.04 LTS:


$ ls -l
drwxrwxr-x 5 carla carla 4096 Mar 27 2017 stuff
drwxrwxr-x 2 carla carla 4096 Dec 8 11:32 things
-rw-rw-r-- 1 carla carla 626052 Nov 21 12:07 fatpdf.pdf
-rw-rw-r-- 1 carla carla 2781 Apr 18 2017 oddlots.txt

Some show years and some show times, which can make your files more cluttered. By default, GNU displays the time, not the year, if your file is less than six months old. I think there's probably a reason for that. If your Linux is like this, try it ls -l --time-style=long-iso Command to have the timestamp displayed in the same way, sorted alphabetically. See how to change the date and time of Linux: Simple commands to learn the various ways to manage time on Linux.

Check current Settings

NTP - Network time protocol, which is the old-fashioned way of keeping your computer on time. ntpd is the NTP daemon that periodically queries the public time server to adjust your computer time as needed. It is a simple, lightweight protocol that is very easy to set up when using its basic functionality. systemd by use systemd-timesyncd.service It has stepped in to do NTP's job and can be used as the client for ntpd.

Before we start "working" with NTP, take a moment to check that the current time Settings are correct.

There are (at least) two clocks on your system: system time, which is managed by the Linux kernel, and the second is the hardware clock on your motherboard, also known as the real-time clock (RTC). When you access BIOS, you can see the time of your hardware clock and you can change its Settings. When you install a new Linux, in one of the graphical time managers, you will be asked if you want to set your RTC to UTC (world Standard time Coordinated Universal Time) time zone, as all time zones and daylight saving time are based on UTC.

You can use the hwclock command to check:


$ sudo hwclock --debug
hwclock from util-linux 2.27.1
Using the /dev interface to the clock.
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2018/01/22 22:14:31
Hw clock time : 2018/01/22 22:14:31 = 1516659271 seconds since 1969
Time since last adjustment is 1516659271 seconds
Calculated Hardware Clock drift is 0.000000 seconds
Mon 22 Jan 2018 02:14:30 PM PST .202760 seconds

Hardware clock on UTC time indicates that your computer's RTC is using UTC time, although it converts that time to your local time. If it is set to local time, it will show Hardware clock is on local time.

You should have 1 /etc/adjtime file. If not, use the following command to synchronize your RTC for system time,


$ sudo hwclock -w

This command will generate the file, which looks something like this:


$ cat /etc/adjtime
0.000000 1516661953 0.000000
1516661953
UTC

The newly invented systemd method is to run the timedatectl command, which does not require root permission:


$ timedatectl
 Local time: Mon 2018-01-22 14:17:51 PST
 Universal time: Mon 2018-01-22 22:17:51 UTC
 RTC time: Mon 2018-01-22 22:17:51
 Time zone: America/Los_Angeles (PST, -0800)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

RTC in local TZ: no indicates that it USES UTC time. So how do you change to local time? There are many ways to do this. The easiest way to do this is to use a graphical configuration tool, such as YaST in openSUSE. You can also use timedatectl:


$ timedatectl set-local-rtc 0

Or edit /etc/adjtime to replace UTC with LOCAL.

systemd - timesyncd client

Now, I'm tired, but we just got to the very exciting part. Who would have thought timing could be so complicated? We haven't even scratched the surface yet; Read man 8 hwclock for details on how your computer holds time.

systemd provides systemd-timesyncd.service Client, which can query the remote time server and adjust your local system time. in /etc/systemd/timesyncd.conf "To configure your server. Most Linux distributions provide a default configuration that points to the time servers they maintain, such as the following for Fedora:


[Time]
#NTP=
#FallbackNTP=0.fedora.pool.ntp.org 1.fedora.pool.ntp.org

You can enter other time servers that you want to use, such as your own local NTP server, and enter a whitespace separated list of servers on the NTP= line. (Don't forget to uncomment this line.) NTP= Anything on line FallbackNTP will overwrite the configuration item on line FallbackNTP.

What if you don't want to use systemd? Then you will need NTP.

Configure the NTP server and client

It is a good practice to configure your own LAN NTP server so that your in-network computer does not have to constantly query the public NTP server. NTP on most Linux comes from the ntp package, and most of them provide the /etc/ ntp.conf file to configure the time server. Refer to the NTP Time server pool to find the appropriate NTP server pool in your area. Then enter 4-5 servers in your /etc/ ntp.conf and use a separate line for each server:


driftfile /var/ntp.drift
logfile /var/log/ntp.log
server 0.europe.pool.ntp.org
server 1.europe.pool.ntp.org
server 2.europe.pool.ntp.org
server 3.europe.pool.ntp.org

driftfile tells ntpd that it needs to save information for use on startup time servers to quickly synchronize your system clock. Instead of being dumped into syslog, the logs will be kept in the directory they specify. If your Linux distribution provides these files by default, use them.

Now go start the daemon; In most mainstream Linux its command is sudo systemctl start ntpd . After leaving it running for a few minutes, let's check its status again:


$ ntpq -p
 remote refid st t when poll reach delay offset jitter
==============================================================
+dev.smatwebdesi 192.168.194.89 3 u 25 64 37 92.456 -6.395 18.530
*chl.la 127.67.113.92 2 u 23 64 37 75.175 8.820 8.230
+four0.fairy.mat 35.73.197.144 2 u 22 64 37 116.272 -10.033 40.151
-195.21.152.161 195.66.241.2 2 u 27 64 37 107.559 1.822 27.346

I don't know what this means, but the important thing is that your daemon is already talking to the time server, which is exactly what we need. You can run it sudo systemctl enable ntpd Command to enable it permanently. If your Linux is not using systemd, then the homework left for you is to find out how to run ntpd.

Now, you can set up systemd-ES172en on other computers in your LAN so that they can use your local NTP server, or, install NTP on them, and then install it on their /etc/ntp.conf Enter your local NTP server.

The NTP server is under attack and demand is growing. You can help by running your own public NTP server. Next week we'll learn how to run your own public server.

conclusion


Related articles: