Linux/Debian 系统时间校准方法简记
时区修正
前提时区要一致,不一致进行修改。
1.查看当前时区:timedatectl
2.时区不一致时候时区改为亚洲上海:timedatectl set-timezone Asia/Shanghai
一、非联网时,手动进行时间校准
查看硬件时间是否与系统时间一致,再和正确的时间对比。
hwclock --show && date
方式一:将系统时间同步到硬件时间
1、查看当前时间:date
2、手动设置系统时间:date -s "20251121 14:38:09"
3、将系统时间同步到硬件时间:hwclock -w或hwclock --systohc
方式二:将硬件时间同步到系统时间
1、查看硬件时间:hwclock --show
2、修改硬件时间:hwclock --set --date '20251121 14:38:09'
3、硬件时间和系统时间相互同步:hwclock -s
二、联网时,通过 NTP 服务进行时间校准
1.Debian 系统
1.查看 ntp 服务版本信息:ntpq -c version
2.若未安装 ntp 服务,执行命令安装:apt-get install ntp ntpdate
3.查看 ntp 服务状态:systemctl status ntp或netstat -tlunp | grep ntp
4.启动 ntp 服务:service ntp start(停止 ntp 服务命令:service ntp stop或lsof -i:123 + kill -9 PID)
5.执行/usr/sbin/ntpdate 时钟服务器 ip 或域名或ntpdate 时钟服务器 ip 或域名
时钟服务器 ip 或域名例如:192.168.100.2,ntp.neu.edu.cn,ntp.aliyun.com,ntp.ntsc.ac.cn
2.Linux 系统
1、安装 NTP 服务:sudo yum install ntp
2、启动并设置 NTP 服务开机自启:sudo systemctl enable ntpd && sudo systemctl start ntpd
3、验证时间同步状态:ntpd -p
4、修改 NTP 配置文件:sudo vim /etc/ntp.conf或者手动同步时间:sudo ntpd ntp.aliyun.com
5、系统时间同步到硬件时间:hwclock --systohc
6、查看硬件时间是否与系统时间一致:hwclock --show && date
或者直接编写执行以下脚本文件
vi ntp.sh
#!/bin/bash timedatectl set-timezone Asia/Shanghai && yum -y install ntp ntpdate > /dev/null && sed -i 's/server 0.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g' /etc/ntp.conf && sed -i 's/server 1.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g' /etc/ntp.conf && sed -i 's/server 2.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g' /etc/ntp.conf && sed -i 's/server 3.centos.pool.ntp.org iburst/#server 0.centos.pool.ntp.org iburst/g' /etc/ntp.conf && echo "server ntp.aliyun.com iburst" | tee -a /etc/ntp.conf > /dev/null && systemctl start ntpd && systemctl enable ntpd && systemctl status ntpd && ntpdate ntp.aliyun.com && hwclock --systohc && ntpq -p


