Linux配置NTP时间同步

Linux配置NTP时间同步

一、ntp和ntpdate区别

两个服务都是centos自带的(centos7中不自带ntp)

ntp守护进程为ntpd,配置文件是/etc/ntp.conf

ntpdate用于客户端的时间矫正,非NTP服务器可以不启动NTP。

二、搭建

  • NTP(network time protocol 网络时间协议)用于同步计算机和网络设备的内部的时间一种协议
  • NTP Server服务器IP:192.168.1.111
  • NTP Client客户端IP:192.168.1.112

2.1、搭建NTP服务器

yum install -y ntp
2.1.1、修改配置文件
[root@master ~]# vim /etc/ntp.conf

把配置文件下面四行注释掉:
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.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst

2.2.2、启动ntp服务,并开机自启动
启动ntp服务,并开机自启动
[root@master ~]# systemctl start ntpd
[root@master ~]# systemctl enable ntpd
2.2.3、查询ntp是否同步

【命令】ntpq -p

  • remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
  • refid:参考上一层ntp主机地址
  • st:stratum阶层
  • when:多少秒前曾经同步过时间
  • poll:下次更新在多少秒后
  • reach:已经向上层ntp服务器要求更新的次数
  • delay:网络延迟
  • offset:时间补偿
  • jitter:系统时间与bios时间差
[root@master ~]# date
Tue Nov 17 21:24:24 CST 2020

[root@master ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 time.cloudflare .STEP.          16 u    -   64    0    0.000    0.000   0.000
 ntp7.flashdance .STEP.          16 u    -   64    0    0.000    0.000   0.000
 202.118.1.130   .STEP.          16 u    -   64    0    0.000    0.000   0.000
*ntp5.flashdance 194.58.202.20    2 u   11   64    1  372.035  -17.860  28.092

[root@master ~]# ntpstat
synchronised to NTP server (193.182.111.14) at stratum 3
   time correct to within 244 ms
   polling server every 64 s

[root@master ~]# timedatectl
      Local time: Tue 2020-11-17 22:37:02 CST
  Universal time: Tue 2020-11-17 14:37:02 UTC
        RTC time: Tue 2020-11-17 14:37:03
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@master ~]# ntpdate -u 193.182.111.14
17 Nov 22:38:10 ntpdate[7787]: adjust time server 193.182.111.14 offset 0.039557 sec

2.3、NTP客户端配置

yum install -y ntp

2.3.1、修改配置文件

[root@slave1 ~]# vim /etc/ntp.conf

#配置允许NTP Server时间服务器主动修改本机的时间
restrict 192.168.1.111 nomodify notrap noquery
#配置时间服务器为本地搭建的NTP Server服务器
server 192.168.1.111 iburst

#配置允许上游时间服务器主动修改本机的时间
restrict 192.168.1.111 nomodify notrap noquery

#注释掉其他时间服务器
#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

2.3.2、启动ntp服务,并开机自启动
启动ntp服务,并开机自启动
systemctl start ntpd
systemctl enable ntpd
2.3.3、查看同步的主服务器,显示如下表示成功
# 启动后查看
[root@slave1 ~]# ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.1.111   202.12.97.45     2 u   20   64    0    0.000    0.000   0.000
原文地址:https://www.cnblogs.com/hsyw/p/13997447.html