NTP协议概述
当前用于在计算机上同步时间的NTP协议可以在局域网上构建NTP服务器来同步时间。 网络时间协议(NTP )是用于同步计算机时间的协议,可以同步服务器或时钟源(石英时钟、GPS等),从而提供高精度的时间修改。
NTP可通过原子钟、天文台、卫星等途径获得准确的时间,然后在NTP服务器级传播。 NTP的网络结构是分层管理的类树结构,Stratum-0是外部时钟源,Stratum-1层从Stratum-0层获取时间,Stratum-2层从Stratum-1层获取时间。 这样,Stratum层的总数需要限制在15层以内。
如下图所示,NTP网络体系很难看。 不要见怪~
NTP网络协议体系
使用时间服务器进行时间同步时,NTP客户端首先发出时间同步请求,并与时间服务器交换时间。 交换的结果是,客户端计算时间延迟并协调与时间服务器的同步。 要查看详细的NTP消息传递协议,请单击此处分析NTP协议。
NTP动作模式
以上是NTP协议的简单信息。 接下来对NTP的3种动作模式进行说明。
主/被动对称工作模式:该模式采用一对一连接,双方均可同步对方或同步对方。 首先申请建立连接的是主动动作模式,另一个是被动动作模式。
客户端/服务器模式:此模式也是一对一连接的。 与主/被动模式一样,客户端在服务器时间同步,而服务器不与客户端同步。
广播模式:在一对多连接中,服务器主动发布时间信息,而不管客户端以何种模式运行。
上述三种操作模式都使用UDP协议发送时间信息,客户端在接收到分组后计算时间偏移量和数据传输延迟时间。
安装和配置NTP服务器
安装NTP服务
安装命令如下:
sudo apt-get安装NTP
现在已经安装了NTP服务。 然后配置NTP。
配置NTP服务
NTP常规配置选项:
服务器
使用server选项,可以按以下格式设置顶级NTP服务器的IP地址:
服务器IP或域名
严格的
restric主要用于设置NTP权限,格式如下
restric ip地址掩码子网掩码参数
参数可以是以下参数之一:
ignore :关闭所有NTP在线服务
nomodify:客户端可以通过服务器上网络学校,但客户端不能更改服务器端的时间参数
notrust除非客户端经过验证,否则负责客户端的源将被视为不可靠的子网
noquery:不提供客户端时间查询
差速文件
driftfile用于指定文件。 文件记录与顶级NTP服务器连接所用的时间。 形式如下。
drift文件文件名
NTP服务器的配置示例如下所示。
NTP配置示例
可以通过修改NTP服务器的参数来配置NTP主配置文件/etc/ntp.conf。
ntp.conf文件的编辑内容如下。
# /etc/ntp.conf,configuration for ntpd; seNTP.conf(5) for help
driftfile/var/lib/NTP/NTP.drift
#已启用hisifyouwantstatisticstobelogged。
#statsdir /var/log/ntpstats/
satisticsloopstatspeerstatsclockstats
filegenloopstatsfileloopstatstypedayenable
filegenpeerstatsfilepeerstatstypedayenable
filegenclockstatsfileclockstatstypedayenable
# specify one or more NTP服务器。
# useserversfromthentppoolproject.approvedbyubuntutechnicalboard
#on2011-02-08(LP:#104525 ).see http://www.pool.NTP.org/join.html for
# more信息。
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool。
ntp.org# Use Ubuntu's ntp server as a fallback.
server ntp.ubuntu.com
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.
# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
restrict 10.12.8.196 mask 255.255.255.0 nomodify
# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255
# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
配置完成后,需要重新启动NTP服务。
sudo service ntp restart
这样我们就配置好了NTP服务器,下面教大家如何通过客户端来同步时间。
同步时间
在ubuntu 下使用ntp同步时间:
lixinxing@Ideapad-Flex:~$ sudo ntpdate -u 10.12.8.196
19 Apr 13:54:17 ntpdate[6148]: adjust time server 10.12.8.196 offset -0.003172 sec
在windows下同步时间
首先打开控制面板中的时间与日期设置,选择Internet时间,点击更改设置
在服务器中输入10.12.8.196,这里10.12.8.196是你搭建的NTP服务器的地址,然后单击立即更新,可以看到时间已经同步完成的信息,接下来会每隔一周时间自动与服务器同步时间。