NTP 意即网络时间协议Network Time Protocol,它经过网络同步计算机系统之间的时钟。NTP 服务器可使组织中的全部服务器保持同步,以准确时间执行基于时间的做业。NTP 客户端会将其时钟与 NTP 服务器同步。
一.验证NTP设置同步是否正常工作的方法,可使用ntpq、ntpstat、timedatectl命令。
一、使用命令 ntpq -p 查看状态
检查同步状态
[root@a /]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
xpingless.com 36.224.68.195 2 u 28 64 363 225.252 10.164 12.366
使用NTP服务时间同步
1.安装ntp
?yum -y install ntp
2.启动ntp服务
systemctl start ?ntpd
systemctl enable ?ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
3.查看ntp服务
systemctl status ntpd
4.修改ntp.conf文件
[root@a /]# vim /etc/ntp.conf
[root@a /]# grep server /etc/ntp.conf
# Use public servers from the pool.ntp.org project.
#server 0.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcast 224.0.1.1 autokey # multicast server
#manycastserver 239.255.254.254 # manycast server
5.重启服务
[root@a /]# systemctl restart ntpd
6.检查同步状态
[root@a /]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
xpingless.com 36.224.68.195 2 u 28 64 363 225.252 10.164 12.366
二、使用命令 ntpstat?查看状态
如果提示
-bash: ntpstat?: command not found
yum -y install ntpstat
[root@a /]# ntpstat
synchronised to NTP server (192.168.1.8) at stratum 3
time correct to within 508 ms
polling server every 64 s
三、使用命令 timedatectl或者timedatectl status查看状态
[root@a /]# timedatectl
Local time: Fri 2022-10-28 15:32:35 CST
Universal time: Fri 2022-10-28 07:32:35 UTC
RTC time: Fri 2022-10-28 07:32:35
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
四、关于NTP客户端的替代品Chrony
Chrony可以更快地同步系统时钟,提高时间精度,对于一直不在线的系统尤其有用,同时chronyd较小,它使用较少的内存,只在必要时才唤醒CPU,这样可以更好地节省电能,即使网络拥塞较长时间,它也能很好地运行。
可以使用以下任何命令来检查Chrony状态:
[root@a /]# chronyc tracking
Reference ID : 64643D58 (100.100.61.88)
Stratum : 2
Ref time (UTC) : Fri Oct 28 07:28:02 2022
System time : 0.000020753 seconds slow of NTP time
Last offset : +0.000098698 seconds
RMS offset : 0.000065608 seconds
Frequency : 41.289 ppm slow
Residual freq : +0.000 ppm
Skew : 0.004 ppm
Root delay : 0.000177195 seconds
Root dispersion : 0.010756997 seconds
Update interval : 1031.0 seconds
Leap status : Normal
?运行sources命令以显示有关当前时间源的信息:
[root@a /]# chronyc sources
210 Number of sources = 15
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 120.25.115.20 2 10 277 245 +821us[ +821us] +/- 19ms
^? 10.143.33.49 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.3.1 2 10 377 699 +4361us[+4459us] +/- 12ms
^+ 100.100.3.2 2 10 377 728 -273us[ -175us] +/- 8575us
^+ 100.100.3.3 2 10 325 467 -263us[ -263us] +/- 8956us
^+ 203.107.6.88 2 4 377 8 -462us[ -462us] +/- 6250us
^? 10.143.33.50 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.33.51 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.44 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.45 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.46 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.5.1 2 10 377 875 -60us[ +38us] +/- 14ms
^+ 100.100.5.2 2 10 377 799 +2370us[+2468us] +/- 12ms
^+ 100.100.5.3 2 10 377 566 -2313us[-2313us] +/- 12ms
^* 100.100.61.88 1 10 377 587 +249us[ +348us] +/- 10ms
[root@a /]# chronyc sources -v
210 Number of sources = 15
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 120.25.115.20 2 10 277 260 +821us[ +821us] +/- 19ms
^? 10.143.33.49 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.3.1 2 10 377 710 +4361us[+4459us] +/- 12ms
^+ 100.100.3.2 2 10 377 739 -273us[ -175us] +/- 8575us
^+ 100.100.3.3 2 10 325 478 -263us[ -263us] +/- 8956us
^+ 203.107.6.88 2 4 377 3 -685us[ -685us] +/- 6322us
^? 10.143.33.50 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.33.51 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.44 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.45 0 10 0 - +0ns[ +0ns] +/- 0ns
^? 10.143.0.46 0 10 0 - +0ns[ +0ns] +/- 0ns
^+ 100.100.5.1 2 10 377 887 -60us[ +38us] +/- 14ms
^+ 100.100.5.2 2 10 377 811 +2370us[+2468us] +/- 12ms
^+ 100.100.5.3 2 10 377 578 -2313us[-2313us] +/- 12ms
^* 100.100.61.88 1 10 377 599 +249us[ +348us] +/- 10ms
|