实现基于LAN的NTP时钟同步

朱雀 2022-11-27 08:59 272阅读 0赞

1.搭建LAN环境

2.选取某个节点作为 NTP服务器

例如 选取 192.168.1.149节点部署NTP,配置为NTP服务器

  1. #相关命令
  2. sudo apt-get install ntp #安装
  3. sudo systemctl enable ntpd #自启动
  4. systemctl is-enabled ntp #检查自启动是否开启
  5. sudo systemctl start ntp #手启动
  6. service ntp status #ntp 状态
  7. ntpq -p #ntp状态
  8. netstat -tlunp | grep ntp #查看启动的端口(123)
  9. firewall-cmd --zone=public --add-port=123/udp --permanent && firewall-cmd --reload #开放端口 123
  10. #ntp.conf
  11. # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
  12. driftfile /var/lib/ntp/ntp.drift
  13. # Leap seconds definition provided by tzdata
  14. leapfile /usr/share/zoneinfo/leap-seconds.list
  15. # Enable this if you want statistics to be logged.
  16. #statsdir /var/log/ntpstats/
  17. statistics loopstats peerstats clockstats
  18. filegen loopstats file loopstats type day enable
  19. filegen peerstats file peerstats type day enable
  20. filegen clockstats file clockstats type day enable
  21. # Specify one or more NTP servers.
  22. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
  23. # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
  24. # more information.
  25. #pool 0.ubuntu.pool.ntp.org iburst
  26. #pool 1.ubuntu.pool.ntp.org iburst
  27. #pool 2.ubuntu.pool.ntp.org iburst
  28. #pool 3.ubuntu.pool.ntp.org iburst
  29. # Use Ubuntu's ntp server as a fallback.
  30. #pool ntp.ubuntu.com
  31. # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
  32. # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
  33. # might also be helpful.
  34. #
  35. # Note that "restrict" applies to both servers and clients, so a configuration
  36. # that might be intended to block requests from certain clients could also end
  37. # up blocking replies from your own upstream servers.
  38. # By default, exchange time with everybody, but don't allow configuration.
  39. restrict -4 default kod notrap nomodify nopeer noquery limited
  40. restrict -6 default kod notrap nomodify nopeer noquery limited
  41. # Local users may interrogate the ntp server more closely.
  42. restrict 127.0.0.1
  43. restrict ::1
  44. # Needed for adding pool entries
  45. restrict source notrap nomodify noquery
  46. # Clients from this (example!) subnet have unlimited access, but only if
  47. # cryptographically authenticated.
  48. #restrict 192.168.123.0 mask 255.255.255.0 notrust
  49. restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap #放行局域网来源,允许192.168.1.x网段中的服务器访问本ntp服务器进行时间同步
  50. # If you want to provide time to your local subnet, change the next line.
  51. # (Again, the address is an example only.)
  52. #broadcast 192.168.123.255
  53. # If you want to listen to time broadcasts on your local subnet, de-comment the
  54. # next lines. Please do this only if you trust everybody on the network!
  55. #disable auth
  56. #broadcastclient
  57. #Changes recquired to use pps synchonisation as explained in documentation:
  58. #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
  59. server time.cloudflare.com prefer # Meinberg GPS167 with PPS
  60. #fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware
  61. #server 127.127.22.1 # ATOM(PPS)
  62. #fudge 127.127.22.1 flag3 1 # enable PPS API
  63. server 127.127.1.0 #local clock,和本地系统时间同步
  64. fudge 127.127.1.0 stratum 10 #127.127.1.0为第10层。ntp和127.127.1.0同步完后,就变成了11层。ntp是层次阶级的。同步上层服务器的stratum大小不能超过或等于16
  65. includefile /etc/ntp/crypto/pw
  66. keys /etc/ntp/keys
  67. disable monitor

3.其余节点手动同步

  1. ntpdate 192.168.1.149

4.其余节点通过 chrony 实现同步

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxODU0Mjkx_size_16_color_FFFFFF_t_70

5.示例

  1. mount -o remount -w / #挂载Linux系统外的文件,致其可修改
  2. vi /etc/chrony.conf #修改配置文件
  3. server 192.168.1.149 iburst minpoll 2 prefer
  4. makestep 1.0 -1
  5. sync # sync 指令会将存于 buffer 中的资料强制写入硬盘中。
  6. ps -ef | grep chronyd
  7. kill 4999
  8. chronyd -d &
  9. chronyc sources

6.常见问题

  • ntp未自启动
  • 关闭 chronyd ; 关闭 Ubuntu 系统设置里面的时钟获取

7.参考文献

设置局域网NTP对时:https://blog.csdn.net/s_p_j/article/details/88386981

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读

    相关 NTP时间同步

    1. 时区及GMT 地球共分为24个时区,按一圈360度计算,每个时区的跨度是15度。国际上约定以0度经线穿过的城市格林威治的时间作为标准时间(Greenwich