CentOS 7 配置NTP服务

  • 原创
  • Madman
  • /
  • /
  • 0
  • 7746 次阅读

Synopsis: Network Time Protocol(网络时间协议)是 GNU/Linux 系统通过互联网时间服务器同步系统软件时钟的最常见方法,设计时考虑到了各种网络延迟,通过公共网络同步时,误差可以降低到10毫秒以内。通过本地网络同步时,误差可以降低到 1 毫秒

在公司环境中,如果你不想为 NTP 传输打开防火墙,就有必要设置一个内部 NTP 服务器,然后让员工使用内部服务器而不是公共 NTP 服务器

1. 为什么需要NTP?

由于制造工艺多种多样,所有的(非原子)时钟并不按照完全一致的速度行走,有一些时钟走的比较快,而有一些走的比较慢。因此经过很长一段时间以后,一个时钟的时间慢慢的和其它的发生偏移,这就是常说的 时钟漂移时间漂移。为了将时钟漂移的影响最小化,使用 NTP 的主机应该周期性地和指定的 NTP 服务器 交互以保持它们的时钟同步

在不同的主机之间进行时间同步对于计划备份入侵检测记录分布式任务调度或者事务订单管理来说是很重要的事情,它甚至应该作为日常任务的一部分

2. NTP 分层架构

NTP 服务器通过一个层级系统进行分类,不同的层级称为 stratum,独立的时间源为stratum 0,直接连接到 stratum 0 的设备为 stratum 1,直接连接到 stratum 1 的源为 stratum 2,以此类推。服务器的 stratum 并不能完全等同于它的精度和可靠度。通常的时间同步都使用 stratum 2 服务器。通过 http://www.pool.ntp.org 官方 NTP 公共时间服务器池可以选择比较近的服务器池

1 NTP分层架构

Stratum 0 由没有时间漂移的时钟组成,例如原子时钟,这种时钟不能在网络上直接使用。Stratum N (N > 1) 层服务器从 Stratum N-1 层服务器同步时间,Stratum N 时钟能通过网络和彼此互联

NTP 支持多达 15 个 stratum 的层级,Stratum 16 被认为是未同步的,不能使用的

2.1 CentOS 7 实现了NTP协议的程序

  • chronyd
  • ntpd

2.2 配置NTP前需要正确设置系统时区

# 查看系统当前时区
[root@NTPServer ~]# timedatectl

# 查看系统支持的时区
[root@NTPServer ~]# timedatectl list-timezones

# 如何更改时区
[root@NTPServer ~]# timedatectl set-timezone "Asia/Shanghai"

3. Chrony Suite

参考: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/ch-Configuring_NTP_Using_the_chrony_Suite.html

3.1 安装软件包

[root@NTPServer ~]# yum -y install chrony

注意:

如果 CentOS 系统中已经安装了 ntp,并已经开启了 ntpd 服务进程的话,需要先停止其服务并取消开机启动,否则后续设置 chronyd 开启启动后,在下次开机时并不会生效

3.2 配置

(1)指定上层NTP服务器

通过查询 http://www.pool.ntp.org,修改配置文件 /etc/chrony.conf 中默认的 *.centos.pool.ntp.org 为离本地最近的 NTP 服务器,比如 cn.pool.ntp.org

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 注释掉默认的下面4行配置
#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
# 指定上层NTP服务器
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
  • 推荐使用 iburst 选项,如果第一次尝试无法建立连接,程序会发送一系列的包
  • 使用 prefer 选项,指定在一组上层 NTP 服务器中,优先使用此服务器

(2)允许客户端来源(可选)

如果你当前服务器只是想校准时间,并不打算作为内部 NTP 服务器(给内部其它服务器同步时间用),那么可以跳过此步骤!如果当前服务器需要当作 NTP 服务器的话,可以配置 允许内部哪些网段,能够从当前服务器同步时间

修改配置文件 /etc/chrony.conf

# Allow NTP client access from local network.
#allow 192.168/16
allow 192.168.40.0/24

(3)启动 chronyd 服务

chronyd 是温和同步时间的,为防止本地 NTP 服务器时间与上层 NTP 差异太多,导致很久才会时间同步一致,需要先手动同步一次:

[root@NTPServer ~]# ntpdate cn.pool.ntp.org

启动 chronyd 并设置开机启动:

[root@NTPServer ~]# systemctl start chronyd
[root@NTPServer ~]# systemctl enable chronyd

注意:

如果系统开启了防火墙 firewalld,则要允许 NTP 服务使用 123/UDP323/UDP(以下配置是允许所有人访问本 NTP):

[root@NTPServer ~]# firewall-cmd --add-service=ntp --permanent 
success
[root@NTPServer ~]# firewall-cmd --reload 
success

(4)验证同步状态

[root@NTPServer ~]# chronyc tracking
Reference ID    : 120.198.233.90 (120.198.233.90)
Stratum         : 4
Ref time (UTC)  : Fri Apr 14 05:57:00 2017
System time     : 0.000244711 seconds fast of NTP time
Last offset     : +0.000560634 seconds
RMS offset      : 0.000740996 seconds
Frequency       : 54.533 ppm slow
Residual freq   : +0.022 ppm
Skew            : 0.497 ppm
Root delay      : 0.106555 seconds
Root dispersion : 0.183685 seconds
Update interval : 1036.1 seconds
Leap status     : Normal
[root@NTPServer ~]
                                
                            
  • kenney
  • challen
  • mitsi
  • tiffaniamber
  • chloelynn
  • dudley
  • caitlen
  • theresita
  • lilien
  • 60505186
  • truly
  • juneau
  • brailey
  • jolijn
  • lilla
  • reggie
  • waunda
  • elder
  • derion
  • siane
  • kejuana
  • karmelo
  • lilleeana
  • faedra
未经允许不得转载: LIFE & SHARE - 王颜公子 » CentOS 7 配置NTP服务

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列