【运维】Linux/Debian系统时间校准方法简记

Linux/Debian系统时间校准方法简记

在linux系统时间有误差时,需要校准时间。

时区修正

前提时区要一致,不一致进行修改

1.查看当前时区:timedatectl

2.时区不一致时候时区改为亚洲上海:timedatectl set-timezone Asia/Shanghai

一、非联网时,手动进行时间校准

查看硬件时间是否与系统时间一致,再和正确的时间对比。

复制代码
hwclock --show && date

方式一:将系统时间同步到硬件时间

1、查看当前时间:date

2、手动设置系统时间:date -s "20251121 14:38:09"

3、将系统时间同步到硬件时间:hwclock -whwclock --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 ntpnetstat -tlunp | grep ntp

4.启动ntp服务:service ntp start(停止ntp服务命令:service ntp stoplsof -i:123 + kill -9 PID

5.执行/usr/sbin/ntpdate 时钟服务器ip或域名ntpdate 时钟服务器ip或域名

时钟服务器ip或域名例如:192.168.100.2ntp.neu.edu.cnntp.aliyun.comntp.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

或者直接编写执行以下脚本文件

xml 复制代码
vi ntp.sh
xml 复制代码
#!/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  

三、定时校准Linux服务器时间

上面的命令是可以校准,但是每天都要执行(发现测试机每天2秒多的误差),怎么可以一劳永逸的解决问题?

可以使用/etc/crontab实现定时校准时间,这个文件是linux的系统定时调度文件,在系统启动后会加载这个文件。

输入vi /etc/crontab编辑该文件,在这个文件最后加入0 16 * * * root ntpdate ntp1.aliyun.com,就可以每天16:00准时以root用户权限,执行ntpdate ntp1.aliyun.com命令。

xml 复制代码
命令格式:`* * * * * user-name command to be executed`
命令说明:
第一个`*`表示:分钟  
第二个`*`表示:小时  
第三个`*`表示:一个月的哪一天  
第四个`*`表示:月份  
第五个`*`表示:一周的哪一天  
`user-name`表示:要执行命令的用户  
其中要表示任何一个都可以就直接用\*表示。前面五个位置以后就表示要执行的命令和参数。
相关推荐
小成202303202654 小时前
Linux高级02
linux·开发语言
mounter6254 小时前
【硬核前沿】CXL 深度解析:重塑数据中心架构的“高速公路”,Linux 内核如何应对挑战?-- CXL 协议详解与 LSF/MM 最新动态
linux·服务器·网络·架构·kernel
++==4 小时前
Linux 进程间通信与线程同步技术详解:IPC 机制、线程 API、同步工具与经典同步问题
linux
特长腿特长5 小时前
centos、ubantu系列机的用户和用户组的结构是什么?具体怎么配置?用户组权限怎么使用?这篇文章持续更新,帮助你复习linux的基础知识
linux·运维·centos
zzzyyy5385 小时前
Linux环境变量
linux·运维·服务器
pluvium275 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
无级程序员5 小时前
centos7 安装 llvm-toolset-7-clang出错的问题解决
linux·centos
赛博云推-Twitter热门霸屏工具6 小时前
Twitter运营完整流程:从0到引流获客全流程拆解(2026)
运维·安全·自动化·媒体·twitter
CHHC18806 小时前
NetCore树莓派桌面应用程序
linux·运维·服务器
帮我吧智能服务平台7 小时前
装备制造智能制造升级:远程运维与智能服务如何保障产线OEE
运维·服务器·制造