ntp-server服务端部署和配置

1.前言

Atomic Clock: 现在计算时间最准确的是使用 原子震荡周期 所计算的物理时钟(Atomic Clock),因此也被定义为标准时间(International Atomic Time)

UTC(coordinated Universal Time): 协和标准时间 就是利用 Atomic Clock 为基准定义出来的正确时间(又称世界统一时间,世界标准时间,国际协调时间)。

硬件时钟: 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因

系统时钟: 就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值

NTP(Network Time Protocol 网络时间协议)是一个用于同步计算机时钟的网络协议。它可以使计算机与其他服务器或时钟源进行时间同步,进行高精度的时间校正。

2.系统与软件版本

2.1 系统版本

CentOS6.5 x86_64

2.2 ntpd软件版本

ntp-4.2.8p10.tar.gz

2.3 下载地址

官方下载地址:http://support.ntp.org/bin/view/Main/SoftwareDownloads

3.编译安装

3.1 安装依赖

1
yum install gcc gcc-c++ openssl-devel libstdc++* libcap*

3.2 备份相关目录

1
2
3
4
5
6
7
8
9
cp -ar /etc/ntp /etc/ntp.bak

cp /etc/ntp.conf /etc/ntp.conf.bak

cp /etc/init.d/ntpd /etc/init.d/ntpd.bak

cp /etc/sysconfig/ntpd /etc/sysconfig/ntpd.bak

cp /etc/sysconfig/ntpdate /etc/sysconfig/ntpdate.bak

3.3 卸载原来的ntp软件

1
yum erase ntp ntpdate

3.4 编译ntp

1
2
3
4
5
6
7
#创建/var/lib/ntp目录
install -v -m710 -o ntp -g ntp -d /var/lib/ntp
#解压
tar -zxvf ntp-4.2.8p10.tar.gz
cd ntp-4.2.8p10
./configure --prefix=/usr --bindir=/usr/sbin --sysconfdir=/etc --enable-linuxcaps --with-lineeditlibs=readline --docdir=/usr/share/doc/ntp-4.2.8p10 --enable-all-clocks --enable-parse-clocks --enable-clockctl
make && make install

编译参数解释:

1
2
3
4
5
6
7
--bindir指定二进制文件的安装位置

--enable-linuxcaps --enable-clockctl ntpd用ntp普通用户运行,使时钟控制功能不受root用户控制

--enable-all-clocks --enable-parse-clocks 允许所有可解析的时钟

--with-lineeditlibs=readline This switch enables Readline support for ntpdc and ntpq programs. (允许ntpdc和ntpq命令可以以readline模式运行)

3.5 创建配置文件

1
2
3
4
5
6
7
8
9
cp /etc/init.d/ntpd.bak /etc/init.d/ntpd

cp /etc/sysconfig/ntpd.bak /etc/sysconfig/ntpd

cp /etc/sysconfig/ntpdate.bak /etc/sysconfig/ntpdate

mv /etc/ntp.bak /etc/ntp

cp /etc/ntp.conf.bak /etc/ntp.conf

3.6 配置主配置文件/etc/ntp.conf

ntp.conf主要参数详解:

ntp.conf里主要可以使用如下几个命令:restrict,server,driftfile,keys

其中server是设定上级时间服务器用的,而restrict是设定哪台服务器可以和ntp server进行时间同步,具有什么样的权限。driftfile是用来指定记录时间差异的文件,keys是用来指定认证key文件的(这里不用)。

先来看restrict的格式为:
restrict [客户端IP] mask [netmask_IP] [parameter]

客户端IP,就是都是哪几台服务器要和这台ntp server进行同步的ip地址
最后的parameter可以有如下几个参数:
ignore:拒绝连接到ntp server
nomodiy:可以连接到ntp server,但是不能对ntp server进行时间上的修改
noquery:不提供对ntp server查询时间,也就是拒绝和ntp server进行时间同步
notrust:对没有认证的客户端不提供服务

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。kod技术可以阻止“Kiss of Death”包(一种Dos攻击)对服务器的破坏,使用此参数开启该功能。

restrict default nomodify notrap noquery
此项设置的含义是不允许其他计算机修改或查询配置在本机linux系统上的NTP服务。其中default表示所有IP.

restrict 127.0.0.1
开放本机内部回环网络接口,以便于在本地对NTP服务进行监控及配置。

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
允许192.168.1.0网络段的NTP客户端使用本NTP服务器进行网络校进,但不允许它们修改本机的NTP服务配置。如想开放此项功能需要根据实际网络情况修改网络段及掩码,并把注释符”#”去掉。

server 0.pool.ntp.org

指定上层的NTP服务器。linux系统中默认指定Internet上的时间服务器池中的时间服务器作为上层NTP服务器。NTP服务器池pool.ntp.org中拥有三百多台自愿加入其中的公共NTP服务器。如果想更准确的校时也可以自己指定上层NTP服务器。

‚server命令的格式是:
server [IP or hostname] [prefer]
其中[IP or hostname]为上级时间服务器的IP或者域名,主机名:
可以是192.168.12.177形式,或者time.nist.gov再或者ntpserver这样的形式。
后面的[prefer]参数是可选的,加上prefer后,ntp server和上级时间服务器同步时会优先先访问加了prefer这行的进行同步。

iburst:当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。

server 127.127.1.0
fudge 127.127.1.0 stratum 10
127.127.1.0是一个特殊的地址,代表本机的系统时钟。fudge是指定本地时间源的层号,数字越大,优先级越低。所以当有外部时间源时会优先使用外部时间源。

driftfile /var/lib/ntp/drift
指定记录与上层NTP服务器联系时所花费时间的文件,指定了用来保存系统时钟频率偏差的文件, ntpd程序使用它来自动地补偿时钟的自然漂移, 从而使时钟即使在切断了外来时源的情况下,仍能保持相当的准确度。

还可以在/etc/ntp.conf文件中还可以进行如下设置
peer 192.168.16.100 #设置IP地址为192.168.16.100的NTP服务器可以与本机的NTP服务器相互进行网络校时
broadcast 224.0.1.1 #224.0.1.1是多播网址,设置该NTP服务器可对所有能访问到的网段进行多播
broadcast 192.168.1.255 #设定该NTP服务器可对子网192.168.1.0/24中的所有计算机定期广播正确的时间

为了让本机的NTP服务器能够到指定的时间源那进行同步,还必须修改/etc/ntp/step-tickers文件,在该文件中把所用的上层NTP服务器的IP地址或域名加入即可。

为了每天与NTP服务器保持时间同步,可以将命令写入到cron中。

附一份ntp server的主配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
#我们每一个system clock的频率都有小小的误差,这个就是为什么机器运行一段时间后会不精确. NTP会自动来监测我们时钟的误差值并予以调整.
#但问题是这是一个冗长的过程,所以它会把记录下来的误差先写入driftfile.这样即使你重新开机以后之前的计算结果也就不会丢失了
driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# 允许所有客户端使用这个服务
restrict default nomodify
#restrict default kod nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
#restrict 127.0.0.1
#restrict -6 ::1
server pool.ntp.org iburst
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# 本机同步远程的ntp的服务器域名
# perfer:表示优先级最高
# burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
# iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测
#NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
fudge 127.127.1.1 stratum 10
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
#设置ntp日志的path
statsdir /var/log/ntp/
##设置ntp日志文件
logfile /var/log/ntp/ntp.log

##watch ntpq -p
#
#参数说明:
#remote: 它指的就是本地机器所连接的远程NTP服务器
#refid: 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器
#st: 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端. 所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.
#t: 未知
#when: 我个人把它理解为一个计时器用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步
#poll: 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小
#reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
#delay: 从本地机发送同步要求到服务器的round trip time
#offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近
#jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小我们和服务器的时间就越精确
#
#tinker panic 0
#server 0.pool.ntp.org prefer
#server 1.pool.ntp.org iburst

4.启动ntp服务

1
2
3
4
5
6
7
service ntpd start

service ntpd status

chkconfig --level 345 ntpd on

chkconfig --list ntpd

5.查看ntp与上层ntp的状态

ntp_server

参数说明:

目前正在使用的上层NTP。+已连线,可提供时间更新的候补服务器

remote:响应这个请求的NTP服务器的主机名或IP。

refid:remote端NTP服务器的上级NTP的IP。

st:remote远程服务器的级别.由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16.为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.

t:类型。“u”表示单播。其他值有:本地、多播和广播。

when:几秒钟前曾做过时间同步更新。

poll:本地主机和远程服务器多少时间进行一次同步(单位为秒).在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小

reach:这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加。377表示100%成功。

delay:从本地发送同步要求到服务器的往返时间。

offset:本地主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近。

jitter 这是一个用来做统计的值.它统计了在特定个连续的连接数里offset的分布情况.简单地说这个数值的绝对值越小,主机的时间就越精确,以ms为单位.

6.配置/etc/sysconfig/ntpd文件

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

允许BIOS与系统时间同步,也可以通过hwclock -w 命令

SYNC_HWCLOCK=yes

7.客户端配置

1
2
3
crontab -e
#设置定时同步ntp 服务的时间
00 01 * * * root /usr/sbin/ntpdate 10.201.3.90; /sbin/hwclock -w
# 推荐文章
  1.docker常用容器管理命令
  2.docker镜像管理
  3.dockerfile指令
  4.深刻理解Docker镜像大小
  5.k8s专题[1.k8s基础概念]

评论


:D 一言句子获取中...