linux时间服务器搭建与集群时间同步
2016-05-27 14:47阅读:
一、声明:
server IP:
192.168.112.130
1
时间和时区
date :查看当前系统时间
时区 :按照地球所在经度划分。自己可以去查查地球仪了解先关知识。
常见时区:CST(chinese stardard time)中国标准时区
UTC(Coordinated Universal Time)世界统一时间
GMT
格林威治时间
2
设置时间和时区
# cp /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime
设置为上海时间
# cp /usr/share/zoneinfo/posix/UTC /etc/localtime
设置为UTC时间
三、NTP服务简易原理
1)分层管理的类树形结构。网络中的节点有两种可能:时钟源或客户
2)所有服务器归入不同的Stratun(层)中。Stratum-1在顶层,有外部UTC接入,而Stratum-2则从Stratum-1获取时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构相互连接,而Stratum-1的时间服务器是整个系统的基础,也是顶端!

3)NTP的工作模式为典型的C/S模型
四、重要性
NTP服务器在普通行业里,由于对时间的
精度要求不高,所以几乎很少搭建,但是对于一些科研机构,对于时间的精度要求非常高,
NTP服务器的安装、调试
一、安装
安装方法有两种:yum 和source包安装,这里以yum安装为例:
首先查看系统是否安装ntp包
[root@node3 jfh]# rpm -q ntp || yum -y install ntp
ntp-4.2.6p5-1.el6.centos.x86_64
############源码包安装方法如下############
以源代码为例
# wget
http://ntp.buptnet.edu.cn/ntp_chinese/down/server/ntp-4.2.0.tar.gz
# tar -zxvf ntp-4.2.0.tar.gz �C /usr/local/src;cd
/usr/local/src/ntp-4.2.0 2. 开始设定参数、编译与安装:
#./configure
--prefix=/usr/local/ntp --enable-all-clocks
--enable-parse-clocks # make clean ; make
# make check # make install
############
END
##################
二、配置NTP配置文件/etc/ntp.conf
1. 编辑配置文件 /etc/ntp.conf
# vi /etc/ntp.conf
# 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).
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 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 default nomodify notrap
noquery
restrict 127.0.0.1
restrict 192.168.112.0 mask
255.255.255.0 nomodify
restrict -6 ::1
# Hosts on local network are less
restricted.
#restrict 192.168.1.0 mask
255.255.255.0 nomodify notrap
# Use public servers from the
pool.ntp.org project.
# Please consider joining the pool
(http://www.pool.ntp.org/join.html).
server 210.72.145.39
prefer
server asia.pool.ntp.org
server
210.72.145.44
server 0.rhel.pool.ntp.org
iburst
server 1.rhel.pool.ntp.org
iburst
server 2.rhel.pool.ntp.org
iburst
server 3.rhel.pool.ntp.org
iburst
logfile
/var/log/ntp.log
#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
[root@node3 wwwjfhotwcom]# service ntpd
restart
Shutting down ntpd:
[ OK ]
Starting ntpd:
[ OK ]
4.以下为正常启动日志message
Mar 24 18:29:45 gang ntpd[3547]: ntpd 4.2.6p5@1.2349-o Sat Nov 23
18:21:48 UTC 2013 (1) Mar 24 18:29:45 gang ntpd[3548]: proto:
precision = 0.628 usec Mar 24 18:29:45 gang ntpd[3548]: 0.0.0.0
c01d 0d kern kernel time sync enabled Mar 24 18:29:45 gang
ntpd[3548]: line 32 column 1 syntax error, unexpected T_String,
expecting $end Mar 24 18:29:45 gang ntpd[3548]: syntax error in
/etc/ntp.conf line 32, column 1 Mar 24 18:29:45 gang ntpd[3548]:
Listen and
drop on 0
v4wildcard 0.0.0.0 UDP
123 Mar 24
18:29:45 gang
ntpd[3548]: Listen and drop
on 1 v6wildcard :: UDP 123
Mar 24 18:29:45
gang ntpd[3548]: Listen normally on
2 lo 127.0.0.1
UDP 123 Mar 24
18:29:45 gang
ntpd[3548]: Listen normally on
3 br0
192.168.0.199 UDP
123 Mar 24
18:29:45 gang
ntpd[3548]: Listen normally on
4 eth0
fe80::eea8:6bff:fea4:912b UDP
123 Mar 24
18:29:45 gang
ntpd[3548]: Listen normally on
5 br0
fe80::eea8:6bff:fea4:912b UDP
123 Mar 24
18:29:45 gang
ntpd[3548]: Listen normally on
6 lo ::1 UDP 123 Mar
24 18:29:45
gang ntpd[3548]: peers refreshed Mar 24
18:29:45 gang
ntpd[3548]: Listening on routing socket
on fd #23 for interface
updates
……………………………………NTP 服务器配置完成………………………………
###################NTP 客户端的配置##################
1、手动同步
[root@node4 ~]# ntpdate
192.168.112.130
27 May 14:44:40 ntpdate[31896]: step
time server 192.168.112.130 offset -14.740805
sec
2、也可写执行计划 crontab -e
15 * * * * /sbin/ntpdate 192.168.112.130
##相关排错方式及讲解:
问题:
解决方法: 在ntp
server上重新启动ntp服务后,ntp
server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no
server suitable for synchronization
found的错误。
在server端,我只配置了上级服务器:asia.pool.ntp.org之后,发现启动日志正常,但是时间一直不同步。输入命令查看得知如下图提示:
[root
@gang ~]
# ntpq -p remote refid st t
when poll reach delay offset jitter
==============================================================================
proxy02.ispros. .
INIT.
16 u -
256 0 0.
000
0.
000
0.
000
发现pool 一直在增加,但是一直不同步,ping asia.pool.ntp.org发现延迟较高,甚至不通。
[root
@gang ~]
# ping asia.pool.ntp.org
PING asia.pool.ntp.org (
78.111.
50.2)
56(
84) bytes of data. ^C ---
asia.pool.ntp.org ping statistics ---
28 packets
transmitted,
0 received,
100% packet
loss,
time 27241ms
解决方法:在配置文件中,添加多个server ,以防单一server连接不通时,时间同步异常。配置完成后,正常情况如下图:
[root
@gang ~]
# ntpq -p remote refid st t
when poll reach delay offset jitter
==============================================================================
*time2.isu.net.
s .GPS.
1 u
2 64 27 341.015
-
64.352 16.181
210.72.
145.44 .INIT.
16 u -
64 0 0.
000
0.
000 0.
000
LOCAL(
1) .NIST.
10 l
13
64 177 0.
000
0.
000
0.
000
ntpq -p输出结果说明(摘录部分):
remote: 它指的就是本地机器所连接的远程NTP服务器
refid: 它指的是参考的上一层NTP主机的地址
st: 远程服务器的级别. 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.
所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.
when: 用做计时,用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步
poll: 本地机和远程服务器多少时间进行一次同步(单位为秒).
在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小
reach: 这是一个八进制值,表示已经向上层NTP伺服器要求更新的次数。每成功连接一次它的值就会增加。
delay: 网路传输过程当中延迟的时间,单位为 10^(-6) 秒
offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别.
offset越接近于0,我们就和服务器的时间越接近。单位为10^(-3)秒
jitter:
Linux系统时间与BIOS硬件时间的差异时间,单位为10^(-6)秒。简单地说这个数值的绝对值越小我们和服务器的时间就越精确。