
如果您在麒麟操作系统中发现系统时间与实际时间存在偏差,且未自动校准,则可能是NTP时间同步服务未启用或配置不当。以下是启用自动同步系统时间的具体操作步骤:
一、启用并配置systemd-timesyncd服务
systemd-timesyncd是麒麟桌面系统默认内置的轻量级NTP客户端,适用于大多数终端用户场景,无需额外安装,仅需启用并指定可用NTP服务器即可生效。
1、编辑timesyncd配置文件:执行命令 sudo nano /etc/systemd/timesyncd.conf
2、取消NTP行注释并修改为可信服务器:将 #NTP= 行改为 NTP=ntp1.aliyun.com ntp2.aliyun.com
3、保存退出后重启服务:执行命令 sudo systemctl restart systemd-timesyncd
4、验证服务状态:执行命令 timedatectl status,确认“Network time on: yes”及“NTP synchronized: yes”显示为激活状态
二、安装并启用chrony服务(推荐用于稳定性要求高的环境)
chrony比systemd-timesyncd更适应网络波动、虚拟机时钟漂移等复杂场景,支持更快收敛和更精准同步,适用于服务器或关键业务终端。
1、安装chrony包:执行命令 sudo apt update && sudo apt install chrony
2、备份原始配置:执行命令 sudo cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak
3、编辑主配置文件:执行命令 sudo nano /etc/chrony/chrony.conf
4、注释所有默认server行,并添加国内可靠源:插入以下三行(每行独立):server ntp1.aliyun.com iburstserver ntp2.aliyun.com iburstserver time1.aliyun.com iburst
5、重启chrony服务:执行命令 sudo systemctl restart chronyd
6、检查同步源状态:执行命令 chronyc sources -v,确认输出中某一行以 ^* 开头
三、安装并启用ntpd服务(传统方式,兼容旧应用依赖)
ntpd是经典NTP守护进程,部分遗留软件或脚本可能显式调用ntpdate或依赖ntpd的运行状态,适用于需保持历史兼容性的部署环境。
1、安装ntp套件:执行命令 sudo apt install ntp
2、停止并禁用systemd-timesyncd(二者互斥):执行命令 sudo systemctl stop systemd-timesyncd && sudo systemctl disable systemd-timesyncd
3、编辑ntp配置文件:执行命令 sudo nano /etc/ntp.conf
4、注释全部pool行,添加如下内容:server ntp1.aliyun.com iburst preferserver ntp2.aliyun.com iburst
5、重启服务并检查状态:执行命令 sudo systemctl restart ntp && sudo systemctl status ntp
6、手动触发一次同步验证:执行命令 sudo ntpdate -q ntp1.aliyun.com,确认输出含“offset”数值且绝对值小于100ms
四、同步硬件时钟(RTC)以保障关机后时间准确性
系统时间同步成功后,若不写入主板实时时钟(RTC),重启后仍将读取偏差较大的硬件时间,导致反复失准。该步骤应作为同步流程的必要补充。
1、确认当前系统时间已准确:执行命令 date,核对年月日时分秒是否与标准时间一致
2、将当前系统时间写入硬件时钟:执行命令 sudo hwclock –systohc
3、验证写入结果:执行命令 sudo hwclock –show,输出时间应与date命令结果基本一致(误差在±1秒内)
五、配置定时强制同步(应对偶发同步失败场景)
当网络临时中断或NTP服务短暂不可达时,系统可能维持旧时间。通过crontab设置周期性强制校准,可提升时间可靠性。
1、编辑当前用户定时任务:执行命令 sudo crontab -e
2、在文件末尾新增一行:*/15 * * * * /usr/bin/timedatectl set-ntp true && /usr/bin/sleep 5 && /usr/bin/timedatectl show-timesync –value | /usr/bin/grep -q “true” || /usr/bin/systemctl restart systemd-timesyncd
3、保存退出后,定时任务即刻生效,每15分钟检测一次NTP同步状态并自动恢复服务

评论(0)