NTP 服务器的配置与使用

服务器技术 2020-06-18 11:04:04 34

导读

搭建背景如果有很多台服务器,并且因为业务需要,导致服务器的时间必须一致时。这时就需要有一台NTP服务器来实现这个需求,下面三哥就讲一下如何在服务器上配置NTP时间服务。战前准备一台CentOS764位的服务器(这里是因为我只有这个设备所以用这个为例子)服务器需要放通NTP默……

搭建背景

如果有很多台服务器,并且因为业务需要,导致服务器的时间必须一致时。这时就需要有一台 NTP 服务器来实现这个需求,下面三哥就讲一下如何在服务器上配置 NTP 时间服务。

战前准备

一台 CentOS 7 64位的服务器(这里是因为我只有这个设备所以用这个为例子)

服务器需要放通 NTP 默认的 UDP 123 端口

充满智慧的大脑 :razz:

战斗开始

1、先检查一下服务器是否已经安装了 ntp 包:

rpm -q ntp

如果回显类似下面的内容,则证明服务器已经安装了 ntp 包:

[root@hufflepuff ~]# rpm -q ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64

如果没有安装 ntp 包,则使用下面的命令进行安装:

yum install ntp ntpdate -y

2、编辑 NTP 守护进程的主配置文件 /etc/ntp.conf :

vi /etc/ntp.conf

3、找到并注释 pool.ntp.org 项目的公共服务器默认列表(在需要注释的条目前加 # ):

# 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

在 NTP Public Pool Time Servers 选择你所在国家位置,然后将 NTP 服务器地址添加到配置中(这里我选择的中国):

server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

如果所配置的 NTP 服务器在使用过程中出现宕机的情况,这时候我们可以把时间同步为为本机时间,添加如下内容:

server 127.127.1.0 iburst local clock

最终 /etc/ntp.conf 配置的 NTP 服务器列表如下:

# 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
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
server 127.127.1.0 iburst local clock

4、找到 restrict 语句,并配置允许使用 NTP 服务器的 IP :

#Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

这里三哥配置的是允许所有 IP 使用,所以 IP 和子网掩码都使用 0.0.0.0:

restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap

这里可以配置仅允许某个 IP 使用,例如:

restrict 10.0.0.0

也可以配置允许 10.0.0.0/8 子网内所有 IP 使用,例如:

restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap

5、添加一个 logfile 语句,来记录 NTP 服务器在运行中遇到的问题,并指定到一个日志文件(在配置文件的空白出新增一行即可):

logfile /var/log/ntp.log

6、保存 /etc/ntp.conf 的配置,并且设置 NTP 开机启动

systemctl start ntpd
systemctl enable ntpd

分享战果

1、验证 NTP 节点同步状态和你的系统时间:

ntpq -p
date -R

回显类似以下内容:

[root@hufflepuff ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 61-216-153-106. .INIT.          16 u    -   64    0    0.000    0.000   0.000
 61-216-153-104. 62.161.56.158    3 u   40   64    1   79.066   -2.241   0.000
*212.47.249.141  5.103.128.88     3 u   26   64   77  235.565   13.264   5.392
 61-216-153-105. 62.161.56.158    3 u  233   64   10   84.121   -2.473   0.000
 LOCAL(0)        .LOCL.           5 l  363   64   40    0.000    0.000   0.000
[root@hufflepuff ~]# date -R
Wed, 22 Nov 2017 16:00:51 +0800

2、Linux 系统同步时间:

ntpdate -u ntp.vtrois.com

3、Windows 系统同步时间:

w32tm /config /syncfromflags:manual /manualpeerlist:"ntp.vtrois.com"
net stop w32time
net start w32time

也可以打开 控制面板 -> 时钟、语言和区域 找到 Internet 时间 选项,将 NTP 服务器地址填写进去点击 立即更新 即可。


1253067 TFnetwork_cn