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

发布时间:2020-07-19 08:08:02 作者:loong576
来源:网络 阅读:1794

一、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命令

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

安装:

[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)

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

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详解

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

remote状态标识详解

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

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

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

3.suse11

安装ntp

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

centos7等各操作系统实现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

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

适用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命令方式都可以实现客户端的时钟同步,可根据生产情况择优选择。

推荐阅读:
  1. linux ntp时间同步配置
  2. linux设置ntp时间同步

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ntp ntpd ntpdate

上一篇:树形数组 学习之外总能发现别人更好的

下一篇:Git基础入门(七)Git撤销操作和远程仓库管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》