原理

NTP(Network TimeProtocol,网络时间协议)是用来使计算机时间同步的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),切可介由加密确认的方式来防止恶意的协议攻击。

运行模式:C/S模式

运行端口/协议:123/udp

安装

直接使用 yum 安装:

yum install ntp ntpdate

配置

配置文件 /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 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.1
#restrict 192.168.137.1 mask 255.255.255.0 nomodify #允许更新的IP地址段
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).
# perfer:表示优先级最高,如果不设置,从上到下,写的越靠上,优先级越高
# iburst:当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测
server cn.pool.ntp.org iburst perfer # 中国国家授时中心ntp服务器
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 # local clock 如果上面的服务器都无法同步时间,就和本地系统时间同步。127.127.1.0在这里是一个IP地址,不是网段。
# 127.127.1.0 为第10层。ntp 和127.127.1.0同步完后,就变成了11层。 ntp是层次阶级的。同步上层服务器的stratum 大小不能超过或等于16。
# 在实际搭建中如果设置了local clock会出现即使ntp服务器本身时间不准确也不会和上层ntp服务器同步的问题,故注释。
# 具体原因待研究
#server 127.127.1.0
#fudge 127.127.1.0 stratum 8 #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

启动

centos6

启动:service ntpd start

停止:service ntpd stop

设置开机启动:chkconfig ntpd on

查看启动状态:service ntpd status

查看日志: tailf /var/log/message

查看端口:netstat -ulnp | grep -w 123

centos7

启动:systemctl start ntpd

停止:systemctl stop ntpd

设置开机启动:systemctl enable ntpd

查看启动状态:systemctl status ntpd

查看日志: tailf /var/log/message

查看端口:netstat -ulnp | grep -w 123

注意: ntpd 有一个自我保护设置:如果本机与上源时间相差太大,ntpd 不运行。所以新设置的时间服务器一定要先ntpdate 从上源取得时间初值,然后再启动 ntpd 服务。

查看同步状态

使用命令 ntpq -p 查看同步状态,ntpstat 列出是否与上游服务器连接。

[root@centos751 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp7.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000
*106.39.20.237 108.196.98.101 2 u 202 64 10 48.325 -4.794 0.997
ntp1.ams1.nl.le 130.133.1.10 2 u 82 64 2 440.047 -111.75 0.000
ntp2.flashdance 192.36.143.130 2 u 7 64 7 268.049 -2.210 45.428
static-5-103-13 .INIT. 16 u - 64 0 0.000 0.000 0.000
[root@centos751 ~]#
  • remote:即remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先。
  • refid:参考的上一层NTP主机的地址
  • st:即stratum阶层
  • poll:下次更新在几秒之后
  • offset:时间补偿的结果
[root@centos751 ~]# ntpstat
synchronised to NTP server (106.39.20.237) at stratum 3 # 可以看到我们当前在3层
time correct to within 350 ms
polling server every 64 s
[root@centos751 ~]#

客户端同步时间

使用 ntpdate 同步

[root@centos752 ~]# ntpdate 192.168.223.101
22 Oct 17:19:08 ntpdate[29924]: adjust time server 192.168.223.101 offset -0.001306 sec

这种方法同步一次后如果时间久了,可能时间又会不准确,所以一般是使用 cron 任务定时同步

*/10 * * * * /usr/sbin/ntpdate 192.168.223.101 | logger -t NTP

拓展补充

1、linux系统时间和BIOS时间

有时候linux系统时间和BIOS时间是不一样的,如果不一样怎么解决呢?方法如下:

a、首先 hwclock -r 读出BIOS的时间参数,查看是否准确

b、如果不准确,使用 hwclock -w 将当前系统时间(已同步准确)写入BIOS中

2、不同机器之间的时间同步

为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步的工作是非常必要的。 同步时间,可以使用ntpdate命令,也可以使用ntp服务。主要的方法有以下3种。

a、使用 ntpdate 配合 cron 同步公网NTP服务器时间

*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org | logger -t NTP

b、将主机搭建成 ntp 服务器同步上层NTP服务器时间

以上两种方法有弊端:必须主机可以连接公网,但是有的时候集群当中有部分主机是不能连接公网的,这时候就要用到第三种的方法

c、将内网中能连接公网的主机搭建成 ntp 服务器同步公网上层NTP服务器时间,然后内网中其他主机使用 ntpdate 定时同步此NTP服务器时间或者搭建成 ntp 服务器并将上层NTP服务器设置为次NTP服务器

centos6 & centos7搭建ntp服务器的更多相关文章

  1. CentOS7搭建NTP服务器及客户端同步时间

    一.服务器配置 1.查看服务器.客户端操作系统版本 [root@hadoop101 ~]# cat /etc/redhat-release CentOS Linux release (Core) 2. ...

  2. linux篇-CentOS7搭建NTP服务器

    1查看服务器.客户端操作系统版本 2查看服务器是否安装ntp 3如果没有安装 4安装完成后重新查看服务器是否安装ntp 5查看ntp服务器状态 6修改配置文件 注释 #server 0.centos. ...

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

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

  4. Centos7安装搭建NTP服务器和NTP客户端同步时间

    NTP简介: NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 在计算机的世界里,时间非常地重要 例如:对于火箭发射这种科研活动,对时间的 ...

  5. CentOS7 搭建 SVN 服务器

    CentOS7 搭建 SVN 服务器 介绍SVN: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上 ...

  6. centos7 搭建WEB服务器

    centos7 搭建WEB服务器 2017年09月17日 09:44:50 逝然1994 阅读数:18321 标签: centosapacheweb服务器 更多 个人分类: centos服务器简单配置 ...

  7. centos7搭建svn服务器及客户端设置

    centos7搭建svn服务器及客户端设置 centos7貌似预装了svn服务(有待确认),因此我们直接启动该服务即可 一.svn服务端配置(服务器IP假设为192.168.100.1) 步骤1:创建 ...

  8. Ubuntu搭建NTP服务器

    NTP简介 NTP是Network Time Protocol的缩写,又称为网络时间协议.是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提 ...

  9. centos7 搭建DHCP服务器

    一.DHCP简单讲解 DHCP就是动态主机配置协议(Dynamic Host Configuration Protocol)是一种基于UDP协议且仅限用于局域网的网络协议,它的目的就是为了减轻TCP/ ...

随机推荐

  1. 赋能时空云计算,阿里云数据库时空引擎Ganos上线

    随着移动互联网.位置感知技术.对地观测技术的快速发展,时空信息已从传统GIS行业渗透到大众应用及各行各业.从静态POI(兴趣点)到APP位置信息,从导航电子地图到车辆行驶轨迹,从卫星影像到三维城市建模 ...

  2. noip历年试题

      noip2018 铺设道路 货币系统 赛道修建 一眼贪心.随便实现. 旅行 环套树枚举删除环上哪条边. 填数游戏 找规律,这谁会啊. 保卫王国 动态Dp,去问这位神仙.   noip2017 小凯 ...

  3. delphi INI文件

    INI 文件读写 filecreate('路径加文件名')://创建一个文件. (1) INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个 ...

  4. API应用实例

    API声明透明 {API声明} type TSetLayeredWindowAttributes = function(wnd: HWND; crKey: DWORD; bAlpha: BYTE; d ...

  5. 学习android文档 -- Adding the Action Bar

    1. Setting Up the Action Bar:users-sdk version 11以上可以使用holo主题:如果不使用holo主题,或者sdk版本较低,则需要在manifest文件的& ...

  6. host文件是作用

    什么是HOST文件:Hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...

  7. [SCOI2009]迷路(矩阵快速幂) 题解

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  8. 51nod-1515 明辨是非——并查集

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

  9. 工具类--MD5Utils

    public class MD5Utils { private static final String[] HEX_DIGITS = { "0", "1", & ...

  10. 牛客 最大值减去最小值小于或等于 num 的子数组数量

    题目链接:https://www.nowcoder.com/practice/5fe02eb175974e18b9a546812a17428e?tpId=101&tqId=33086& ...