Home > System Tutorial > LINUX > body text

Keep Linux accurate time

WBOY
Release: 2024-06-27 13:27:48
Original
1096 people have browsed it

让 Linux 保持精确时间

How to keep the correct time, how to use NTP and systemd to keep your computers in sync without abusing time servers.

How long is it?

Linux is weird when it comes to telling you the time. You may think that you are using the time command to tell you the time, but 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. Timestamps on files can also be a source of confusion, as there are two different ways to display them, depending on your distribution's default settings. Here's 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
Copy after login

Some show the year, some show the time, this way makes your file more confusing. The GNU default is to display the time instead of the year if your file is less than six months old. I thought there might be a reason for this. If your Linux is like this, try using the ls -l --time-style=long-iso command to display the timestamps in the same way, sorted alphabetically.

Check current settings

NTP – Network Time Protocol, the old-school way of keeping your computer’s time correct. ntpd is the NTP daemon that adjusts your computer's time on demand by periodically querying a public time server. It is a simple, lightweight protocol that is very easy to set up using its basic functionality. systemd has gone beyond "doing the NTP job" by using systemd-timesyncd.service, which can be used as a client for ntpd.

Before we start “dealing” with NTP, take some time to check that the current time settings are correct. There are (at least) two clocks on your system: the system time - which is managed by the Linux kernel, and the second is the hardware clock on your motherboard, which is also called the real-time clock (RTC). When you enter your system's BIOS, you can see the time on your hardware clock and you can 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 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
Copy after login

Hardware clock is 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 display 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
Copy after login

This command will generate the file, the content will look similar to the following:

$ cat /etc/adjtime
0.000000 1516661953 0.000000
1516661953
UTC
Copy after login

The newly invented systemd way is to run the timedatectl command, which 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
Copy after login

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

$ timedatectl set-local-rtc 0
Copy after login

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

systemd-timesyncd client

systemd 提供了 systemd-timesyncd.service 客户端,它可以查询远程时间服务器并调整你的本地系统时间。在 /etc/systemd/timesyncd.conf 中配置你的(时间)服务器。大多数 Linux 发行版都提供了一个默认配置,它指向他们维护的时间服务器上,比如,以下是 Fedora 的:

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

你可以输入你希望使用的其它时间服务器,比如你自己的本地 NTP 服务器,在 NTP= 行上输入一个以空格分隔的服务器列表。(别忘了取消这一行的注释)NTP= 行上的任何内容都将覆盖掉 FallbackNTP 行上的配置项。如果你不想使用 systemd 呢?那么,你将需要 NTP 就行。

配置 NTP 服务器和客户端

配置你自己的局域网 NTP 服务器是一个非常好的实践,这样你的网内计算机就不需要不停查询公共 NTP 服务器。在大多数 Linux 上的 NTP 都来自 ntp 包,它们大多都提供 /etc/ntp.conf 文件去配置时间服务器。查阅 NTP 时间服务器池 去找到你所在的区域的合适的 NTP 服务器池。然后在你的 /etc/ntp.conf 中输入 4 - 5 个服务器,每个服务器用单独的一行:

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
Copy after login

driftfile 告诉 ntpd 它需要保存用于启动时使用时间服务器快速同步你的系统时钟的信息。而日志也将保存在他们自己指定的目录中,而不是转储到 syslog 中。如果你的 Linux 发行版默认提供了这些文件,请使用它们。

现在去启动守护程序;在大多数主流的 Linux 中它的命令是 sudo systemctl start ntpd。让它运行几分钟之后,我们再次去检查它的状态:

$ 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
Copy after login

我不知道这些内容是什么意思,但重要的是,你的守护程序已经与时间服务器开始对话了,而这正是我们所需要的。你可以去运行 sudo systemctl enable ntpd 命令,永久启用它。如果你的 Linux 没有使用 systemd,那么,给你留下的家庭作业就是找出如何去运行 ntpd。

现在,你可以在你的局域网中的其它计算机上设置 systemd-timesyncd,这样它们就可以使用你的本地 NTP 服务器了,或者,在它们上面安装 NTP,然后在它们的 /etc/ntp.conf 上输入你的本地 NTP 服务器。

The above is the detailed content of Keep Linux accurate time. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template