3358 www.Sina.com/http://www.Sina.com/http://www.Sina.com /
3358 www.Sina.com/http://www.Sina.com /校准时间,一点一点地匹配时间,但最终逐渐匹配时间。 ntpd服务可以在修改时间的同时修改cpu tick。 ntpd有自我保护设置: 如果本机和源时间太长,ntpd将无法工作
具体两种模式:不考虑其他程序是否会引起阵痛,直接调整时间,可能会影响程序
ntpdate [-NV ] [ IP/hostname ] # ntpdate-u 192.168.0.2 # ntpdate-utime.NTP.org # ntpdate-untp.sjtu.edhu
1、服务器数量比较少,可以直接与时间服务器同步
使用ntpd调整时钟,而不是调整计算机时钟的时间。
当计算机刚启动且许多服务尚未启动时,可以使用ntpdate同步时间
另一方面,直接命令与时间服务器同步的示例:使用ntpdate同步: ntpdate -u x.x.x.x#这里的x.x.x.x是公共网络的时间服务器的IP地址,也是两种自制的ntp服务器
crontab- e */5 * * */usr/sbin/ntpdate-UX.x.x/etc/init.d/crond restart这里是几个卫星信号,用于发送和接收GPS和北斗
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
2、本地服务器较多,在本地自建时间同步服务器
# ntpdate-utime.pool.a liyun.com http://www.Sina.com /
0.centos.pool.NTP.org1. centos.pool.NTP.org2. centos.pool.NTP.org 2,自制ntpd时间同步服务器时间同步的两个命令:
首先,主机启动NTP。
客户端将调整时间的消息发送到NTP服务器。
然后,NTP服务器将当前标准时间发送到客户端
客户端接受来自服务器的时间后,会根据该信息调整自己的时间。 这样就实现了网络配对。
ntpd :
ntpdate :
因为NTP是分层结构,所以有顶级服务器、多层Relay Server和客户端。
因此,服务器可以从高到低设置为1-16
原则上应避免直接连接到一级服务以缓解负载和网络拥塞
1、构成ntp服务器1 ) # yum -y install ntp 2)开机自启动(systemctlstartntpd # systemctlenablentpd3)修改配置文件((详细信息) ) root @ llentpd 3 formoreinformationaboutthisfile,seethemanpages#NTP.conf(5),ntp_acc(5) 5,ntp_auth(5)5),NTP _ cloclococe NTP_mon(5).driftfile /var/lib/ntp/drift #系统时间和BIOS事件偏差日志配置本地同步的计算机权限# permittimesynchronizationwithourure
ce on this system.restrict default kod nomodify notrap nopeer noqueryrestrict -6 default kod nomodify notrap nopeer noquery默认拒绝任何操作,任何ip4地址、ip6地址 不能修改、不能trap远程登录、不能尝试对等、不能校对时间,
restrict 控制相关权限。
其中IP地址也可以是default ,default 就是指所有的IP
参数有以下几个:
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :拒絕沒有認證的用戶端
noquery :客户端不能使用ntpq,ntpc来查询ntp服务器,等于不提供校对时间服务
notrap :不提供trap远程登录功能,trap服务是一种远程时间日志服务
拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等
kod : 访问违规时发送 KoD 包,向不安全的访问者发送Kiss-Of-Death报文。
restrict -6 表示IPV6地址的权限设置。
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
一个简单实例如下:
restrict default kod nomodify notrap nopeer noquery#拒绝所有ip4 的请求# restrict -6 default kod nomodify notrap nopeer noquery #针对ipv6设置 # 允许本地所有操作restrict 127.0.0.1#restrict -6 ::1 # 允许的局域网络段或单独ip 某些权限restrict 10.0.0.0 mask 255.0.0.0 nomodify motraprestrict 192.168.0.0 mask 255.255.255.0 nomodify motraprestrict 192.168.1.123 mask 255.255.255.255 nomodify motrap # 使用上层的internet ntp服务器restrict cn.pool.ntp.orgrestrict 1.cn.poo.ntp.orgrestrict 0.asia.pool.ntp.orgrestrict 3.asia.pool.ntp.orgserver cn.pool.ntp.org preferserver 1.cn.poo.ntp.orgserver 0.asia.pool.ntp.orgserver 3.asia.pool.ntp.org # 如果无法与上层ntp server通信以本地时间为标准时间server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10driftfile /var/lib/ntp/drift# 计算本ntp server 与上层ntpserver的频率误差 # Key file containing the keys and key identifiers used when operating# with symmetric key cryptography.keys /etc/ntp/keyslogfile /var/log/ntp.log#日志文件配置/etc/sysconfig/ntpd文件
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
# 允许BIOS与系统时间同步,也可以通过hwclock -w 命令
启动ntpd 服务
# systemctl start ntpd查看ntp的端口,可以看到123端口
# netstat -unlnp查看ntp服务器有无和上层连通
# ntpstatsynchronised to NTP server (84.16.73.33) at stratum 2 time correct to within 184 ms polling server every 128 s刚启动的时候,一般是:
# ntpstat unsynchronised time server re-starting polling server every 64 s连接并同步后:
synchronised to NTP server (202.112.10.36) at stratum 3 time correct to within 275 ms polling server every 256 s ntptrace -n 127.0.0.1 127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610116.193.83.174: timed out, nothing received查看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 2、配置NTP客户端: 在所有客户端上vim /etc/ntp.conf,添加: server x.x.x.x #x.x.x.x 为上面配置的ntp服务器地址注意:当server与client之间的时间误差过大时(可能是1000秒),处于对修改时间可能对系统和应用带来不可预知的问题,NTP将停止时间同步!
所以如果发现NTP启动之后时间并不进行同步时,应该考虑到可能是时间差过大引起的,此时需要先手动进行时间同步 ntpdate !