设为首页 收藏本站
查看: 472|回复: 0

[经验分享] ntp redhat-Linux学习

[复制链接]
发表于 2018-5-10 06:12:57 | 显示全部楼层 |阅读模式
  http://yuebaibai222.blog.51cto.com/2535988/1317928
  先简单介绍几个名词
  Atomic Clock: 现在计算时间最准确的是使用 原子震荡周期 所计算的物理时钟(Atomic Clock),因此也被定义为标准时间(International Atomic Time)
  UTC(coordinated Universal Time): 协和标准时间 就是利用 Atomic Clock 为基准定义出来的正确时间
  硬件时钟: 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因
  系统时钟: 就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值
  搭建 NTP 服务之前的准备
  1.检查系统时区是否正确
  在中国,正确的时区应为 CST(Chinese Standard Time),也就是我们通常所说的北京时间.
  那么中国当地的时间晚上8点的话,我们可以有下面两种表示方式
  20:00 CST
  12:00 UTC
  因为中国处在UTC+8时区,依次类推,在UTC标准时间,就是12:00了.
  不管通过任何渠道我们想要同步系统的时间,通常提供方只会给出UTC+0的时间值而不会提供时区(因为它不知道你在哪里).所以当我们设置系统时间的时候,设置好时区是首先要做的工作
12# dateThu Oct 31 11:22:45 EDT 2013  如果发现时区不正确,修改方法见本文 附1
  2.检查 NTP 是否安装
123# rpm -qa |grep ^ntpntp-4.2.4p8-3.el6.centos.x86_64ntpdate-4.2.4p8-3.el6.centos.x86_64  如上所示就是正确安装了
  3.检查上层 NTP 服务器是否正常连通
  直接使用国家对时服务器
  1.cn.pool.ntp.org
  2.cn.pool.ntp.org
  为了防止出现上层对时服务器故障,无法正确提供时间同步,一般建议配置两个或以上时间同步服务器.下面分别测试两个时钟服务是否正常可用
123456789101112# ntpdate -q 1.cn.pool.ntp.orgserver 202.112.10.36, stratum 2, offset -1.883582, delay 0.18350server 202.112.29.82, stratum 2, offset -1.860654, delay 0.23692server 202.112.31.197, stratum 2, offset -1.812637, delay 0.13278server 218.75.4.130, stratum 2, offset -1.837221, delay 0.0409931 Oct 09:07:03 ntpdate[1625]: step time server 218.75.4.130 offset -1.837221 sec# ntpdate -q 2.cn.pool.ntp.orgserver 202.112.10.36, stratum 2, offset -1.834295, delay 0.07317server 202.112.29.82, stratum 2, offset -1.810122, delay 0.12784server 202.112.31.197, stratum 2, offset -1.838527, delay 0.07227server 218.75.4.130, stratum 2, offset -1.833332, delay 0.0434631 Oct 09:12:36 ntpdate[22558]: step time server 218.75.4.130 offset -1.833332 sec  如上所示,就是正常的,如果上层服务器不可用,一般如下显示
12server 10.32.196.60, stratum 0, offset 0.000000, delay 0.0000026 Oct 15:01:17 ntpdate[23338]: no server suitable for synchronization found  4.手工校正当前时间
  当系统时间与标准时间相差太大时,NTP 通常无法正常同步时间,因此我们一般要手工先对当前时间进行调整
  两个对时服务器都可以用,随便挑一个就行
12# ntpdate 2.cn.pool.ntp.org31 Oct 09:14:18 ntpdate[22563]: step time server 218.75.4.130 offset -1.830097 sec  调整好后,用 date 命令检查一下,时间是否正确了
12# dateThu Oct 31 09:14:50 CST 2013  如果时间还是不对,建议用 date 命令手工调整一下,我在实验过程中没有遇到过,但有的网友说会说现这种情况
  设置 NTP 的配置文件
  NTP 服务器说白了,就是起一个承上启下的作用,寻找上层服务器获取正确的时间,同时为下层服务器提供网络对时服务,所以对它的设置,也主要由两方面组成
  1.指定上层对时服务器
  加#号注释掉centos默认的对时服务器,如果主机可以访问外网,这一步最好做一下.
  添加我们刚才测试的国家对时服务器.
  prefer 表示优先使用
12345678# vim /etc/ntp.conf# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).#server 0.centos.pool.ntp.org#server 1.centos.pool.ntp.org#server 2.centos.pool.ntp.orgserver 1.cn.pool.ntp.orgserver 2.cn.pool.ntp.org prefer  2.设置对内网主机提供NTP服务
  在我的实验环境中,内网使用 192.168.80.0/24 网段
12345678910# 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 noqueryrestrict -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 127.0.0.1restrict -6 ::1restrict 192.168.80.0 mask 255.255.255.0 nomodify notrap  几点说明:
  restrict default kod nomodify notrap nopeer noquery
  kod kod技术可以阻止 "Kiss of Death"包(一种DOS攻击)对服务器的破坏
  nomodify 用户端不能更改NTP服务端的时间参数,但可以通过NTP服务端进行时间校对
  notrap 不提供trap 远程事件登录功能
  nopeer 不与其它同一层的NTP服务器进行时间同步
  noquery 不提供NTP服务
  restrict 还可以使用的其它参数
  ignore 关闭所有NTP服务
  notrust 拒绝没有通过认证的客户端
  restrict 127.0.0.1默认对本机无限制
  restrict 192.168.80.0 mask 255.255.255.0 nomodify notrap这是我添加的,允许192.168.80.0/24 网段的主机来进行时间校对,但不允许客户端来修改,登录我的NTP服务器
  3.设置同步更新本地hwclock
  在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了那么既然两个时钟独自运行,那么时间久了必然就会产生误差了,而NTP默认又只更新系统时间,因此我们需要设置硬件时钟进行同步调整
1234# vim /etc/sysconfig/ntpd# Drop root to>OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"SYNC_HWCLOCK="yes"  添加SYNC_HWCLOCK="yes"
  启动NTP服务
12# service ntpd startStarting ntpd:                                             [  OK  ]  启动后的观察
  1.确认端口监听启动
  ntpd启动 123 端口 通过UDP协议对外提供服务
  
1234567# netstat -nlpu |grep ntpdudp        0      0 192.168.80.3:123            0.0.0.0:*                               22816/ntpd   udp        0      0 127.0.0.1:123               0.0.0.0:*                               22816/ntpd   udp        0      0 0.0.0.0:123                 0.0.0.0:*                               22816/ntpd   udp        0      0 fe80::20c:29ff:fed4:315f:123 :::*                                    22816/ntpd   udp        0      0 ::1:123                     :::*                                    22816/ntpd   udp        0      0 :::123                      :::*                                    22816/ntpd  2.查看系统日志,确定启动过程无报错
123456789101112# tail -n 20 -f /var/log/messagesOct 31 10:56:29 CentOS1 ntpd[22815]: ntpd 4.2.4p8@1.1612-o Fri Feb 22 11:23:27 UTC 2013 (1)Oct 31 10:56:29 CentOS1 ntpd[22816]: precision = 0.052 usecOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #0 wildcard, 0.0.0.0#123 DisabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #1 wildcard, ::#123 DisabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #2 lo, ::1#123 EnabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #3 eth0, fe80::20c:29ff:fed4:315f#123 EnabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #4 lo, 127.0.0.1#123 EnabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #5 eth0, 192.168.80.3#123 EnabledOct 31 10:56:29 CentOS1 ntpd[22816]: Listening on routing socket on fd #22 for interface updatesOct 31 10:56:29 CentOS1 ntpd[22816]: kernel time sync status 2040Oct 31 10:56:29 CentOS1 ntpd[22816]: frequency initialized 23.140 PPM from /var/lib/ntp/drift  3.确认已进行时间同步
  该命令执行,通常要等10到15分钟,才会显示同步成功
  
1234567# ntpstatsynchronised to NTP server (202.112.31.197) at stratum 3   time correct to within 80 ms   polling server every 128 s#进行时间校对的NTP服务器#本地主机与上层NTP服务器的时间差#下次同步时间  
  4.查看与上层服务器连接情况
12345# ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================+dns1.synet.edu. 202.118.1.46     2 u   13   64   77  567.019  132.808  32.883*dns2.synet.edu. 202.118.1.46     2 u   58   64   37  209.322  -47.016  36.082  remote 本地主机所连接的上层NTP服务器
  *目前正在使用的上层NTP
  +已连线,可提供时间更新的候补服务器
  refid 给上层NTP服务器提供时间校对的服务器
  st 就是stratum 上层NTP的级别
  when 几秒钟前曾做过时间同步更新
  poll 下一次更新在几秒后
  reach 已经向上层服务器要求更新的次数
  delay 网络传输过程中的延迟时间
  offset 本地主机与上层NTP服务器的时间差,该值的绝对值越接近0,与上层服务器的时间就越接近
  jitter 一个统计值,这个值的绝对值越小,本地主机的时间就越精确
  5.确认无问题后,将服务设为开机启动
12345# chkconfig --list ntpdntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off# chkconfig ntpd on# chkconfig --list ntpdntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off  客户端设置
  客户端到时钟服务器同步时间,有两种常见的用法:
  1.如果主机比较少,可以通过 cron 来同步时间
12# vim /etc/crontab*/3 * * * * root (/usr/sbin/ntpdate 192.168.80.3 && /sbin/hwclock -w) & >/dev/null  为了尽快看到执行效果,所以设置为每三分钟同步一次,实际生产中不用这么频繁,每天同步一次就差不多了
123# tail -f /var/log/cronOct 31 11:37:01 CentOS2 crond[1522]: (*system*)>/etc/crontab)Oct 31 11:39:01 CentOS2 CROND[1688]: (root) CMD ((/usr/sbin/ntpdate 192.168.80.3 && /sbin/hwclock -w) & >/dev/null)  2.启动NTP服务自动同步
  如果内网服务器比较多
  可以通过架设NTP来实现时间同步
  因为该NTP只需向上层来同步时间,而不需对下层提供对时服务,就比较简单了
  只要重复上面NTP架设步骤,去除restrict 步骤即可
  附1:
  1.查看当前时区
12# dateThu Oct 31 09:37:09 EDT 2013  2.修改 /etc/sysconfig/clock
  将原来的时区改为
1234# vi /etc/sysconfig/clockZONE="America/New_York"#改为ZONE="Asia/Shanghai"  3.覆盖 /etc/localtime
12# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtimecp: overwrite `/etc/localtime'? y  4.查看修改后的时区
12# dateThu Oct 31 21:42:33 CST 2013

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-457764-1-1.html 上篇帖子: redhat 6.5 配置WAS控制台中文 下篇帖子: Redhat7.2下编译rpm包的形式安装openvswitch
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表