ntp

局域网中client和server时间保持时间同步需要ntp服务

ntp服务只需要在Server上配置即可,Client使用ntpdate来同步Server时间

1、配置Server(10.121.63.240)端ntp配置文件/etc/ntp.conf

hadoop@muhe221:~/hadoop/name$ sudo apt-get install ntp
hadoop@muhe221:~/hadoop/name$ sudo apt-get install ntpstat
muhe221@muhe221:~$ ntpstat
unsynchronised
polling server every 8 s
hadoop@muhe221:~/hadoop/name$ service --status-all
[ + ] kmod
[ + ] lightdm
[ - ] mountall-bootclean.sh
[ - ] mountall.sh
[ + ] networking
[ + ] nmbd
[ + ] ntp
[ + ] ssh
[ + ] thermald
[ + ] udev
[ + ] ufw
[ - ] umountfs
可以看到ntp服务已经启动([+]表示已经启动。)  -----这个有点假 可能不正确ntp服务实际上状态可能不对

sudo vim /etc/ntp.conf

driftfile /var/lib/ntp/ntp.drift

# 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

# 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  # 开启内部递归网络接口 lo  
restrict ::1

server 127.127.1.0
fudge  127.127.1.0 stratum 8

# 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
#在内部子网里面的客户端可以 进行网络校时,但不能修改NTP服务器的时间参数   restrict
10.121.0.0 mask 255.255.0.0 nomodify # 接受 10.121.*.* 段客户端

hadoop@muhe221:~$ sudo service ntp restart
hadoop@muhe221:~$ ntpstat
hadoop@muhe221:/var/log/ntpstats$ ntpstat
synchronised to local net at stratum 9           #这里是服务器同步本地时间
time correct to within 11 ms
polling server every 64 s

设置开机自启动
chkconfig ntpd on

2、Client(10.121.63.215)端同步Server时间

2.1 使用ntpdate手动将Client同步Service时间(不推荐使用此方法)
hadoop@caoming:~/hadoop/name$ sudo apt-get install ntpdate
hadoop@caoming:~$ date
2019年 02月 22日 星期五 18:10:45 CST
hadoop@caoming:~$ sudo ntpdate 10.121.63.240 #手动同步时间
23 Feb 19:16:53 ntpdate[14267]: step time server 10.121.63.240 offset 90300.071368 sec
hadoop@caoming:~$ date #同步后查看本地时间发现它变化了
2019年 02月 23日 星期六 19:16:55 CST
hadoop@caoming:~$ sudo ntpdate 10.121.63.240
23 Feb 19:17:34 ntpdate[14274]: adjust time server 10.121.63.240 offset 0.000059 sec

设置Client每分钟自动同步

在10.121.63.215这台主机上面,编辑/etc/crontab这个文件,用于设置此节点每分钟要向主服务器同步时间 
hadoop@caoming:~$ sudo vi /etc/crontab

# m  h   dom mon dow user    command
* * * * * root /usr/sbin/ntpdate 10.121.63.240;/sbin/hwclock -w 

2.2 使用ntp服务来将client同步Server时间
hadoop@caoming:~/hadoop/name$ sudo apt-get install ntp
hadoop@caoming:~/hadoop/name$ sudo apt-get install ntpstat
配置/etc/ntp.conf

driftfile /var/lib/ntp/ntp.drift

# 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
 
# 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

server 10.121.63.240

# Needed for adding pool entries
restrict source notrap nomodify noquery

$ sudo service ntp start

过大概10分钟(具体多久不清楚)左右,时间会自动同步服务器时间

hadoop@caoming:~$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*muhe221         LOCAL(0)         9 u    1   64  377    0.228    0.211   0.021
hadoop@caoming:~$ ntpstat       #时间同步的状态,这个状态可能会有时间延迟
synchronised to NTP server (10.121.63.240) at stratum 10
time correct to within 16 ms
polling server every 64 s

字段说明:
remote: 它指的就是本地机器所连接的远程NTP服务器
refid : 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器
st: 远程服务器的层级别(stratum).
when: 距离上一次同步多久,单位为秒
poll: 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小
reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay: 从本地机发送同步要求到服务器的round trip time
offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近
jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小我们和服务器的时间就越精确

命令: restrict IP地址 mask 子网掩码 参数
注:
1)其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
2)参数有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询

NTP在时间差大于一天的情况下是无法同步的。

层的概念
这些问题主要涉及到NTP的层(stratum)的概念,顶层是1,值为0时表示层数不明,层的值是累加的,比如NTP授时方向是A-〉B-〉C,假设A的stratum值是3,那么B从A获取到时间,B的stratum置为4,C从B获取到时间,C的值被置为5。一般只有整个NTP系统最顶层的服务器stratum才设为1。
NTP同步的方向是从stratum值较小的节点向较大的节点传播,如果某个NTP客户端接收到stratum比自己还要大,那么NTP客户端认为自己的时间比接受到的时间更为精确,不会进行时间的更新。

 

posted @ 2019-02-23 19:40  牧 天  阅读(498)  评论(0)    收藏  举报