Ubuntu 18自带了systemd-timesyncd服务来同步时钟,可以使用以下步骤来进行配置:
打开终端窗口,进入root用户权限:sudo su。
检查系统当前的时钟同步状态:
timedatectl
如果输出类似下面的信息,表示NTP同步功能已经开启:
Local time: Wed 2019-11-27 10:16:48 CST
Universal time: Wed 2019-11-27 02:16:48 UTC
RTC time: Wed 2019-11-27 02:16:48
Time zone: Asia/Shanghai (CST, +0800)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
如果`NTP synchronized`为`yes`,则无需进行时钟同步的配置,可直接退出。
3. 如果系统未开启NTP同步,则需手动进行配置:
- 编辑`/etc/systemd/timesyncd.conf`配置文件,将`NTP`选项设置为NTP服务器地址:
[Time]
NTP=ntp.ubuntu.com
如果需要指定多个NTP服务器,可以用空格隔开。
- 启动`systemd-timesyncd`服务:
systemctl start systemd-timesyncd
- 查看时钟同步状态:
timedatectl
如果输出类似下面的信息,则表示时钟同步成功:
Local time: Wed 2019-11-27 10:16:48 CST
Universal time: Wed 2019-11-27 02:16:48 UTC
RTC time: Wed 2019-11-27 02:16:48
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
4. 如果需要将`systemd-timesyncd`服务设置为开机自启动,可以使用以下命令:
systemctl enable systemd-timesyncd
5. 查询ntp服务器时间
ntpdate -q ip
对比时间,你的Ubuntu 18系统的时钟已经同步了。