现有一台ubuntu22.04服务器,需要配置时间服务。
首先通过timedatectl查看时间及时间同步服务器:
root@host:~# timedatectl
Local time: Tue 2026-08-11 09:21:16 UTC
Universal time: Tue 2026-08-11 09:21:16 UTC
RTC time: Tue 2026-08-11 09:36:19
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
可以看出当前时间是2026年8月11日9点21分,时区是Etc/UTC为零时区,NTP服务已开启,但是System clock synchronized: no,没有同步成功时间。
查看时间同步配置文件/etc/systemd/timesyncd.conf:
root@host:~# cat /etc/systemd/timesyncd.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating “drop-ins” in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.
[Time]
#NTP=
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
可以看出未配置NTP服务器,假设NTP服务器为10.10.10.3,将#NTP=改为NTP=10.10.10.3,保存配置文件。
重启时间同步服务systemctl restart systemd-timesyncd。
再次使用timedatectl命令,查看:
root@host:~# timedatectl
Local time: Tue 2026-08-11 09:50:00 UTC
Universal time: Tue 2026-08-11 09:50:00 UTC
RTC time: Tue 2026-08-11 09:50:00
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
发现System clock synchronized: yes,即时间同步成功,但是时区不对。
使用命令timedatectl set-timezone Asia/Shanghai将时区设置为上海,再次使用命令timedatectl,即会显示北京时间:
root@host:~# timedatectl
Local time: Tue 2026-08-11 17:50:11 CST
Universal time: Tue 2026-08-11 09:50:11 UTC
RTC time: Tue 2026-08-11 09:50:11
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
已经显示上海时区,2026-08-11 17:50:11为北京时间。
使用date命令查看时间:
root@host:~# date
Tue Aug 11 05:51:52 PM CST 2026
使用命令timedatectl show-timesync –all查看当前时间同步服务器:
root@host:~# timedatectl show-timesync –all
LinkNTPServers=
SystemNTPServers=10.10.10.3
FallbackNTPServers=ntp.ubuntu.com
ServerName=10.10.10.3
ServerAddress=10.10.10.3
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=2min 8s
时间显示正常,配置完成。