ntp客户端的安装与配置

问题发现:近期进行某业务测试,业务主机时间不同步会导致业务主机日志时间不一样,对业务日志分析造成了很大时间成本;同时,由于时间不同步也会导致很多小问题,特别是与其他接口对接的测试,因此,特简单整理出ntp配置的步骤,本次总结了ntp服务端的配置。另外,可使用date -s命令设置系统时间,但是也会造成时间不太同步(本次操作在测试机上进行并已在真实环境下验证)。
一、环境介绍如下:
1、系统版本:
(1)客户端:

root@xuhs:/root>cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

(2)服务端:

root@mysql1:/root>cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.9 (Santiago)

2、查看ntp版本:

root@xuhs:/root>rpm -qa|grep ntp
root@xuhs:/root>

环境没有部署ntp

二、部署ntp客户端:
1、准备ntp的rpm包
下载:http://mirror.centos.org/centos/7/os/x86_64/Packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm

2、上传ntp的rpm包:

root@xuhs:/root>cd /tmp
root@xuhs:/tmp>rz -e
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm...
  100%     548 KB     548 KB/sec    00:00:01       0 Errors 

3、使用rpm命令进行编译:

root@xuhs:/tmp>rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm 
warning: ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
error: Failed dependencies:
        ntpdate = 4.2.6p5-29.el7.centos.2 is needed by ntp-4.2.6p5-29.el7.centos.2.x86_64

若有ntpdate依赖报错,需要安装ntpdate依赖,下载ntpdate的地址如下:http://mirror.centos.org/centos/7/os/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

4、上传ntpdate的rpm包:

root@xuhs:/tmp>cd /tmp
root@xuhs:/tmp>rz -e
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm...
  100%      86 KB      86 KB/sec    00:00:01       0 Errors 
root@xuhs:/tmp>ls -ltr ntp*
-rw-r--r-- 1 root root 561812 May  6 12:52 ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
-rw-r--r-- 1 root root  88692 May  6 13:05 ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

5、再次使用rpm命令进行编译:

root@xuhs:/tmp>rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm 
warning: ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:ntpdate-4.2.6p5-29.el7.centos.2  ################################# [100%]
root@xuhs:/tmp>
root@xuhs:/tmp>
root@xuhs:/tmp>rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
warning: ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:ntp-4.2.6p5-29.el7.centos.2      ################################# [100%]
root@xuhs:/tmp>

6、进行ntp配置,先ntp配置文件进行备份:

root@xuhs:/tmp>cd /etc/
root@xuhs:/etc>cp ntp.conf  ntp.conf.bak
root@xuhs:/etc>ls -ltr ntp.con*
-rw-r--r-- 1 root root 2000 Nov 28  2019 ntp.conf
-rw-r--r-- 1 root root 2000 May  6 13:29 ntp.conf.bak

7、配置ntp服务端地址:

root@xuhs:/etc>vi ntp.conf
root@xuhs:/etc>echo -e 'server 192.168.100.11'>>ntp.conf

root@xuhs:/etc>diff  ntp.conf ntp.conf.bak 
59d58
< server 192.168.100.11

8、启动ntp服务(先手工同步,再启动ntp,可减少同步时间):

root@xuhs:/etc>ntpdate 192.168.100.11
 6 May 15:16:58 ntpdate[84169]: adjust time server 192.168.100.11 offset 0.133845 sec
root@xuhs:/etc>service ntpd start
Redirecting to /bin/systemctl start ntpd.service

9、查看ntp运行状态:
(1)ntp服务运行状态(或者使用service ntpd status):

root@xuhs:/root>systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-05-06 15:17:08 CST; 53min ago
  Process: 84186 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 84187 (ntpd)
   CGroup: /system.slice/ntpd.service
           ├─84187 /usr/sbin/ntpd -u ntp:ntp -g
           └─84188 /usr/sbin/ntpd -u ntp:ntp -g

May 06 15:43:20 xuhs ntpd[84187]: 0.0.0.0 0612 02 freq_set kernel -66.918 PPM
May 06 15:43:20 xuhs ntpd[84187]: 0.0.0.0 0615 05 clock_sync
May 06 15:48:21 xuhs ntpd_intres[84188]: host name not found: 0.centos.pool.ntp.org
May 06 15:48:21 xuhs ntpd_intres[84188]: host name not found: 1.centos.pool.ntp.org
May 06 15:48:21 xuhs ntpd_intres[84188]: host name not found: 2.centos.pool.ntp.org
May 06 15:48:21 xuhs ntpd_intres[84188]: host name not found: 3.centos.pool.ntp.org
May 06 16:04:24 xuhs ntpd_intres[84188]: host name not found: 0.centos.pool.ntp.org
May 06 16:04:24 xuhs ntpd_intres[84188]: host name not found: 1.centos.pool.ntp.org
May 06 16:04:24 xuhs ntpd_intres[84188]: host name not found: 2.centos.pool.ntp.org
May 06 16:04:24 xuhs ntpd_intres[84188]: host name not found: 3.centos.pool.ntp.org

(2)ntp服务连接服务端状态:

root@xuhs:/etc>ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.100.11  LOCAL(1)         6 u    1   64   37    0.629  128.791  92.521
root@xuhs:/etc>

10、设置开机自启动:

root@xuhs:/root>systemctl list-unit-files |grep ntpd
ntpd.service                                  disabled
ntpdate.service                               disabled
root@xuhs:/root>
root@xuhs:/root>systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
root@xuhs:/root>
root@xuhs:/root>systemctl list-unit-files |grep ntpd
ntpd.service                                  enabled 
ntpdate.service                               disabled

到此ntp客户端就配置完成。

11、ntp常用命令:
(1)ntp服务启动命令:
service ntpd start
(2)ntp服务重启命令:
service ntpd restart
(3)ntp服务停止命令:
service ntpd stop
(4)查看ntp服务:
watch ntpq -p或者ntpq -p

三、部署ntp遇到问题:
一)重启主机后,ntp服务异常
1、进行宕机测试后,ntp服务出现异常,但是自启动是正常的;

root@xuhs:/root>reboot 
root@xuhs:/root>ntpq -p
ntpq: read: Connection refused
root@xuhs:/root>
root@xuhs:/root>systemctl list-unit-files |grep ntpd
ntpd.service                                  enabled 
ntpdate.service                               disabled

2、查看日志message后,发现有个chronyd服务,:

root@xuhs:/var/log>grep 'NTP' messages 
May  6 16:46:28 localhost systemd: Starting NTP client/server...
May  6 16:46:28 localhost chronyd[1159]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
May  6 16:46:29 localhost systemd: Started NTP client/server.

3、使用man chronyd查看:

root@xuhs:/root>man chronyd
CHRONYD(8)                                                       System Administration                                                      CHRONYD(8)
NAME
       chronyd - chrony daemon
SYNOPSIS
       chronyd [OPTION]... [DIRECTIVE]...
DESCRIPTION
       chronyd is a daemon for synchronisation of the system clock. It can synchronise the clock with NTP servers, reference clocks (e.g. a GPS
       receiver), and manual input using wristwatch and keyboard via chronyc. It can also operate as an NTPv4 (RFC 5905) server and peer to provide a
       time service to other computers in the network.
       If no configuration directives are specified on the command line, chronyd will read them from a configuration file. The compiled-in default
       location of the file is /etc/chrony.conf.
       Information messages and warnings will be logged to syslog.
             .......

Chronyd是一个同步系统时钟的守护进程,可以同步时钟与NTP服务器。CentOS7默认下是使用chronyd进行时间同步。

4、通过禁用Chronyd服务,即可解决:

root@xuhs:/var/log>systemctl disable chronyd
Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.
root@xuhs:/var/log>
root@xuhs:/var/log>systemctl list-unit-files chronyd
UNIT FILE STATE

0 unit files listed.
root@xuhs:/var/log>

5、验证如下:

root@xuhs:/var/log>reboot
root@xuhs:/root>ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.100.11  LOCAL(1)         6 u   44   64   17    0.792   -0.056   0.045
root@xuhs:/root>
上一篇:Andrew Ng机器学习编程作业:Logistic Regression


下一篇:Linux中安装ntp时间服务器