1、NTP简介

NTP(Network Time Protocol,网络时间协议)用来使计算机时间同步的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒)。对于服务器群集来说,这个是一个很重要的服务,因为群集需要保证每个服务器的时间是保持一致的,这样它们在执行同一个任务时才不会出现有的服务器有滞后的现象,这样群集的状态才是健康的。

我们在平时工作中可能会遇到内网环境中服务器因为不能联网而出现与正常时间不一致的情况,这时候可以采用搭建ntp时间同步服务器,让内网服务器以客户端的方式连接时间同步服务器,当然时间同步服务器要能连接外网,进行时间同步。

2、NTP原理

NTP的授时方式是Client—Server方式,客户端首先向服务端发送一个NTP 包,其中包含了该包离开客户端的时间戳T1,当服务端接收到该包时,依次填入包到达的时间戳T2、包离开的时间戳T3,然后立即把包返回给客户端。客户端在接收到响应包时,记录包返回的时间戳T4。从而计算出时间进行同步。

3、NTP服务端搭建

服务器规划:

192.168.149.20(能连接外网) 服务端
192.168.149.21 (不能连接外网) 客户端

(一)、NTP服务器端(192.168.149.20)安装

可以看到已经安装了ntp,大部分服务器都自带ntp服务

如果没有可以直接 yum -y install ntp进行安装

[root@node1 ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64

(二)、配置NTP服务器端配置文件

下方是已经配好的,可以对照着修改

[root@node1 ~] vim /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 nomodify
# nomodify允许局域网客户端同步 # 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.1
restrict ::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 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#将以上默认时间源都注释 #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 # Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor server ntp1.aliyun.com
server time.nist.gov
#在最后加入以上两行时间源

(三)、启动NTP服务

查看计划任务有无时间同步,有的话注释掉,否则启动ntp会报错

[root@node1 ~]# crontab -e    

启动并查看启动状态

[root@node1 ~]# systemctl start ntpd
[root@node1 ~]# systemctl status ntpd

如下图所示代表启动成功

通过ntpq -p和ntpstat命令查看是否有同步状态

[root@node1 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+120.25.115.20 10.137.53.7 2 u 74 64 176 44.800 11.154 4.838
*time-a-b.nist.g .NIST. 1 u 8 64 377 199.241 -5.427 3.776 [root@node1 ~]# ntpstat
synchronised to NTP server (132.163.96.1) at stratum 2
time correct to within 176 ms
polling server every 64 s

4、ntp客户端同步

连接服务端进行时间同步(192.168.149.21)

查看是否有ntpdate

[root@node2 ~]# rpm -qa | grep ntpdate
ntpdate-4.2.6p5-29.el7.centos.2.x86_64

要等10分钟再与ntp服务器进行时间同步,否则会报以上错误,如果仍然报错请检查防火墙配置

[root@node2 ~]# ntpdate 192.168.149.20
22 Apr 11:06:29 ntpdate[1806]: no server suitable for synchronization found

客户端连接成功

[root@node2 ~]# ntpdate 192.168.149.20
22 Apr 11:09:22 ntpdate[1854]: adjust time server 192.168.149.20 offset -0.017823 sec

linux搭建ntp时间同步服务的更多相关文章

  1. 关于linux下ntp时间同步服务的安装与配置

    1.安装ntp服务,要使用时间同步.那么服务端与客户端都需要使用如下命令安装NTP软件包 [root@ ~]# yum install ntp -y 2.如果只是作为客户端的话,配置则可以非常简单,编 ...

  2. LINUX之ntp时间同步服务配置

    本篇将介绍LINUX之ntp服务配置,时钟同步服务器配置.这个在很多地方都会用到,保持各主机之前的时间保持一致,保证主机之间的心跳稳定. 三台主机都是centos7 192.168.1.110 mas ...

  3. Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决

    [root@EPDDB log]# [root@EPDDB log]# ntpdate 10.154.8.200 6 Sep 09:35:09 ntpdate[30210]: the NTP sock ...

  4. centos7搭建ntp时间同步服务器chrony服务

    centos7搭建ntp时间同步服务器chrony服务 前言: 在centos6的时候我们基本使用的是ntp服务用来做时间同步,但是在centos7后推荐是chrony作为时间同步器的服务端使用, ...

  5. NTP时间同步 服务端 客户端 自动化安装配置

    NTP时间同步 服务端 客户端 自动化安装配置 原创内容 http://www.cnblogs.com/elvi/p/7657994.html #!/bin/sh #运行环境 centos6.cent ...

  6. openstack (1)----- NTP 时间同步服务

    一.标准时间 1.地球分为东西十二个区域,共计24个时区 2.格林威治作为全球标准时间即(GMT时间),东时区以格林威治时区进行加,而西时区则进行减 3.地球的轨道并非正圆,在加上自传速度逐年递减,因 ...

  7. NTP时间同步服务

    NTP时间服务器 作用:ntp主要是用于对计算机的时间同步管理操作. 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器时间来记录相关信息,如果时间不准,则可能造成很大的影响. 部署安装NTP ...

  8. Linux集群配置离线ntp时间同步服务

    集群中时间不同步有可能会让大数据的应用程序运行混乱,造成不可预知的问题,比如Hbase.mongodb副本集等,Hbase当时间差别过大时就会挂掉,mongodb如果副本时间过快,会出现时间栈帧溢出提 ...

  9. Linux NTP时间同步服务

    NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网 ...

随机推荐

  1. Molecule实现数栈至简前端开发新体验

    Keep It Simple, Stupid. 这是开发人耳熟能详的 KISS 原则,也像是一句有调侃意味的善意提醒,提醒每个前端人,简洁易懂的用户体验和删繁就简的搭建逻辑就是前端开发的至简大道. 这 ...

  2. [源码解析] TensorFlow 分布式环境(6) --- Master 动态逻辑

    [源码解析] TensorFlow 分布式环境(6) --- Master 动态逻辑 目录 [源码解析] TensorFlow 分布式环境(6) --- Master 动态逻辑 1. GrpcSess ...

  3. Where和having都是条件筛选关键字,它们有什么分别?

    WHERE是在数据分组前进行条件过滤, HAVING子句是在数据分组后进行条件过滤,WHERE子句中不能使用聚合函数,HAVING子句可以使用聚合函数. 需要注意说明:当同时含有where子句.gro ...

  4. 重载(Overload)和重写(Override)的区别。重载的 方法能否根据返回类型进行区分?

    方法的重载和重写都是实现多态的方式,区别在于前者实现的是编译时的多态性,而后者实现的是运行时的多态性.重载发生在一个类中,同名的方法如果有不同的参数列表(参数类型不同.参数个数不同或者二者都不同)则视 ...

  5. spring-boot--lernning之自定义starters

    思路: 1这个场景需要使用到的依赖是什么??? 2如何编写自动配置 @Configuration 指定这个类是一个配置类 @ConditionalOnXXXX 指定条件下成立的情况下自动配置类生效 @ ...

  6. 处理器映射器(HandlerMapping)及处理器适配器(HandlerAdapter)详解(二)

    注解的 处理器映射器 和 处理器适配器 介绍 注解的映射器: 在 Spring3.1 之前使用 DefaultAnnotationHandlerMapping 注解映射器(根据 DispatcherS ...

  7. spring aware 的个人理解

    今天学习到了spring aware ,特地百度了下这方面的知识,现在谈下我的理解. Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的.即你可以将你的容器替换成 ...

  8. Tcp三次握手四次挥手个人学习

    最近想跳槽,学习了tcp中的三次握手与四次挥手,特意记录下,加深记忆 SYN 代表请求创建连接 FIN 表示请求关闭连接 ACK 代表确认接受,不管是三次握手还是四次分手,在回应的时候都会加上ACK= ...

  9. 错误问题之“Apache Log4j 漏洞,在版本为包含2.14以内!”

    漏洞概述 Apache Log4j是一个用于Java的日志记录库,其支持启动远程日志服务器. Log4j 1.2 中包含一个 SocketServer 类,该类容易受到不可信数据反序列化的影响,当侦听 ...

  10. 用jq实现移动端滑动轮播以及定时轮播效果

    Html的代码: <div class="carousel_img"> <div class="car_img" style="ba ...