1. NTP 的基本资料
- NTP:Network Time Protocol 网络时间协议,是用来同步服务器的网络时间的;用的UDP端口是123
这个时间标准就是世界标准时间,我们的时间跟这个时间进行同步;但是根据不同的时间在我们国家有一个标准时间,
是北京时间,我们的服务器一般根据北京时间进行同步。
2. 服务器的时间有两个
- 软件时钟: date
date +%s 1356781495 $ date +"%Y-%m-%d %H:%M:%S" 2012-12-29 19:45:16
- 硬件时间:BIOS时间
hwclock -r Sat 29 Dec 2012 07:44:30 PM CST -0.700987 seconds
3. NTP 的意义:
当我们的服务器时间不准确的时候可以同步NTP 进行时间矫正;对于一两台机器我们可以手动矫系统时间
date --set "1/1/09 00:01" <== (月/日/年时:分:秒) 2009年 01月 01日 星期四 00:01:00 CST date -s "时间" date --set "时间"
但是一个公司有成千上万台机器,手动修改时间那肯定是不行;就要搭建一个NTP服务器
2. NTP 工作的基本原理
假设:Device B 是NTP的server端,Device A 是NTP的client端;则Device A 时间同步的信息是
Device A 的时钟设定为10:00:00am,Device B 的时钟设定为 11:00:00am。
Device B 作为 NTP 时间服务器,即 Device A 将使自己的时钟与 Device B 的时钟同步。
NTP 报文在 Device A 和 Device B 之间单向传输所需要的时间为 1 秒。
Device A 发送一个 NTP 报文给 Device B,该报文带有它离开 Device A 时的时间戳,该时间戳为10:00:00am(T1)。
当此 NTP 报文到达 Device B 时,Device B 加上自己的时间戳,该时间戳为11:00:01am(T2)。
当此 NTP 报文离开 Device B 时,Device B 再加上自己的时间戳,该时间戳为11:00:02am(T3)。
当 Device A 接收到该响应报文时,Device A 的本地时间为 10:00:03am(T4)。
至此,Device A 已经拥有足够的信息来计算两个重要的参数:
NTP 报文的往返延时
- delay=(T4-T1)-(T3-T2)=2 秒。
Device A 相对 Device B 的时间差
- offset=((T2-T1)+(T3-T4))/2=1 小时。
这样,Device A 就能够根据这些信息来设定自己的时钟,使之与 Device B 的时钟同步。
NTP 具体的工作模式就是client/server ;在client(我们的服务器)上面安装ntp 包,然后启动一个deamon,进行通信;进而同步服务器的时间。
rpm -q ntp ntp-4.2.2p1-15.el5 rpm -ql ntp-4.2.2p1-15.el5 ps aux|grep ntp|grep -v grep ntp 3867 0.0 0.0 23412 5040 ? SLs Aug10 0:13 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
3. NTP 相关工具:
3.1 /usr/sbin/ntpq
[zengtai ~]$ ntpq -pn remote refid st t when poll reach delay offset jitter ============================================================================== +172.23.101.6 121.182.147.191 2 u 196 256 377 0.194 8.422 0.531 *172.23.101.7 121.182.147.191 2 u 27 256 377 0.176 4.283 1.387
- +:这IP 作为辅助NTPserver 和带 * 的服务器一起提供同步服务,当*号 的NTP server 服务器不能用时,可以接管
- *:远程的NTP服务器已经确认的同步我们系统时间的主NTP server;
- remote:远程NTP server 的IP
- refid:NTP server是层状的,remote的NTP server的上一层NTPserver
- delay: 上面基本原理有
- offset:单位是ms + - :服务器 时间跟NTP server的时间差别
3.2 /usr/bin/ntpstat
/usr/bin/ntpstat synchronised to NTP server (172.23.101.6) at stratum 3 time correct to within 118 ms polling server every 128 s
3.3 /usr/sbin/ntptrace
/usr/sbin/ntptrace localhost: stratum 3, offset 0.003508, synch distance 0.119514 ntp101006.cm3.tbsite.net: stratum 2, offset 0.008610, synch distance 0.093111 121.182.147.191: stratum 1, offset 0.000005, synch distance 0.000000, refid 'GPS'
4. NTP 学习资料