目录
错误1:ntpdate -u ip -> no server suitable for synchronization found
错误2:ntpdate ip -> ntpdate[2768]: the NTP socket is in use, exiting
一、背景
在搭建数据库集群时往往需要同步集群内各机器的时间,想利用ntp服务来同步时间,因为服务器无法连接到公网,所以不能同步公网时间,只能以其中一台机器本地时间为准同步其他机器的时间。
二、搭建ntp服务(服务端)
1.安装ntp服务
yum install -y ntp
2.修改配置文件ntp.conf
vi /etc/ntp.conf
# For more information about this file, see the ntp.conf(5) man page.
# Record the frequency of the system clock.
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 noepeer noquery
# Permit association with pool servers.
restrict source nomodify notrap noepeer 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
#添加客户端ip和掩码信息,即允许哪些机器使用该机器的ntp服务同步时间
restrict 192.168.200.11 mask 255.255.255.0 nomodify notrap
# 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).
# pool 2.openEuler.pool.ntp.org iburst
# Reduce the maximum number of servers used from the pool.
tos maxclock 5
# Enable public key cryptography.
# crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
# trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
# requestkey 8
# Specify the key identifier to use with the ntpq utility.
# controlkey 8
# Enable writing of statistics records.
# statistics clockstats cryptostats loopstats peerstats
#指定公网的NTP源,如果不需要则不添加即可
server ntp1.aliyun.com prefer iburst minpoll 4 maxpoll 4
server ntp2.aliyun.com iburst minpoll 4 maxpoll 4
#配置本地时钟源
server 127.127.1.0
fudge 127.127.1.0 stratum 10
3.重启ntp服务
systemctl restart ntpd
三、利用ntp服务同步时间(客户端)
1.使用ntp服务自动同步时间
与服务端搭建步骤相同,配置ntp.conf,无需配置restrict项,server项配置服务端的ip作为ntp源。
以下是一个示例
# For more information about this file, see the ntp.conf(5) man page.
# Record the frequency of the system clock.
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 noepeer noquery
# Permit association with pool servers.
restrict source nomodify notrap noepeer 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).
# pool 2.openEuler.pool.ntp.org iburst
# Reduce the maximum number of servers used from the pool.
tos maxclock 5
# Enable public key cryptography.
# crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
# trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
# requestkey 8
# Specify the key identifier to use with the ntpq utility.
# controlkey 8
# Enable writing of statistics records.
# statistics clockstats cryptostats loopstats peerstats
#以192.168.200.10机器作为ntp时钟源,将其作为首选ntp源并快速同步,每2^4(16)秒同步一次。
server 192.168.200.10 prefer iburst minpoll 4 maxpoll 4
配置完成后启动ntp服务即可实现每16秒同步一次时间。
注意事项
使用ntp自动同步时,server的配置中必须指定minpoll和maxpoll才能实现自动同步时间,具体参数的含义可以参考总结中引用的博客。
2.使用ntpdate手动同步时间
使用ntpdate -u ip(ntp源)手动同步时间,将其添加到crontable中实现定时同步时间。
ntpdate -d 选项可以查看调试信息。
ntpdate -u 选项可以避免和ntp服务本身冲突
ntpq -p 可以显示当前ntp服务的时钟源信息,具体参数的含义可以参考总结中引用的博客。
常见错误
错误1:ntpdate -u ip -> no server suitable for synchronization found
原因:在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。NTP server还没有和其自身或者它的server同步上。在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
处理:等待几分钟后,重试一般解决。
错误2:ntpdate ip -> ntpdate[2768]: the NTP socket is in use, exiting
原因:从报错信息可以看到是socket已经在使用,正是ntp服务占用了。
处理:停止ntp服务或添加ntpdate -u选项。发现添加-d选项也可以,就是会打印出调试信息。
四、总结
因为ntp服务启动后即使只是用本地时间源作为ntp时钟源,也需要5分钟的时间进行同步后才能对外提供服务,所以最终也没有采用这种方案。只是对ntp服务进行了简单的了解。
更详细的配置参数和命令的使用可以参考这篇博客,写的比较详细linux——搭建NTP服务器_linux ntp-CSDN博客文章浏览阅读7.9k次,点赞3次,收藏36次。简单搭建一个NTP服务器_linux ntphttps://blog.csdn.net/qq_43325582/article/details/130852018?spm=1001.2014.3001.5506
参考博客:
linux——搭建NTP服务器_linux ntp-CSDN博客