Set the time to keep Linux accurate

Posted by stokie-rich on Fri, 19 Nov 2021 12:26:57 +0100

How to keep the right time and how to use NTP and systemd to keep your computer synchronized without abusing the time server.

How long is it?

It's strange to let Linux tell you the time. You may think that you use the time command to tell you the time. In fact, it is not, because time is just a timer that measures how long a process has been running. To get the time, you need to run the date command. If you want to see more dates, you can run the cal command. The timestamp on the file is also a confusing place, because it generally has two different display methods according to 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 the year, some show the time, which makes your files more chaotic. By default, GNU displays the time instead of the year if your file is within six months. I think there may be a reason for this. If your Linux is like this, try using   ls -l --time-style=long-iso   Command to display the timestamp in the same way and sort it alphabetically.

Check current settings

NTP -- network time protocol, which is an old-fashioned way to keep the computer correct time. Ntpd is an NTP daemon that periodically queries the public time server to adjust your computer time on demand. It is a simple and lightweight protocol, and it is very easy to set up when using its basic functions. SYSTEMd by using   systemd-timesyncd.service   It has "done the work of NTP" and can be used as the client of ntpd.

Before we start "dealing" with NTP, take some time to check whether the current time setting is 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 real-time clock (RTC). When you enter the BIOS of the system, you can see the time of your hardware clock, and you can also change its settings. When you install a new Linux, in some graphical time managers, you will be asked whether to set your RTC to UTC (Coordinated Universal Time) time zone, because all time zones and daylight saving time are based on UTC. You can use hwlock   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 is on UTC time indicates that your computer's RTC uses UTC time, although it converts the time to your local time. If it is set to local time, it displays Hardware clock is on local time. You should have a / etc/adjtime file. If not, use the following command to synchronize your RTC to the system time:

$ sudo hwclock -w

This command will generate the file, and the content looks like the following:

$ cat /etc/adjtime
0.000000 1516661953 0.000000
1516661953
UTC

The system D mode of the new invention is to run   timedatectl   Command that does not require root privileges to run:

$ 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 to use local time instead? There are many ways to do this. The easiest way 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, replace UTC with LOCAL.

SYSTEMd timesyncd client

SYSTEMd provides   systemd-timesyncd.service   Client, which can query the remote time server and adjust your local system time. stay  / etc/systemd/timesyncd.conf   Configure your (time) server in. Most Linux distributions provide a default configuration, which points to the time server they maintain. For example, the following is Fedora's:

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

You can enter other time servers you want to use, such as your own local NTP server, and enter a space separated list of servers on the NTP = line. (don't forget to uncomment this line) anything on the NTP = line will overwrite the configuration item on the FallbackNTP line. What if you don't want to use systemd? Then you will need NTP.

Configure NTP server and client

Configuring your own LAN NTP server is a very good practice, so that your network computers do not need to constantly query the public NTP server. NTP on most Linux comes from the NTP package, and most of them provide  / etc/ntp.conf   File to configure the time server. Check the NTP time server pool to find the appropriate NTP server pool in your area. And then in your  / etc/ntp.conf   Enter 4 - 5 servers, each on a separate line:

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

The drivefile tells ntpd that it needs to save information for quickly synchronizing your system clock with the time server at startup. The logs will also be saved in the directory specified by them, rather than dumped into syslog. If your Linux distribution provides these files by default, use them.

Now start the daemon; In most mainstream Linux, its commands are   sudo systemctl start ntpd. After running it for a few minutes, we 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 has started talking to the time server, and that's what we need. You can run   sudo systemctl enable ntpd   Command to permanently enable it. If your Linux doesn't use systemd, then the homework left for you is to find out how to run ntpd.

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