(1)安装NTP服务
自行安装,在命令行终端输入:
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo apt-get -y install ntp
(2)修改配置文件/etc/ntp.conf
以服务器 192.168.20.21 的时间与 ntp.ubuntu.com 同步,允许同步的 NTP 客户端所在的网段为192.168.20.0/255.255.255.0为例
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo vim /etc/ntp.conf
以下为配置文件内容:
driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server ntp.ubuntu.com
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
restrict 192.168.20.0 mask 255.255.255.0 nomodify
restrict 127.0.0.1
restrict ::1
Tips:其中 server ntp.ubuntu.com,当连接外网的时候,本机首先要同步的就是 ntp.ubuntu.com 的时间;当用局域网的时候,应把地址设为标准时间的机器IP。如果在同步时间的时候出现“Service dropped: strata too high”的报错,应对配置文件的 server 进行如下修改:
server 127.127.1.0 fudge
127.127.1.0 stratum 8
重启服务生效:
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo systemctl restart ntp
(3)在客户端执行下列命令就可以进行同步:
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo ntpdate 192.168.20.21
需等待几分钟时间才能同步完成。可同时查看服务器的硬件时钟并进行更改:
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo hwclock
ruoshui@ruoshui-VMware7-1:~/桌面$ sudo hwclock -w
(4)可以在服务器端执行下面的命令来查看同步的过程:
$ ntpq -p