NTP服务器

使用 date 命令查看 Ubuntu 系统中的当前时区:

1
date

示例输出:

1
Mon 16 Dec 2019 06:34:37 AM UTC

如上所见,date 命令显示实际日期和当前时间。我当前的时区是 UTC,代表协调世界时

现在,让我们看看时钟是否与互联网同步。只需运行:

1
timedatectl

示例输出:

1
2
3
4
5
6
7
               Local time: Mon 2019-12-16 06:35:03 UTC
Universal time: Mon 2019-12-16 06:35:03 UTC
RTC time: Mon 2019-12-16 06:35:06
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: active
RTC in local TZ: no

timedatectl 命令显示本地时间、世界时、时区以及系统时钟是否与互联网服务器同步,以及 NTP service 是处于活动状态还是非活动状态。就我而言,System clock synchronized: no,系统时钟与互联网时间服务器不同步。

如果你看到 System clock synchronized: 值设置为 no,那么 timesyncd 服务可能处于非活动状态。因此,只需重启服务并看下是否正常。

1
sudo systemctl restart systemd-timesyncd.service

现在检查 timesyncd 服务状态:

1
sudo systemctl status systemd-timesyncd.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
● 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 Mon 2019-12-16 06:35:48 UTC; 2min 21s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 1624 (systemd-timesyn)
Status: "Idle."
Tasks: 2 (limit: 2208)
Memory: 1.2M
CGroup: /system.slice/systemd-timesyncd.service
└─1624 /lib/systemd/systemd-timesyncd

Dec 16 06:35:48 sky systemd[1]: Stopping Network Time Synchronization...
Dec 16 06:35:48 sky systemd[1]: systemd-timesyncd.service: Succeeded.
Dec 16 06:35:48 sky systemd[1]: Stopped Network Time Synchronization.
Dec 16 06:35:48 sky systemd[1]: Starting Network Time Synchronization...
Dec 16 06:35:48 sky systemd[1]: Started Network Time Synchronization.
Dec 16 06:35:58 sky systemd-timesyncd[1624]: Timed out waiting for reply from 91.189.89.199:123 (ntp.ubuntu.com).

你可以使用命令验证是否启用了时间同步:

1
timedatectl

如果仍然不起作用,请运行以下命令以启用时间同步:

1
sudo timedatectl set-ntp true

现在,你的系统时钟将与互联网时间服务器同步。

但是这里我们看到Status: "Idle.",说明ntp服务器空闲的。

使用以下命令修改启用:

1
sudo vi /etc/systemd/timesyncd.conf
1
2
3
[Time]
NTP=ntp1.aliyun.com
FallbackNTP=ntp2.aliyun.com

这里本人改为阿里的ntp服务器ntp.ubuntu.com可能会超时。重启服务生效:

1
sudo systemctl restart systemd-timesyncd.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ sudo systemctl status systemd-timesyncd.service
● 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 Mon 2019-12-16 16:16:43 CST; 19s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 5140 (systemd-timesyn)
Status: "Synchronized to time server for the first time 120.25.115.20:123 (ntp1.aliyun.com)."
Tasks: 2 (limit: 2208)
Memory: 1.1M
CGroup: /system.slice/systemd-timesyncd.service
└─5140 /lib/systemd/systemd-timesyncd

Dec 16 16:16:42 sky systemd[1]: Stopping Network Time Synchronization...
Dec 16 16:16:42 sky systemd[1]: systemd-timesyncd.service: Succeeded.
Dec 16 16:16:42 sky systemd[1]: Stopped Network Time Synchronization.
Dec 16 16:16:42 sky systemd[1]: Starting Network Time Synchronization...
Dec 16 16:16:43 sky systemd[1]: Started Network Time Synchronization.
Dec 16 16:16:43 sky systemd-timesyncd[5140]: Synchronized to time server for the first time 120.25.115.20:123 (ntp1.aliyun.com).

更改时区

将UTC 时区换为中国标准时区 CST。

使用命令列出可用时区:

1
timedatectl list-timezones

换为中国标准时区 CST。

1
sudo timedatectl set-timezone Asia/Shanghai

使用 timedatectl 命令检查时区是否已真正更改:

1
2
3
4
5
6
7
8
$ timedatectl
Local time: Mon 2019-12-16 16:21:35 CST
Universal time: Mon 2019-12-16 08:21:35 UTC
RTC time: Mon 2019-12-16 08:21:35
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no