NTP时间同步服务


纸上得来终觉浅,绝知此事要躬行。

NTP时间同步服务


1. 时间时区概念

  • UTC
    • 整个地球分为二十四时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时。
  • GMT
    • 格林威治标准时间指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线(UTCGMT时间基本相同)。
  • CST
    • 中国标准时间
    • CST = UTC+8 = GMT+8
  • DST
    • 夏令时指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用,中国不使用。
# 查看当前服务器时区
timedatectl

# 列出时区并设置时区
timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai

2. ntpd 与 ntpdate

ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对。而ntpdate不会考虑其他程序是否会阵痛,直接调整时间。一个是校准时间,一个是调整时间。

因为许多应用程序依赖连续的时钟,而使用ntpdate这样的时钟跃变,有时候会导致很严重的问题,如数据库事务操作等。

不够安全:ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。

不够精确:一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

不够优雅:由于ntpdate是急变,而不是使时间变快或变慢,依赖时序的程序会出错。例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的。理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。


3. 部署 NTP 服务

使用NTP公共时间服务器池同步你的服务器时间,部署完成之后,这样集群会自动定期进行服务的同步,如此以来集群的时间就保持一致了。

  • (1) 服务软件的安装
# 查看是否安装
rpm -q ntp

# 如果没有安装过的话,可以执行此命令安装
yum install ntpdate ntp -y
  • (2) 服务的基本配置(/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

# 新增:日志目录
logfile /var/log/ntpd.log

# 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.
# 还要确保localhost足够权限,这个常用的IP地址用来指Linux服务器本身
restrict 127.0.0.1
restrict ::1
# 这一行的含义是授权172.16.128.0网段上的所有机器可以从这台机器上查询和同步时间
restrict 172.16.128.0 mask 255.255.255.0 nomodify notrap

# 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).

# 删减:注释掉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

# 新增:时间服务器列表
# 指定我们需要同步的时间服务器地址,可以有多个
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

# 新增:当外部时间不可用时可以使用本地时间
server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10

# 新增:允许上层时间服务器主动修改本机时间
# 其中restrict语句控制允许哪些网络查询和同步时间
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
......
  • (3) 设置系统开机自启动
# 默认为CentOS7的配置,CentOS6中需要使用chkconfig命令
systemctl enable ntpd
systemctl enable ntpdate
systemctl is-enabled ntpd
# 在ntpd服务启动时,先使用ntpdate命令同步时间,确保没什么问题
ntpdate -u 1.cn.pool.ntp.org

# 启动NTP服务器
# 默认为CentOS7的配置,CentOS6中需要使用service命令
systemctl start ntpdate
systemctl start ntpd
  • (4) 加入防火墙
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload
  • (5) 将正确时间写入硬件
ss -tlunp | grep ntp
ntpq -p
hwclock -w
  • (6) 客户端使用配置
# (1) 以服务进程方式实时同步
# 编辑客户端的配置文件(/etc/ntp.conf),添加如下内容
server 172.16.128.171
# (2) 重启服务
# 修改任意节点服务器的NTP配置文件都需要重起ntpd服务
systemctl restart ntpd
# (3) 设置定时任务进行时间校对
# 需安装ntpdate,每天24点更新同步时间
crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171; hwclock -w
  • (7) 查看 ntp 同步状态
# 使用如下命令查看节点同步状态
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 218.189.210.3   118.143.17.82    2 u    7   64    1  101.974  -33.967   0.000
 209.58.185.100  .INIT.          16 u    -   64    0    0.000    0.000   0.000
 103-226-213-30- .INIT.          16 u    -   64    0    0.000    0.000   0.000
remote:即NTP主机的IP或主机名称。
注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,
如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。

refid:参考的上一层NTP主机的地址
st:即stratum阶层
when:几秒前曾做过时间同步更新的操作
poll:下次更新在几秒之后
reach:已经向上层NTP服务器要求更新的次数
delay:网络传输过程钟延迟的时间
offset:时间补偿的结果
jitter:Linux系统时间与BIOS硬件时间的差异时间
# 查询你的ntp服务器同步信息
[root@localhost ~]# ntpdate -q  0.hk.pool.ntp.org
server 203.95.213.129, stratum 2, offset -0.020632, delay 0.06477
server 209.58.185.100, stratum 2, offset -0.011884, delay 0.06216
server 218.189.210.4, stratum 0, offset 0.000000, delay 0.00000
server 218.189.210.3, stratum 2, offset -0.036728, delay 0.11096
 6 Apr 12:51:43 ntpdate[10190]: adjust time server 209.58.185.100 offset -0.011884 sec

文章作者: Escape
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Escape !
  目录