­

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

  • 2019 年 11 月 30 日
  • 筆記

一、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 ntp  root      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 ntpd  Unit 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  server ntp.ntsc.ac.cn prefer  server ntp1.aliyun.com iburst  server time1.cloud.tencent.com  iburst  server 0.cn.pool.ntp.org iburst  server 1.cn.pool.ntp.org iburst  server 2.cn.pool.ntp.org iburst  > 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 iburst  includefile /etc/ntp/ntp-source.conf

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

启动服务

[root@ntp-server ~]# systemctl start ntpd  [root@ntp-server ~]# systemctl enable ntpd  Created 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.service  enabled  [root@ntp-server ~]# systemctl disable chronyd.service  Removed 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 -nupl  Active 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 iburst  server ntpserver iburst  [root@centos7 ~]# view /etc/hosts  172.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/hosts  root@ubuntu1604:~# apt install -y ntp  root@ubuntu1604:~# view /etc/ntp.conf  root@ubuntu1604:~# systemctl enable ntp  ntp.service is not a native service, redirecting to systemd-sysv-install  Executing /lib/systemd/systemd-sysv-install enable ntp  root@ubuntu1604:~# systemctl start ntp  root@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/hosts  172.27.9.131    ntpserver  suse11:~ # view /etc/ntp.conf  #server 127.127.1.0             # local clock (LCL)  #fudge  127.127.1.0 stratum 10  # LCL is unsynchronized  server ntpserver iburst  suse11:~ # service ntp restart  Shutting down network time protocol daemon (NTPD)                                                                                                                                                                             done  Starting network time protocol daemon (NTPD)                                                                                                                                                                                  done  suse11:~ # 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.285  suse11:~ # chkconfig ntp on  suse11:~ # chkconfig --list|grep ntp  ntp                       0:off  1:off  2:off  3:on   4:off  5:on   6:off

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

4.AIX

aix:/ #view /etc/ntp.conf  driftfile /etc/ntp.drift  tracefile /etc/ntp.trace  server ntpserver  aix:/ #view /etc/hosts  172.27.9.131    ntpserver  aix:/ #stopsrc -s xntpd  0513-044 The /usr/sbin/xntpd Subsystem was requested to stop.  aix:/ #startsrc -s xntpd  0513-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.43  aix:/ #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/hosts  172.27.9.131    ntpserver  [root@redhat6 ~]# view /etc/ntp.conf  server ntpserver iburst  [root@redhat6 ~]# service ntpd restart  Shutting 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 -l  0 1 * * * /usr/sbin/ntpdate  ntpserver

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

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