CentOS7 配置NTP时间同步服务器

一、主机环境

主机名                              角色                                              IP

m01                                  ntp服务端                                   172.16.1.61

web01                              ntp客户端                                    172.16.1.7

web02                              ntp客户端                                    172.16.1.8

 

二、服务端配置

1.首先安装检查服务器是否安装了ntp、ntpdate

[root@m01 ~]# rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.x86_64
ntp-4.2.6p5-29.el7.centos.x86_64

2.如果没有,需要使用安装

[root@m01 ~]# yum -y install ntp ntpdate

3.修改ntp配置文件/etc/ntp.conf

1)注释以下配置

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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)新增如下配置

#日志文件
logfile /var/log/ntpd.log

#授权172.16.1.0网段上所有机器可以从这台机器上查询和时间同步
restrict 172.16.1.0 mask 225.225.225.0 nomotify notrap

#时间服务器列表
server 210.72.145.44    #中国国家授时中心
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com

#当外部时间不可用时,使用本地时间
server 127.0.0.1
fudge 127.0.0.1 stratum 10

#允许上层时间服务器主动修改本机时间
restrict 210.72.145.44    nomodify notrap noquery
restrict ntp1.aliyun.com  nomodify notrap noquery
restrict ntp2.aliyun.com  nomodify notrap noquery
restrict ntp3.aliyun.com  nomodify notrap noquery

4.保存退出,重启ntp服务、加入开启自启

[root@m01 ~]# systemctl enable ntpd
[root@m01 ~]# systemctl restart ntpd

5.启动ntp服务时,先手动同步下本地时间

[root@m01 ~]# ntpdate -u ntp1.aliyun.com

6.查询ntp同步时间是否启动

等待几分钟后,当出现如同第二次命令执行结果时即可

[root@m01 ~]# ntpstat 
unsynchronised
  time server re-starting
   polling server every 8 s

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

 

三、客户端配置

1.安装ntp、ntpdate

[root@web01 ~]# yum install ntp ntpdate

2.修改配置文件/etc/ntp.conf

1)注释以下配置

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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)新增如下配置

#ntp服务器地址
server 172.16.1.61

#新增:允许上层时间服务器主动修改本机时间
restrict 172.16.1.61 nomodify notrap noquery

#新增:当外部时间不可用时,使用本地时间
server 127.0.0.1  #local clock
fudge 127.0.0.1 stratum 10

3.保存配置,重启ntp服务并加入开启自启

[root@web01 ~]# systemctl enable ntpd
[root@web01 ~]# systemctl restart ntpd

4.查看ntp服务器信息

[root@web01 zabbix]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 172.16.1.61     120.25.115.20    3 u   30   64    1    0.512  -42.740   0.000
 localhost       .INIT.          16 l    -   64    0    0.000    0.000   0.000

 

四、测试

1.查看ntp服务端时间

[root@m01 ~]# date
Sun Mar 29 00:10:52 CST 2020

2.修改客户端时间

[root@web01 ~]# date -s 2020-01-01
Wed Jan  1 00:00:00 CST 2020
[root@web01 ~]# date
Wed Jan  1 00:00:03 CST 2020

3.等待一段时间后客户端时间自动同步,这里用手动同步验证下

[root@web02 ~]# ntpdate -u 172.16.1.61
29 Mar 00:12:14 ntpdate[19563]: adjust time server 172.16.1.61 offset 0.008159 sec
[root@web01 ~]# date
Sun Mar 29 00:12:22 CST 2020

4.如果觉得自动同步时间间隔比较久,可以设定个定时任务,使用ntpdate命令来同步服务端时间

这里以每分钟同步为例

[root@m01 ~]# crontab -e 
* * * * * /usr/sbin/ntpdate -u 172.16.1.61 &> /dev/null

 

至此,ntp时间同步服务器搭建完成

 

点赞
  1. Google Chrome Windows 10
    可以,可以,求带老多发一些这样有利于centos7新手的文章
  2. 说道:
    Google Chrome Windows 10
    被网页吸引的程序媛

回复 取消回复