Linux系统配置NTP时钟服务器

时间:2023-02-11 11:59:50

1、需要配置的计算机IP

192.168.0.131 bznode1

192.168.0.134 bznode2

192.168.0.135 bznode3

192.168.0.136 bznode4

设置IP=192.168.0.131为时钟服务器

IP=192.168.0.134服务器为需要同步机器

IP=192.168.0.135服务器为需要同步机器

IP=192.168.0.136服务器为需要同步机器

2、查看每个机器是否安装了ntp

[root@bznode1 ~]# rpm -qa | grep ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64
ntpdate-4.2.6p5-25.el7.centos.2.x86_64
[root@bznode1 ~]#

这样的输出证明已经安装好了,其中包就是" ntp-4.2.6p5-25.el7.centos.2.x86_64"

如果已经安装则略过此步,如果没有安装则先安装ntp,安装ntp有两种方式

1.使用yum进行安装,并设置系统开机自动启动并启动服务

[root@ bznode1 ~]# yum -y install ntp
[root@ bznode1 ~]# systemctl enable ntpd
[root@ bznode1 ~]# systemctl start ntpd

2.离线安装

首先需要下载ntp的rpm包,把下载好的包放到/home文件夹下面

下载地址​​​https://pkgs.org/​​

Linux系统配置NTP时钟服务器

安装ntp可能会遇到下面的问题,缺少依赖

[root@ bznode1 home]# rpm -ivh ntp-4.2.6p5-25.el7.centos.2.x86_64
错误:依赖检测失败:

Libopts.so.25()(64bit) 被 ntp-4.2.6p5-22.el7.centos.x86_64 需要
ntpdate = 4.2.6p5-22.el7.centos 被 ntp-4.2.6p5-22.el7.centos.x86_64 需要
[root@ bznode1 home]#

上面给出的截图里面已经下载到了需要的rpm包,下面就依次进行安装即可

[root@bznode1 home]# rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm 
警告:autogen-libopts-5.18-5.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:autogen-libopts-5.18-5.el7 ################################# [100%]
[root@bznode1 home]# rpm -ivh ntpdate-4.2.6p5-25.el7.centos.2.x86_64.rpm
警告:ntpdate-4.2.6p5-25.el7.centos.2.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:ntpdate-4.2.6p5-25.el7.centos.2 ################################# [100%]
[root@bznode1 home]# rpm -ivh ntp-4.2.6p5-25.el7.centos.2.x86_64.rpm
警告:ntp-4.2.6p5-25.el7.centos.2.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:ntp-4.2.6p5-25.el7.centos.2 ################################# [100%]
[root@bznode2 home]#

3、配置ntp.conf文件

配置NTP服务器的关键是需要配置这个文

[root@ bznode1 ~]# vi /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 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
#restrict 192.168.195.0 mask 255.255.0.0 nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 11
下面的省略了....

这是要改变的地方,server 0-3 注释掉, 加入本地时间服务器,主要是 127.127.1.0,由于是顶层服务,也就是说没有上级时间服务器 了,这里就是使用192.168.0.131的时间为准。接着,重启或者启动ntpd服务:

[root@ceph-node1 ~]# systemctl restart ntpd.service

此时,ntpd服务器已经配置好了。再执行:

[root@ceph-node1 ~]# systemctl enable ntpd.service

使服务在系统启动后就开启

4、在客户端机器上进行配置 

[root@ bznode2 ~]# vi /etc/ntp.conf 
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 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 127.127.1.0 iburst local clock
#fudge 127.127.1.0 stratum 11
#restrict 192.168.195.0 mask 255.255.0.0 nomodify
server 192.168.0.131
fudge 127.127.1.0 stratum 11
下面省略....

主要是添加下面两行:

server 192.168.0.131

fudge 127.127.1.0 stratum 11

#这个是需要11,因为不是*的

配置好保存,重启ntp服务并设置跟随系统启动

[root@bznode2 home]# systemctl restart ntpd.service
[root@bznode2 home]# systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

查看ntp服务状态

[root@bznode2 /]# ntpq -p
remote refid st t when poll reach delay offset jitter
=====================================================
*192.168.0.131 LOCAL(0) 6 u 66 64 377 0.094 -6.614 2.241
[root@bznode2 /]#

refid为LOCAL(0)为准

其他需要同步的服务器同样按照上面步骤进行操作即可

5、实时同步时间

执行命令 ntpdate 192.168.0.131可能会遇到下面的问

[root@bznode2 home]# ntpdate 192.168.0.131
11 Jan 16:31:32 ntpdate[5723]: the NTP socket is in use, exiting

然后使用lsof –i:123命令

[root@bznode2 home]# lsof -i:123
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ntpd 5670 ntp 16u IPv4 12173 0t0 UDP *:ntp
ntpd 5670 ntp 17u IPv6 12174 0t0 UDP *:ntp
ntpd 5670 ntp 18u IPv4 12179 0t0 UDP localhost:ntp
ntpd 5670 ntp 19u IPv4 12180 0t0 UDP bznode3:ntp
ntpd 5670 ntp 20u IPv6 12181 0t0 UDP bznode3:ntp
ntpd 5670 ntp 21u IPv6 12182 0t0 UDP localhost:ntp
[root@bznode2 home]# kill -9 5670

此后,再使用ntpdate 192.168.0.131就成功了!

[root@bznode2 home]# ntpdate 192.168.0.131
11 Jan 16:32:43 ntpdate[5753]: adjust time server 192.168.0.131 offset 0.150170 sec
[root@bznode2 home]#

如果lsof –i:123命令出现下面问题

[root@bznode2 /]# lsof -i:123
-bash: lsof: 未找到命令
[root@bznode2 /]#

则只需要安装上面截图中的lsof-4.87-4.el7.x86_64 (1).rpm就可以了

[root@bznode2 home]# rpm -ivh lsof-4.87-4.el7.x86_64\ \(1\).rpm 
警告:lsof-4.87-4.el7.x86_64 (1).rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:lsof-4.87-4.el7 ################################# [100%]
[root@bznode2 home]#