Linux NTP时间同步服务


一、时间时区概念

  • UTC
    整个地球分为二十四时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时。

  • GMT
    格林威治标准时间指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线(UTC与GMT时间基本相同)。

  • CST
    中国标准时间

CST = UTC+8 = GMT+8
  • DST
    夏令时指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用,中国不使用。
# 查看当前服务器时区
timedatectl

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

二、ntpd 与 ntpdate

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

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

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

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

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

三、部署 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
......

阿里的ntp时钟源

ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
3 设置系统开机自启动
# 默认为CentOS7的配置,CentOS6中需要使用chkconfig命令
systemctl enable ntpd
systemctl enable ntpdate
systemctl is-enabled 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硬件时间的差异时间

其中,when和reach是观察时间是否同步上的标志字段。

查询你的ntp服务器同步信息

[root@Server1 ~]# ntpdate -q ntp.aliyun.com
server 203.107.6.88, stratum 2, offset 0.716122, delay 0.08424
 9 Jun 11:31:45 ntpdate[156654]: step time server 203.107.6.88 offset 0.716122 sec
[root@Server1 ~]# ntpdate -q ntp1.aliyun.com
server 120.25.115.20, stratum 2, offset 0.712180, delay 0.06381
 9 Jun 11:32:29 ntpdate[157925]: step time server 120.25.115.20 offset 0.712180 sec

四、NTP的编译安装

可以参考

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值