NTP 是 Network Time Protocol 的缩写,是网络对时服务。
网络对时有两种方式进行,一种是使用 ntpdate 命令后面接时间服务器地址进行手动对时,别一种使用 ntpd 服务进行自动对时。
ntpdate 是一个命令行工具,可以接直接使用它来和时间服务器进行对时。
$ ntpdate ntp1.aliyun.com23 Dec 19:49:34 ntpdate[13228]: adjust time server 120.25.115.20 offset -0.002805 sec
ntpdate 与 ntpd 不能同时使用,当 ntpd 后台进程启动时,使用 ntpdate 对时会报错
$ ntpdate ntp1.aliyun.com23 Dec 19:50:38 ntpdate[17942]: the NTP socket is in use, exiting
ntpd 即可作为时间服务器,又可以作为时间客户端。作为时间服务器时,/etc/ntp.conf 配置文件如下:
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for helpdriftfile /var/lib/ntp/ntp.drift#日志文件statsdir /var/log/ntpstats/statistics loopstats peerstats clockstatsfilegen loopstats file loopstats type day enablefilegen peerstats file peerstats type day enablefilegen clockstats file clockstats type day enable#上层ntp server# server cn.pool.ntp.org preferserver ntp1.aliyun.com prefer#让NTP Server和其自身保持同步,如果在/etc/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端server 127.127.1.0fudge 127.127.1.0 stratum 5#不允许来自公网上ipv4和ipv6客户端的访问restrict -4 default kod notrap nomodify nopeer noqueryrestrict -6 default kod notrap nomodify nopeer noquery#运行上层ntp server的所有权限# restrict cn.pool.ntp.orgrestrict ntp1.aliyun.com#允许这个网段的对时请求#restrict 10.138.0.0 mask 255.255.0.0 nomodify# Local users may interrogate the ntp server more closely.restrict 127.0.0.1restrict ::1
查看时间同步状态,可以使用 ntpq -p 查看与上级时间服务间的时间差
$ ntpq -premote refid st t when poll reach delay offset jitter==============================================================================120.25.115.20 10.137.53.7 2 u 4 64 3 27.903 32.135 32.809*LOCAL(0) .LOCL. 5 l 10 64 3 0.000 0.000 0.000
如果不作为服务器,只想使用 ntpd 作为客户端同步时间,/etc/ntp.conf 配置如下:
driftfile /var/lib/ntp/driftrestrict default kod nomodify notrap nopeer noqueryrestrict -6 default kod nomodify notrap nopeer noqueryrestrict 127.0.0.1restrict -6 ::1# 本地时间服务器 IPserver 10.0.0.1 iburstincludefile /etc/ntp/crypto/pwkeys /etc/ntp/keys
客户端查看时间同步状态同样使用 ntpq -p
$ ntpq -premote refid st t when poll reach delay offset jitter==============================================================================*10.0.0.1 LOCAL(0) 6 u 871 1024 275 0.563 -0.212 0.564
文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




