千家信息网

centos7等各操作系统实现ntp时钟同步

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,一、ntp server搭建1.centos7.6搭建ntp server安装前:[root@ntpserver ~]# more /etc/redhat-release CentOS Linux r
千家信息网最后更新 2025年12月02日centos7等各操作系统实现ntp时钟同步

一、ntp server搭建

1.centos7.6搭建ntp server

安装前:

[root@ntpserver ~]# more /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@ntp-server ~]# ps -ef|grep ntproot      18054  17742  0 15:53 pts/0    00:00:00 grep --color=auto ntp[root@ntp-server ~]# ll /etc/|grep ntp[root@ntp-server ~]# ntpdate-bash: ntpdate: 未找到命令[root@ntp-server ~]# systemctl status ntpdUnit ntpd.service could not be found.

ntp server ip为172.27.9.131,操作系统版本为centos 7.6,安装前无ntp服务,/etc目录下也没有相关ntp文件,也没有ntpdate命令

安装:

[root@ntp-server ~]# yum -y install ntp[root@ntp-server ~]# ps -ef|grep ntp      root      18225  17742  0 16:00 pts/0    00:00:00 grep --color=auto ntp[root@ntp-server ~]# ll /etc/|grep ntp    drwxr-xr-x   3 root root       52 11月  5 15:59 ntp-rw-r--r--   1 root root     2000 8月   6 21:44 ntp.conf[root@ntp-server ~]# ntpdate               5 Nov 16:01:04 ntpdate[18240]: no servers can be used, exiting[root@ntp-server ~]# systemctl status ntpd● ntpd.service - Network Time Service   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)   Active: inactive (dead)

2.ntp server配置

新建配置文件ntp-source.conf

[root@ntp-server ~]# cat >/etc/ntp/ntp-source.conf < EOF

ntp.ntsc.ac.cn为国家授时中心域名(优先选用该ntp源),ntp1.aliyun.com为阿里云ntp服务器,time1.cloud.tencent.com为腾讯云ntp服务器,0.cn.pool.ntp.org等为ntp官网中国地区ntp服务器。

修改配置文件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 iburstincludefile /etc/ntp/ntp-source.conf

时钟源调用配置文件/etc/ntp/ntp-source.conf

启动服务

[root@ntp-server ~]# systemctl start ntpd [root@ntp-server ~]# systemctl enable ntpdCreated symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.[root@ntp-server ~]# systemctl is-enabled chronyd.serviceenabled[root@ntp-server ~]# systemctl disable chronyd.serviceRemoved symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.

启动ntp服务并设置开机启动, 如果 chrony 被设置为开机启动则移除, Centos7中默认使用chrony来实现时间同步 , chrony与ntpd冲突,可能引起ntpd开启启动失败

ntp服务查看

[root@ntpserver ~]# ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================*114.118.7.161   123.139.33.3     2 u   36   64   77   23.196    6.770   1.028+120.25.115.20   10.137.53.7      2 u   24   64   77   20.913    6.897   1.506-139.199.215.251 100.122.36.196   2 u   22   64   77   21.854    8.122   1.458-119.28.183.184  100.122.36.196   2 u   19   64   77   33.594    4.266   2.011+ntp6.flashdance 194.58.202.148   2 u   91   64   72  268.044    6.483   1.516+119.28.206.193  100.122.36.4     2 u   20   64   77   32.332    7.080   1.344[root@ntpserver ~]# ntpstat synchronised to NTP server (114.118.7.161) at stratum 3   time correct to within 996 ms   polling server every 64 s

ntpq -p和ntpstat命令可以查看ntp同步状态,每64秒同步一次

ntpq -p详解

  • remote:响应这个请求的 NTP 服务器的名称。
  • refid:NTP 服务器使用的上一级 NTP 服务器。
  • st:remote 远程服务器的级别。服务器从高到低级别设定为1 - 16,为了减缓负荷和网络堵塞,原则上建议避免直接连接到级别为1的服务器。
  • t :u: unicast(单播), b: broadcast(广播), l: local(本地时钟)
  • when:上一次成功请求之后到现在的秒数。
  • poll:本地机和远程服务器多少时间进行一次同步(单位为秒)。初始运行 NTP 时,poll 值会比较小,和服务器同步的频率增加,建议尽快调整到正确的时间范围。调整之后,poll 值会逐渐增大,同步的频率也将会相应减小。
  • reach:八进制值,用来测试能否和服务器连接。每成功连接一次,reach 的值将会增加。
  • delay:从本地机发送同步要求到 NTP 服务器的 round trip time。
  • offset:主机通过 NTP 时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset 越接近于0,主机和 NTP 服务器的时间越接近。
  • jitter:用来做统计的值。统计在特定连续的连接数里 offset 的分布情况。即 jitter 数值的绝对值越小,主机的时间就越精确。

remote状态标识详解

  • :表示没有远程通信的主机
  • x:已不再使用
  • -:已不再使用
  • #:良好的远程节点或服务器但是未被使用
  • +:良好的且优先使用的远程节点或服务器
  • *****:当前作为优先主同步对象的远程节点或服务器
  • o:PPS 节点 (当优先节点是有效时)。实际的系统同步是源于秒脉冲信号(pulse-per-second,PPS),可能通过PPS 时钟驱动或者通过内核接口

ntp端口查看

[root@ntpserver ~]# netstat -nuplActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    udp        0      0 172.27.9.131:123        0.0.0.0:*                           17174/ntpd          udp        0      0 127.0.0.1:123           0.0.0.0:*                           17174/ntpd          udp        0      0 0.0.0.0:123             0.0.0.0:*                           17174/ntpd          udp6       0      0 fe80::20c:29ff:fed9:123 :::*                                17174/ntpd          udp6       0      0 ::1:123                 :::*                                17174/ntpd          udp6       0      0 :::123                  :::*                                17174/ntpd         

ntp服务端口为123,协议为UDP

二、客户端同步--ntpd服务方式

1.centos7.3

安装和启动同ntp server,配置如下:

[root@centos7 ~]# view /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 iburstserver ntpserver iburst [root@centos7 ~]# view /etc/hosts172.27.9.131    ntpserver[root@centos7 ~]# systemctl enable ntpd[root@centos7 ~]# systemctl start ntpd[root@centos7 ~]# ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================*ntpserver       114.118.7.161    3 u    1   64    3    0.221   12.641   0.495[root@centos7 ~]# ntpstat synchronised to NTP server (172.27.9.131) at stratum 4   time correct to within 1049 ms   polling server every 64 s

2.ubuntu1604

root@ubuntu1604:~# view /etc/hostsroot@ubuntu1604:~# apt install -y ntproot@ubuntu1604:~# view /etc/ntp.conf root@ubuntu1604:~# systemctl enable ntpntp.service is not a native service, redirecting to systemd-sysv-installExecuting /lib/systemd/systemd-sysv-install enable ntproot@ubuntu1604:~# systemctl start ntproot@ubuntu1604:~# ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================*ntpserver       114.118.7.161    3 u   51   64  377    0.254   -4.194   1.090

3.suse11

安装ntp

suse11:~ # view /etc/hosts172.27.9.131    ntpserversuse11:~ # view /etc/ntp.conf #server 127.127.1.0             # local clock (LCL)#fudge  127.127.1.0 stratum 10  # LCL is unsynchronizedserver ntpserver iburstsuse11:~ # service ntp restartShutting down network time protocol daemon (NTPD)                                                                                                                                                                             doneStarting network time protocol daemon (NTPD)                                                                                                                                                                                  donesuse11:~ # ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================*ntpserver       114.118.7.161    3 u    1   64    1    0.223    2.271   0.285suse11:~ # chkconfig ntp onsuse11:~ # chkconfig --list|grep ntpntp                       0:off  1:off  2:off  3:on   4:off  5:on   6:off

默认为与本地同步,修改为与ntpserver同步。

4.AIX

aix:/ #view /etc/ntp.confdriftfile /etc/ntp.drifttracefile /etc/ntp.traceserver ntpserveraix:/ #view /etc/hosts  172.27.9.131    ntpserveraix:/ #stopsrc -s xntpd0513-044 The /usr/sbin/xntpd Subsystem was requested to stop.aix:/ #startsrc -s xntpd0513-059 The xntpd Subsystem has been started. Subsystem PID is 1663192.aix:/ #ntpq -p     remote           refid      st t when poll reach   delay   offset    disp==============================================================================*ntpserver       114.118.7.161    3 u    1   64   37     1.16    5.744  875.43aix:/ #l***c -ls xntpd Program name:    /usr/sbin/xntpd  Version:         3  Leap indicator:  00 (No leap second today.)  Sys peer:        ntpserver  Sys stratum:     4  Sys precision:   -17  Debug/Tracing:   DISABLED  Root distance:   0.051193  Root dispersion: 0.176468  Reference ID:    172.27.9.131  Reference time:  e16e4fd5.e2945000  Thu, Nov  7 2019 16:09:57.885  Broadcast delay: 0.003906 (sec)  Auth delay:      0.000122 (sec)  System flags:    pll monitor filegen   System uptime:   436 (sec)  Clock stability: 7.000000 (sec)  Clock frequency: 0.000000 (sec)  Peer: ntpserver       flags: (configured)(sys peer)       stratum:  3, version: 3       our mode: client, his mode: server Subsystem         Group            PID          Status  xntpd            tcpip            1663192      active

5.redhat6.8

[root@redhat6 ~]# view /etc/hosts172.27.9.131    ntpserver[root@redhat6 ~]# view /etc/ntp.confserver ntpserver iburst[root@redhat6 ~]# service ntpd restartShutting down ntpd:                                        [  OK  ]Starting ntpd:                                             [  OK  ][root@redhat6 ~]# ntpq -p     remote           refid      st t when poll reach   delay   offset  jitter==============================================================================*ntpserver       114.118.7.161    3 u    1   64    1    0.432    3.366   0.000[root@redhat6 ~]# ntpstat synchronised to NTP server (172.27.9.131) at stratum 4    time correct to within 8030 ms   polling server every 64 s

6.windows 7

适用windows7和Windows Server 2008等

三、客户端同步--ntpdate命令方式

[root@centos7 ~]# systemctl stop ntpd[root@centos7 ~]# ntpdate ntpserver   7 Nov 16:33:02 ntpdate[2618]: adjust time server 172.27.9.131 offset -0.002048 sec[root@centos7 ~]# crontab -l0 1 * * * /usr/sbin/ntpdate  ntpserver

ntp服务和ntpdate命令不可同时使用,使用ntpdate之前,需要停止ntp服务。采用ntpdate命令方式进行时钟同步适用于suse、redhat、AIX、centos、ubuntu等操作系统,可做成定时任务每天定时同步。

ntpd服务方式和ntpdate命令方式都可以实现客户端的时钟同步,可根据生产情况择优选择。

服务 同步 服务器 命令 时间 时钟 方式 节点 配置 主机 文件 系统 客户 级别 操作系统 良好 成功 单位 客户端 建议 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 实践训练软件开发文档 服务器内存占用过高 兰州ai服务器如何挑选 linux服务器连接数 广西网络安全技术培训十大品牌 云服务器的数据能相互交换吗 流花湖服务器 cf服务器暂停图片 电子科技互联网工程师是做什么的 如何提高数据库运行性 保定友泽软件开发有限公司 静安区即时网络技术铸造辉煌 数据库表创建语句 数据库应用课程的课后总结 阿卡丽网络技术有限公司 不动产数据库建库技术方案 电信网络安全宣传进校园 蜀山区网络技术服务怎么样 怎么处理数据库模式中的数据冗余 国家扶持的互联网科技股票 网络技术实训总结体会 国泰新点软件开发面试题 碧橙网络技术有限公司招聘 深圳软件开发培训机构排名榜 服务器机械硬盘声音大吗 怒江长期上门回收服务器 如何实施数据库安全 软件开发网络接单平台 word文管理服务器 深圳捷思互联网科技
0