自己搭建ntp服务器

1、背景

终端在使用过程中,发现联网后进行NTP(network time protocol,网络时间协议)校时过程中,经常出现校时失败的问题,特别是本地环境,因此需要在自己这边搭建测试环境。

2、搭建步骤

2.1 安葬服务器

sudo apt-get install ntp

 2.2 配置文件解释

ntpd daemon 在启动后读取ntp.conf配置文件,用于指定同步源、模式和其他相关信息。通常配置文件安装在/etc目录下(/etc/ntp.conf),也可以安装再其他地方(守护进程的-c选项)。

格式与其他UNIX配置文件类似。注释行采用#,空白行被忽略。

配置命令: 关键词   参数列表 [选项]

 用空格分割

下面是一份配置文件

newlif@newlif-virtual-machine:~$ more /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

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

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# 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 limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# 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

#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)
#fudge 127.127.22.1 flag3 1            # enable PPS API

2.3 修改配置文件/etc/ntp.conf

2.3.1 server 表示当前NTP从哪个主机获取时间

利用server设定服务的时间来源:

server  [IP OR HOSTNAME]        [PREFER]

参数说明:

IP OR HOSTNAME:上层ntp服务器的ip地址或者域名
PREFER         : 表示优先使用的主机

本地部署,用自身的时间作为NTP服务的时间来源,如下图所示。

 其中fudge 是用来指定NTP服务来源的层级,如5或10.

2.3.2 restrict 限制哪些客户端可以使用这个NTP服务或者有哪些限制

一般格式: restrict  IP地址  mask 子网掩码  参数

参数

参数详细描述
ignore拒绝所有类型的报文,包括ntpq和ntpdc查询。
nomidify客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
notrust客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery拒绝ntpq和ntpdc查询。不影响时间服务。
notrap不提供trap远端登陆
nopeer用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
kod访问违规时发送 KoD 包(kiss of death)
limited如果数据包违反了discard命令设置的速率限制,则拒绝时间服务。这不适用于ntpq和ntpdc查询。  终端出现过
-4限制只允许IPv4地址访问NTP服务
-6限制只允许IPv6地址访问NTP服务

restric 127.0.0.1 表示当前主机可以使用这个时间服务

restric 172.16.1 0 mask 255.255.255.0 表示172.16.1网络的所有主机可以使用这个时间服务

restric 0.0.0.0 mask 0.0.0.0 nomodify notrap 表示所有主机都可以访问这个时间服务

restrict -4 default kod notrap nomodify nopeer noquery  对于IPv4地址,notrap nomodify

 nopeer noquery,如果违反了规则,则会发送一个"kiss-o'-Death"死亡之吻包

2.3.4 配置存储系统时钟的漂移率

driftfile      :记录时间差异。因为预设的NTP Server本身的时间计算是依据BIOS的晶片震荡周期频率来计算的,但是这个数值与上层Time Server有误差。所以NTP这个daemon会自动的去计算我们自己主机的频率与上层TimeServer的频率,并且将两个频率的误差记录下来,记录下来的档案就是在driftfile后面接的完整文件名中了

driftfile /var/lib/ntp/ntp.drift   漂移文件路徑

2.3 重新启动ntp

/etc/init.d/ntp restart

2.4 修改本地时间

sudo date -s 12:00:01

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值