linux搭建ntp服务

目录

一、背景

二、搭建ntp服务(服务端)

1.安装ntp服务

2.修改配置文件ntp.conf

3.重启ntp服务

三、利用ntp服务同步时间(客户端)

1.使用ntp服务自动同步时间

注意事项

2.使用ntpdate手动同步时间

常见错误

错误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博客

ntp服务及时间同步问题_ntpdate -u-CSDN博客

NTP时间同步_ntpdate_Sicong Lee的博客-CSDN博客

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值