ntp时间同步
具体两种模式
1.服务器数量比较少,可以直接与时间服务器同步
2.本地服务器较多,在本地自建时间同步服务器,
时间同步的两个命令
ntpd : 校准时间,一点点的校准过来时间的,最终把时间慢慢的校正对。
ntpd服务可以在修正时间的同时,修正cpu tick
ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行
ntpdate : 直接调整时间,有可能会对程序造成影响
ntpdate [-nv] [ IP/hostname]
# ntpdate -u 192.168.0.2
# ntpdate -u time.ntp.org
# ntpdate -u ntp.sjtu.edu.cn >>/var/log/ntp.log 2>&1;hwclock -w
注意:若不加上-u参数, 会出现以下提示:no server suitable for synchronization found
-u:指定使用无特权的端口发送数据包,可以越过防火墙与主机同步;
理想的做法是:
使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
在计算机刚刚启动,但还没有启动很多服务的那个时候可以使用ntpdate同步时间
一、直接用命令与时间服务器进行同步
实例:
使用ntpdate 进行同步:
ntpdate -u x.x.x.x
这里的x.x.x.x可以是公网时间服务器IP地址,也可以是下面二中我们自建的ntp服务器地址。
可以加一个定时计划,定时同步
1. crontab -e
2. */5 * * * * /usr/sbin/ntpdate -u x.x.x.x
3. /etc/init.d/crond restart
2.这里提供几个公网时间服务器地址
阿里云时间服务器,授时信号来自GPS、北斗两套卫星信号,并配备原子钟守时,
以下7个域名提供服务,大家可以直接使用。
http://time1.aliyun.com
http://time2.aliyun.com
http://time3.aliyun.com
http://time4.aliyun.com
http://time5.aliyun.com
http://time6.aliyun.com
http://time7.aliyun.com
或者直接访问这个地址 time.pool.aliyun.com
# ntpdate -u time.pool.aliyun.com
13 Nov 10:48:28 ntpdate[26527]: adjust time server 182.92.12.11 offset 0.000458 sec
centos也有几个时间服务器(最好还是用国内的吧)
0.centos.pool.ntp.org
1.centos.pool.ntp.org
2.centos.pool.ntp.org
查看ntp服务器与上层ntp服务器的状态:ntpq
#ntpq -p
# 其中:
# remote - 本机和上层ntp的ip或主机名,“+”有连线可做候选,“*”正在使用的
# refid - 更上一层的ntp地址
# st - stratum的 级别
# when - 多少秒前曾经同步过时间
# poll - 下次更新在多少秒后
# reach - 已经向上层ntp服务器要求更新的次数
# delay - 网络延迟
# offset - 时间补偿
# jitter - 系统时间与bios时间差
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*time4.aliyun.co 10.137.38.86 2 u 111 128 377 27.480 -5.995 1.852
配置NTP客户端:
在所有客户端上vim /etc/ntp.conf,添加:
server x.x.x.x
x.x.x.x 为上面配置的ntp服务器地址
注意:当server与client之间的时间误差过大时(可能是1000秒),处于对修改时间可能对系统和应用带来不可预知的问题,NTP将停止时间同步!
所以如果发现NTP启动之后时间并不进行同步时,应该考虑到可能是时间差过大引起的,
此时需要先手动进行时间同步 ntpdate !
本文来自博客园,作者:xiaoming zhang,转载请注明原文链接:https://www.cnblogs.com/xmzhang

浙公网安备 33010602011771号