西维蜀黍

🐒 Software engineer | 📷Photographer | 👹Urban explorer

  • 主页
  • 所有文章
Tag Friends

西维蜀黍

🐒 Software engineer | 📷Photographer | 👹Urban explorer

  • 主页
  • 所有文章

【Linux】时间同步问题与Linux NTP

2017-04-25

零.相关知识

1.时间与时区

如果有人问你说现在几点? *你看了看表回答他说晚上8点了。这样回答看上去没有什么问题,但是如果问你的这个人在欧洲的话那么你的回答就会让他很疑惑,因为他那里还太阳当空呢。
*
这里就有产生了一个如何定义时间的问题
。为在地球环绕太阳旋转的24个小时中,世界各地日出日落的时间是不一样的.所以我们才有划分时区(timezone) 的必要,也就是把全球划分成24个不同的时区。所以我们可以把时间的定义理解为一个时间的值加上所在地的时区(注意这个所在地可以精确到城市)。

那么假如现在中国当地的时间是晚上8点的话。我们可以有下面两种表示方式:

  • 20:00 CST
  • 12:00 UTC

这里的 CST 是Chinese Standard Time,即北京时间。UTC 是Coordinated Universal Time,即协调世界时。

因为中国处在UTC+8时区,依次类推那么也就是12:00 UTC了。

2.硬件时间时钟 与 系统时钟

在我们的计算机上,存在两个时钟:硬件时间时钟(RTC),和系统时钟(System Clock)。

  • 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因。
  • 系统时钟就是操作系统内核计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的。

在Linux下,系统时间在开机的时候会和硬件时间同步(synchronization)。然而在这次同步之后,这两个时钟也就各自独立运行了(硬件时间依靠BIOS电池来维持,而系统时间依靠CPU tick来维持)。

查看系统时间:

1
date

修改系统时间:

1
date -set  "2017-05-01 00:01"

查看当前硬件时钟对应的时间:

1
hwclock --show


我们可以看到,硬件时间和系统时间是会存在一定误差的,因此我们可以把他们同步。

将硬件时间设置成当前系统时间:

1
hwclock --hctosys

将当前系统时间设置成硬件时间:

1
hwclock --systohc

但是,问题在于【如果这两个时间都不准】怎么办呢?这时候就要在互联网上找一个可以提供准确时间的服务器,并通过一种协议将我们的系统时间,与服务器的时间进行同步,这就是NTP同步,这个协议就是NTP(Network Time Protocol)。

二.NTP Server搭建

我们的计算机运行一定时间后就会产生时间误差,真正能够精确地测算时间的还是原子钟。但是,由于原子钟十分的昂贵,只有少部分组织拥有。他们连接到计算机之后就成了一台 NTP Server。

NTP Server是用于给其他计算机进行时间同步的服务器。

在服务器集群中,通常会搭建一台NTP Server,以向集群中的其他服务器提供时间同步服务。

事实上,这台NTP Server也会向权威NTP Server进行时间同步,通常会选择距离集群中这台NTP Server最近的权威NTP Server来进行同步。

权威ntp服务器列表:http://www.pool.ntp.org

下面我们来搭建集群中的这台NTP Server:

1.查看NTP是否已经安装

1
rpm -qa | grep ntp

出现ntp-...,说明已经安装了,我的本机已经安装了。如果没有安装,可用如下方式:

1
yum -y install ntp

2.ntpd 配置

(1)修改 ntpd 配置

我们在NTP的官方网站(http://www.pool.ntp.org)上找到距离我们服务器最近的权威 NTP Server。

以中国为例:

  • 0.cn.pool.ntp.org
  • 1.cn.pool.ntp.org
  • 2.cn.pool.ntp.org
  • 3.cn.pool.ntp.org

我们在配置文件中修改依赖的权威 NTP Server:

1
vi /etc/ntp.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# server用来设置依赖的权威 NTP Server
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst

# restrict用来设置哪些主机(客户端)可以访问NTP Server
# 格式:restrict [address] mask [netmask_ip] [parameter]
#  paramter的选项有:
#   ignore :关闭所有的 NTP 联机服务
#   nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,Client 端仍然可以透过 Server 端来进行网络校时。
#   notrust:该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
#   noquery:不提供 Client 端的时间查询
#   notrap:不提供trap这个远程事件登入
#  如果paramter完全没有设定,那就表示该 IP (或网域)“没有任何限制”

# 允许上层时间服务器主动修改本机时间
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery

# 允许本机查询
restrict 127.0.0.1

# 在192.168.0.1/24网段内的服务器就可以通过这台NTP Server进行时间同步了
restrict 192.168.0.1 mask 255.255.255.0 nomodify

修改完成后先手动同步一次时间:

1
ntpdate 0.cn.pool.ntp.org

启动ntpd服务:

1
systemctl start ntpd

确认该NTP Server是否已经向权威NTP Server同步时间:

1
ntpstat

如果是这样,说明已经成功向权威NTP Server同步了,且每1分钟触发一次同步:

(2)设置 ntpd

将 ntpd 设置为开机启动:

1
systemctl enable ntpd

重启后,检查ntpd是否会自动开机启动:

1
systemctl status ntpd

从下面的图中,看到ntpd已经被设置成自动开机启动了,但是事实上,在开机后,启动并未成功。

引起这个问题常见的原因有可能是系统上安装了一个与 ntpd 相冲突的工具:chrony。

检查 chronyd 是否被设置为开机自动启动:

1
systemctl status chronyd

下图可以看到,chronyd 确实被设置为开机自动启动了:

禁止 chronyd 开机启动:

1
systemctl disable chronyd

三.NTP client设置

类似地,首先查看NTP是否已经安装:

1
rpm -qa | grep ntp

如果没有安装,用如下方式:

1
yum -y install ntp

在客户端 NTP的配置文件中,加上我的这台NTP Server:

1
vi /etc/ntp.conf

增加下面几条设置:

1
2
3
4
5
# 允许我们的NTP Server 做为本地的时间服务器
server 10.138.11.122 iburst

# 允许上层时间服务器主动修改本机时间
restrict 10.138.11.122 nomodify notrap noquery

如果客户端与NTP Server的时间差异太大,可能会出现客户端同步失败的情况。因此,先在客户端手动同步一把。

手动同步时间:

1
ntpdate 服务器IP

注意,每次重启NTP Server之后大约要3-5分钟,客户端主机才能与 Server 建立正常的通讯连接。

四.验证NTP Server的服务

NTP Server上查看端口是否存在:

1
netstat -tulnp | grep ntp

NTP Server上查看现有连接客户端:

1
watch ntpq -p

client 上查看同步的结果:

1
ntpstat
1
2
3
synchronised to local net at stratum 11
time correct to within 12 ms
polling server every 512 s

参考

  • Linux NTP配置详解 (Network Time Protocol)
  • Linux服务管理之NTP服务器配置
  • Linux
  • Linux

扫一扫,分享到微信

【iOS】 Apple移动设备处理器指令集与Xcode指令集相关设置
【Linux】CentOS7/RedHat7 NTP服务无法开机自启动
© 2020 西维蜀黍
  • Tag
  • Friends

tag:

  • Algorithm Problem
  • AWS
  • Algorithm
  • Architectural Pattern
  • Architecture
  • ArchitectureDesign
  • Nginx
  • Frontend
  • Cache
  • Browser
  • C#
  • Debug
  • Visual Studio
  • Cache System
  • Compile
  • Data Structure
  • JavaScript
  • Data Format
  • Database
  • Design Pattern
  • Distributed System
  • Microservices
  • Django
  • Redis
  • Docker
  • ELK
  • Format
  • Git
  • Version Control
  • Golang
  • HTTP
  • Network
  • Hardware
  • Interview
  • JQuery
  • Java EE
  • Software Testing
  • Java
  • Network Programming
  • LaTeX
  • Linux
  • Operating System
  • Linxu
  • Lock
  • macOS
  • Markdown
  • Lucene
  • Mattermost
  • MySQL
  • Netwok
  • Netwrok
  • Node.js
  • nvm
  • NPM
  • npm
  • OOP
  • OpenWrt
  • Operating Systems
  • Performancede
  • Performance
  • Programming
  • Protobuf
  • Python
  • RaspbeeryPi
  • Codis
  • SQL
  • Regular Expression
  • Security
  • Spring
  • TypeScript
  • VMware
  • Vmware
  • Windows
  • WordPress
  • VPN
  • hexo
  • ZooKeeper
  • iOS
  • hugo

    缺失模块。
    1、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    2、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: true
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • My English Blog
  • My OJ Blog
  • 西维蜀黍的健身 Blog