debian时间同步
介绍 (Introduction)
Accurate timekeeping has become a critical component of modern software deployments. Whether it’s making sure logs are recorded in the right order or database updates are applied correctly, out-of-sync time can cause errors, data corruption, and other difficult issues to debug.
精确的计时已成为现代软件部署的重要组成部分。 无论是确保以正确的顺序记录日志还是正确应用数据库更新,不同步的时间都可能导致错误,数据损坏以及其他难以调试的问题。
Debian 10 has time synchronization built in and activated by default using the standard ntpd time server, provided by the ntp
package. In this article we will look at some basic time-related commands, verify that ntpd is active and connected to peers, and learn how to activate the alternate systemd-timesyncd network time service.
Debian 10内置了时间同步功能,默认情况下使用ntp
软件包提供的标准ntpd时间服务器将其激活。 在本文中,我们将研究一些与时间相关的基本命令,验证ntpd是否处于活动状态并已连接到对等端,并学习如何激活备用的systemd-timesyncd网络时间服务。
先决条件 (Prerequisites)
Before starting this tutorial, you will need a Debian 10 server with a non-root, sudo
-enabled user, as described in this Debian 10 server setup tutorial.
在开始本教程之前,您将需要具有非root用户且启用了sudo
Debian 10服务器 ,如本Debian 10服务器设置教程中所述 。
第1步-浏览基本时间命令 (Step 1 — Navigating Basic Time Commands)
The most basic command for finding out the time on your server is date
. Any user can type this command to print out the date and time:
找出服务器上date
最基本命令是date
。 任何用户都可以键入以下命令以打印出日期和时间:
- date 日期
Output
Wed 31 Jul 2019 06:03:19 PM UTC
Most often your server will default to the UTC time zone, as highlighted in the above output. UTC is Coordinated Universal Time, the time at zero degrees longitude. Consistently using Universal Time reduces confusion when your infrastructure spans multiple time zones.
如以上输出中突出显示的,大多数情况下,您的服务器将默认为UTC时区。 UTC是协调世界时 ,即零度经度的时间。 当您的基础结构跨越多个时区时,始终使用通用时间可以减少混乱。
If you have different requirements and need to change the time zone, you can use the timedatectl
command to do so.
如果您有不同的要求,并且需要更改时区,则可以使用timedatectl
命令进行更改。
First, list the available time zones:
首先,列出可用的时区:
- timedatectl list-timezones timedatectl列表时区
A list of time zones will print to your screen. You can press SPACE
to page down, and b
to page up. Once you find the correct time zone, make note of it then type q
to exit the list.
时区列表将显示在屏幕上。 您可以按SPACE
键向下翻页,按b
键向上翻页。 找到正确的时区后,记下该时区,然后键入q
退出列表。
Now set the time zone with timedatectl set-timezone
, making sure to replace the highlighted portion below with the time zone you found in the list. You’ll need to use sudo
with timedatectl
to make this change:
现在,使用timedatectl set-timezone
,确保将下面突出显示的部分替换为您在列表中找到的时区。 您将需要使用sudo
和timedatectl
进行更改:
sudo timedatectl set-timezone America/New_York
sudo timedatectl设置时区美国/纽约
You can verify your changes by running date
again:
您可以通过运行date
再次验证您的更改:
- date 日期
Output
Wed 31 Jul 2019 02:08:43 PM EDT
The time zone abbreviation should reflect the newly chosen value.
时区缩写应反映新选择的值。
Now that we know how to check the clock and set time zones, let’s make sure our time is being synchronized properly.
现在,我们知道如何检查时钟并设置时区,让我们确保时间已正确同步。
步骤2 —检查ntpd的状态 (Step 2 — Checking the Status of ntpd)
By default, Debian 10 runs the standard ntpd server to keep your system time synchronized with a pool of external time servers. We can check that it’s running with the systemctl
command:
默认情况下,Debian 10运行标准的ntpd服务器,以使系统时间与外部时间服务器池保持同步。 我们可以使用systemctl
命令检查它是否正在运行:
- sudo systemctl status ntp sudo systemctl状态ntp
Output
● ntp.service - Network Time Service
Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-07-31 13:57:08 EDT; 17min ago
Docs: man:ntpd(8)
Main PID: 429 (ntpd)
Tasks: 2 (limit: 1168)
Memory: 2.1M
CGroup: /system.slice/ntp.service
└─429 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 106:112
. . .
The active (running)
status indicates that ntpd started up properly. To get more information about the status of ntpd we can use the ntpq
command:
active (running)
状态表示ntpd已正确启动。 要获取有关ntpd状态的更多信息,我们可以使用ntpq
命令:
- ntpq -p ntpq -p
Output
remote refid st t when poll reach delay offset jitter
==============================================================================
0.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
1.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
2.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
3.debian.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
+208.67.72.50 152.2.133.55 2 u 12 64 377 39.381 1.696 0.674
+198.46.223.227 204.9.54.119 2 u 6 64 377 22.671 3.536 1.818
-zinc.frizzen.ne 108.61.56.35 3 u 43 64 377 12.012 1.268 2.553
-pyramid.latt.ne 204.123.2.72 2 u 11 64 377 69.922 2.858 0.604
+nu.binary.net 128.252.19.1 2 u 10 64 377 35.362 3.148 0.587
#107.155.79.108 129.7.1.66 2 u 65 64 377 42.380 1.638 1.014
+t1.time.bf1.yah 98.139.133.62 2 u 6 64 377 11.233 3.305 1.118
*sombrero.spider 129.6.15.30 2 u 47 64 377 1.304 2.941 0.889
+hydrogen.consta 209.51.161.238 2 u 45 64 377 1.830 2.280 1.026
-4.53.160.75 142.66.101.13 2 u 42 64 377 29.077 2.997 0.789
#horp-bsd01.horp 146.186.222.14 2 u 39 64 377 16.165 4.189 0.717
-ntpool1.603.new 204.9.54.119 2 u 46 64 377 27.914 3.717 0.939
ntpq
is a query tool for ntpd. The -p
flag asks for information about the NTP servers (or peers) ntpd is connected to. Your output will be slightly different, but should list the default Debian pool servers plus a few others. Bear in mind that it can take a few minutes for ntpd to establish connections.
ntpq
是ntpd的查询工具。 -p
标志询问有关ntpd连接的NTP服务器(或对等服务器)的信息。 您的输出将略有不同,但应列出默认的Debian池服务器以及其他一些服务器。 请记住,ntpd可能需要花费几分钟才能建立连接。
步骤3 —切换到systemd-timesyncd (Step 3 — Switching to systemd-timesyncd)
It is possible to use systemd’s built-in timesyncd component to replace ntpd. timesyncd is a lighter-weight alternative to ntpd that is more integrated with systemd. Note, however, that it doesn’t support running as a time server, and it is slightly less sophisticated in the techniques it uses to keep your system time in sync. If you are running complex real-time distributed systems, you may want to stick with ntpd.
可以使用systemd的内置timesyncd组件替换ntpd。 timesyncd是ntpd的一种轻量级替代方案,它与systemd集成程度更高。 但是请注意,它不支持作为时间服务器运行,并且在使系统时间保持同步方面所使用的技术稍微复杂一些。 如果您正在运行复杂的实时分布式系统,则可能要坚持使用ntpd。
To use timesyncd, we must first uninstall ntpd:
要使用timesyncd,我们必须首先卸载ntpd:
- sudo apt purge ntp sudo apt清除ntp
Then, start up the timesyncd service:
然后,启动时间同步服务:
- sudo systemctl start systemd-timesyncd sudo systemctl启动systemd-timesyncd
Finally, check the status of the service to make sure it’s running:
最后,检查服务的状态以确保其正在运行:
- sudo systemctl status systemd-timesyncd sudo systemctl状态为systemd-timesyncd
Output
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/systemd-timesyncd.service.d
└─disable-with-time-daemon.conf
Active: active (running) since Wed 2019-07-31 14:21:37 EDT; 6s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 1681 (systemd-timesyn)
Status: "Synchronized to time server for the first time 96.245.170.99:123 (0.debian.pool.ntp.org)."
Tasks: 2 (limit: 1168)
Memory: 1.3M
CGroup: /system.slice/systemd-timesyncd.service
└─1681 /lib/systemd/systemd-timesyncd
We can use timedatectl
to print out systemd’s current understanding of the time:
我们可以使用timedatectl
打印出systemd对时间的当前理解:
- timedatectl timedatectl
Output
Local time: Wed 2019-07-31 14:22:15 EDT
Universal time: Wed 2019-07-31 18:22:15 UTC
RTC time: n/a
Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
This prints out the local time, universal time (which may be the same as local time, if you didn’t switch from the UTC time zone), and some network time status information. System clock synchronized: yes
means that the time has been successfully synced, and NTP service: active
means that timesyncd is enabled and running.
这会打印出本地时间,通用时间(如果您未从UTC时区切换,则可能与本地时间相同)以及一些网络时间状态信息。 System clock synchronized: yes
表示已成功同步时间; NTP service: active
表示已启用并运行时间同步。
结论 (Conclusion)
In this article we’ve shown how to view the system time, change time zones, work with ntpd, and switch to systemd’s timesyncd service. If you have more sophisticated timekeeping needs than what we’ve covered here, you might refer to the offical NTP documentation, and also take a look at the NTP Pool Project, a global group of volunteers providing much of the world’s NTP infrastructure.
在本文中,我们展示了如何查看系统时间,更改时区,使用ntpd以及切换到systemd的时间同步服务。 如果您的计时需求比我们这里讨论的要复杂,您可以参考官方的NTP文档 ,并查看NTP Pool Project ,这是一个由志愿者组成的全球性小组,提供世界上许多NTP基础结构。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-debian-10
debian时间同步