普遍使用Chrony

现在Linux的发行版已经普遍使用chrony来代替ntpd进行时间同步管理了。
使用以下命令查看你的系统上chrony启动状态:

  1. systemctl status chronyd

如果你的系统没有chronyd,可以使用以下命令进行安装。

  1. yum install chrony -y

一般情况下,chrony只要启动了很快就会从默认的配置中完成时间的同步,所以只需要重启chrony就可以同步时间。

  1. #启动应用
  2. systemctl enable --now chronyd
  3. #重新启动应用
  4. systemctl restart chronyd

Chrony包含两个部分,chronyd是提供时间同步服务的守护进程,chronyc是用来控制chrony的命令行工具。

Chronyc

与ntpd还有一个不同之处就是Chrony自带了一个命令行工具。

  1. System clock:
  2. tracking Display system time information
  3. makestep Correct clock by stepping immediately
  4. makestep <threshold> <updates>
  5. Configure automatic clock stepping
  6. maxupdateskew <skew> Modify maximum valid skew to update frequency
  7. waitsync [<max-tries> [<max-correction> [<max-skew> [<interval>]]]]
  8. Wait until synchronised in specified limits
  9. Time sources:
  10. sources [-v] Display information about current sources
  11. sourcestats [-v] Display statistics about collected measurements
  12. reselect Force reselecting synchronisation source
  13. reselectdist <dist> Modify reselection distance
  14. NTP sources:
  15. activity Check how many NTP sources are online/offline
  16. ntpdata [<address>] Display information about last valid measurement
  17. add server <address> [options]
  18. Add new NTP server
  19. add peer <address> [options]
  20. Add new NTP peer
  21. delete <address> Remove server or peer
  22. burst <n-good>/<n-max> [<mask>/<address>]
  23. Start rapid set of measurements
  24. maxdelay <address> <delay> Modify maximum valid sample delay
  25. maxdelayratio <address> <ratio>
  26. Modify maximum valid delay/minimum ratio
  27. maxdelaydevratio <address> <ratio>
  28. Modify maximum valid delay/deviation ratio
  29. minpoll <address> <poll> Modify minimum polling interval
  30. maxpoll <address> <poll> Modify maximum polling interval
  31. minstratum <address> <stratum>
  32. Modify minimum stratum
  33. offline [<mask>/<address>] Set sources in subnet to offline status
  34. online [<mask>/<address>] Set sources in subnet to online status
  35. onoffline Set all sources to online or offline status
  36. according to network configuration
  37. polltarget <address> <target>
  38. Modify poll target
  39. refresh Refresh IP addresses
  40. Manual time input:
  41. manual off|on|reset Disable/enable/reset settime command
  42. manual list Show previous settime entries
  43. manual delete <index> Delete previous settime entry
  44. settime <time> Set daemon time
  45. (e.g. Sep 25, 2015 16:30:05 or 16:30:05)
  46. NTP access:
  47. accheck <address> Check whether address is allowed
  48. clients Report on clients that have accessed the server
  49. serverstats Display statistics of the server
  50. allow [<subnet>] Allow access to subnet as a default
  51. allow all [<subnet>] Allow access to subnet and all children
  52. deny [<subnet>] Deny access to subnet as a default
  53. deny all [<subnet>] Deny access to subnet and all children
  54. local [options] Serve time even when not synchronised
  55. local off Don't serve time when not synchronised
  56. smoothtime reset|activate Reset/activate time smoothing
  57. smoothing Display current time smoothing state
  58. Monitoring access:
  59. cmdaccheck <address> Check whether address is allowed
  60. cmdallow [<subnet>] Allow access to subnet as a default
  61. cmdallow all [<subnet>] Allow access to subnet and all children
  62. cmddeny [<subnet>] Deny access to subnet as a default
  63. cmddeny all [<subnet>] Deny access to subnet and all children
  64. Real-time clock:
  65. rtcdata Print current RTC performance parameters
  66. trimrtc Correct RTC relative to system clock
  67. writertc Save RTC performance parameters to file
  68. Other daemon commands:
  69. cyclelogs Close and re-open log files
  70. dump Dump all measurements to save files
  71. rekey Re-read keys from key file
  72. shutdown Stop daemon
  73. Client commands:
  74. dns -n|+n Disable/enable resolving IP addresses to hostnames
  75. dns -4|-6|-46 Resolve hostnames only to IPv4/IPv6/both addresses
  76. timeout <milliseconds> Set initial response timeout
  77. retries <retries> Set maximum number of retries
  78. keygen [<id> [<type> [<bits>]]]
  79. Generate key for key file
  80. exit|quit Leave the program
  81. help Generate this help

因为chrony在启动的时候会立刻进行时间同步,所以chronyc的主要用途除了进行配置之外,就是查看当前的状态信息。

配置文件

  1. # Use public servers from the pool.ntp.org project.
  2. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  3. # 从下列服务器获取时间
  4. server 0.centos.pool.ntp.org iburst
  5. server 1.centos.pool.ntp.org iburst
  6. server 2.centos.pool.ntp.org iburst
  7. server 3.centos.pool.ntp.org iburst
  8. # Record the rate at which the system clock gains/losses time.
  9. driftfile /var/lib/chrony/drift
  10. # Allow the system clock to be stepped in the first three updates
  11. # if its offset is larger than 1 second.
  12. makestep 1.0 3
  13. # Enable kernel synchronization of the real-time clock (RTC).
  14. rtcsync
  15. # Enable hardware timestamping on all interfaces that support it.
  16. #hwtimestamp *
  17. # Increase the minimum number of selectable sources required to adjust
  18. # the system clock.
  19. #minsources 2
  20. # Allow NTP client access from local network.
  21. # 允许以下网段的设备访问此服务器的时间同步服务
  22. #allow 192.168.0.0/16
  23. # Serve time even if not synchronized to a time source.
  24. #local stratum 10
  25. # Specify file containing keys for NTP authentication.
  26. #keyfile /etc/chrony.keys
  27. # Specify directory for log files.
  28. logdir /var/log/chrony
  29. # Select which information is logged.
  30. #log measurements statistics tracking