1、查看时区
1. 查看本地时间
# date -RThu, 16 Apr 2026 15:36:20 +0800 //如果显示为UTC或+0000,说明时区未正确设置为东八区(如Asia/Shanghai)
2. 查看时区、时钟同步是否成功
# timedatectl Local time: Thu 2026-04-16 15:36:46 CST //显示本地时间 Universal time: Thu 2026-04-16 07:36:46 UTC RTC time: Thu 2026-04-16 07:36:46 //显示硬件实时时钟(RTC)的时间 Time zone: Asia/Shanghai (CST, +0800) //显示时区,如果为UTC或+0000,说明时区未正确设置为东八区(如Asia/Shanghai)。System clock synchronized: yes // 如果为no,这通常意味着系统时钟没有与任何已知的时间服务器同步成功。 NTP service: active RTC in local TZ: no //显示 RTC 是否设置为本地时区# cat /etc/timezone
2、检查时钟同步是否成功
# timedatectl Local time: Fri 2026-04-17 14:10:53 CST Universal time: Fri 2026-04-17 06:10:53 UTC RTC time: Fri 2026-04-17 05:46:26 Time zone: Asia/Shanghai (CST, +0800)System clock synchronized: no //没有和远程NTP服务器成功同步 NTP service: n/a //意味着NTP服务(如systemd-timesyncd)没有启动和运行 RTC in local TZ: yes //如果为no表示硬件时钟(RTC)设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间.
3、修改时区为东八区(北京时间)
方法1:使用timedatectl(推荐,适用于大多数现代Linux发行版)
查看可用时区列表# timedatectl list-timezones | grep -i shanghai设置时区为上海(东八区)# timedatectl set-timezone Asia/Shanghai# 验证下# timedatectl
方法2:手动创建符号链接(适用于旧版Linux)
# 删除旧的时区链接sudo rm -f /etc/localtime# 创建新的时区链接(指向上海时区)sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4、配置时钟同步
安装 ntpdate +配置定时任务,来与时钟服务器同步
或使用 ntpd 渐进式更新时间,来与时钟服务器同步
备注:systemd-timesyncd(和ntpdate)是断点更新,ntpd 为步进式的逐渐校正时间,不会出现时间跳变。另外有些应用程序可能对时间的任何干扰很敏感。ntpd使用复杂的技术来不断地、逐步地保持系统的时间。
无论采用那种方案,如果没有成功同步,就需要解决
# timedatectl ... System clock synchronized: no //如果不是yes,而是no,表示没有和远程时钟服务器成功同步 NTP service: n/a //显示为n/a,不是active,表示NTP服务没有激活,所以需要启动NTP服务
查看定时任务 # crontab -l#每5分钟执行一次,与时间服务器同步,并将系统时钟同步到硬件时钟*/5 * * * * /usr/sbin/ntpdate your-time-server-host-ip >/dev/null 2>&1; /usr/sbin/hwclock -w
1. 启动NTP服务
# timedatectl set-ntp on如果开启时出现错误提示 Failed to set ntp: NTP not supported,就要排查有没有安装systemd-timesyncd,安装后尝试再次开启TP服务# apt list --installed | grep systemd-timesyncd 或 systemctl status systemd-timesyncd# apt install systemd-timesyncd备注:systemd-timesyncd,它是一个用于跨网络同步系统时钟的守护服务。它实现了一个 SNTP 客户端,相比NTP,这个服务简单的多,它只专注于从远程服务器查询然后同步到本地时钟。systemd-timesyncd 只会更改系统时间不会更改硬件时间,可以通过hwclock -w命令将系统时间同步到硬件时间
2. NTP服务开启后,检查下
# timedatectl Local time: Fri 2026-04-17 15:28:00 CST Universal time: Fri 2026-04-17 07:28:00 UTC RTC time: Fri 2026-04-17 07:03:32 Time zone: Asia/Shanghai (CST, +0800)System clock synchronized: yes //为yes表示同步成功 NTP service: active RTC in local TZ: yesWarning: The system is configured to read the RTC time in the local time zone. This mode cannot be fully supported. It will create various problems with time zone changes and daylight saving time adjustments. The RTC time is never updated, it relies on external facilities to maintain it. If at all possible, use RTC in UTC by calling 'timedatectl set-local-rtc 0'.
3. 设置硬件时钟,默认为UTC,1设为本地时区的当前时间,0设为UTC时区的时间
设置硬件时钟为本地时间:timedatectl set-local-rtc 1设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
如果设置为1,会报如下警告。如果仅为了将时间改为本地时间,使用默认的 rtc=1,就行(不用改)Warning: The system is configured to read the RTC time in the local time zone. This mode cannot be fully supported. It will create various problems with time zone changes and daylight saving time adjustments. The RTC time is never updated, it relies on external facilities to maintain it. If at all possible, use RTC in UTC by calling 'timedatectl set-local-rtc 0'.
5、如果时间还是没有同步成功,则检查与时钟服务器的连通性
在客户机上连通时钟服务器,-u 来指定通讯协议UDP# nc -v -u 时钟服务器IP 123# nmap -p 123 -sU 时钟服务器IP
6、确认 ntpdate 是否成功同步了系统时间
方法1、使用 timedatectl
如果你的系统使用 systemd,可以使用 timedatectl 命令来查看当前的时间状态,包括是否启用了 NTP。# timedatectl Time zone: Asia/Shanghai (CST, +0800)System clock synchronized: yes //这一行,如果显示为 yes,则表示系统已经与远程时间服务器同步成功。 NTP service: active
方法2、查看 /var/log/syslog 或其他日志文件
# grep ntpdate /var/log/syslog
方法3、如果你客户机上,使用的是 ntpd 而不是 ntpdate,可以使用 ntpq -p
所有连接的 NTP 服务器及其状态,包括它们是否被选中作为同步源。
附-使用 ntpd 渐进式更新时间
systemd-timesyncd和ntpdate 是断点更新,ntpd 为步进式的逐渐校正时间,不会出现时间跳变。另外有些应用程序可能对时间的任何干扰很敏感。ntpd使用复杂的技术来不断地、逐步地保持系统的时间。
https://developer.aliyun.com/article/1141208
附-Chrony搭建全攻略
https://blog.csdn.net/qq_42204518/article/details/157555095