news 2026/7/24 23:35:43

Linux 时间管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux 时间管理

文章目录

  • Linux 时间管理
    • 系统时间设置
      • date 命令
      • hwclock 命令
      • timedatectl 命令
      • tzselect 命令
      • windows 自动对时
      • 自动对时-chronyd 服务
      • 补充:vim 高级用法
    • 部署时间服务器
      • 服务端
      • 客户端

Linux 时间管理

系统时间设置

date 命令

# 设置语言为英语[root@server ~13:27:02]# LANG=en_US.utf8 dateFri Jul2413:28:02 CST2026# 中文语言代码为zh_CN.utf-8# 设置为特定时间,时间字符串必须是英文格式[root@centos7 ~]# date -s '2022年 11月 11日 星期四 11:30:10 CST'date: 无效的日期"2022年 11月 11日 星期四 11:30:10 CST"[root@server ~13:28:02]# date -s 'Thu Nov 11 11:30:59 CST 2022'Fri Nov1111:30:59 CST2022

hwclock 命令

# 读取硬件时钟[root@server ~13:50:26]# hwclock -rFri24Jul202601:56:16 PM CST-0.942963seconds# 将硬件时钟时间设置与系统时间一致[root@server ~13:56:16]# hwclock -w# 将系统时间设置与硬件时钟时间一致[root@server ~13:56:34]# hwclock -s

timedatectl 命令

[root@server ~13:56:42]# timedatectlLocal time: Fri2026-07-2413:57:27 CST Universal time: Fri2026-07-24 05:57:27 UTC RTC time: Fri2026-07-24 05:57:27 Time zone: Asia/Shanghai(CST, +0800)NTP enabled:yesNTP synchronized: no RTCinlocalTZ: no DST active: n/a# RTC real time clock,也就是硬件时钟时间。# NTP enabled: yes,代表对时服务chronyd应开机自启。# 关闭自动对时[root@centos7 ~]# timedatectl set-ntp no[root@server ~11:37:51]# timedatectlLocal time: Fri2026-07-2411:37:57 CST Universal time: Fri2026-07-24 03:37:57 UTC RTC time: Fri2026-07-24 03:37:57 Time zone: Asia/Shanghai(CST, +0800)NTP enabled: no NTP synchronized: no RTCinlocalTZ: no DST active: n/a[root@centos7 ~]# timedatectl set-time '2026-7-24 11:42:54'# 如果自动对时未关闭,显示如下[root@server ~11:32:20]# timedatectl set-time '2026-7-24 11:42:54'Failed tosettime: Automatictimesynchronization is enabled# 设置时区[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai

tzselect 命令

查询时区名称。

[root@server ~13:41:53]# tzselectPlease identify a location so thattimezone rules can besetcorrectly. Pleaseselecta continent or ocean.1)Africa2)Americas3)Antarctica4)Arctic Ocean5)Asia6)Atlantic Ocean7)Australia8)Europe9)Indian Ocean10)Pacific Ocean11)none - I want to specify thetimezone using the Posix TZ format.#? 5Pleaseselecta country.1)Afghanistan18)Israel35)Palestine2)Armenia19)Japan36)Philippines3)Azerbaijan20)Jordan37)Qatar4)Bahrain21)Kazakhstan38)Russia5)Bangladesh22)Korea(North)39)Saudi Arabia6)Bhutan23)Korea(South)40)Singapore7)Brunei24)Kuwait41)Sri Lanka8)Cambodia25)Kyrgyzstan42)Syria9)China26)Laos43)Taiwan10)Cyprus27)Lebanon44)Tajikistan11)East Timor28)Macau45)Thailand12)Georgia29)Malaysia46)Turkmenistan13)Hong Kong30)Mongolia47)United Arab Emirates14)India31)Myanmar(Burma)48)Uzbekistan15)Indonesia32)Nepal49)Vietnam16)Iran33)Oman50)Yemen17)Iraq34)Pakistan#? 9Pleaseselectone of the followingtimezone regions.1)Beijing Time2)Xinjiang Time#? 1The following information has been given: China Beijing Time ThereforeTZ='Asia/Shanghai'will be used. Localtimeis now: Fri Jul2413:47:41 CST2026. Universal Time is now: Fri Jul2405:47:41 UTC2026. Is the above information OK?1)Yes2)No#? 1You canmakethis change permanentforyourself by appending the lineTZ='Asia/Shanghai';exportTZ to thefile'.profile'inyour home directory;thenlog out and loginagain. Here is that TZ value again, thistimeon standard output so that you can use the /bin/tzselectcommandinshell scripts: Asia/Shanghai

windows 自动对时

自动对时-chronyd 服务

# 安装软件包[root@centos7 ~]# yum install chrony# 修改对时服务器[root@centos7 ~]# vim /etc/chrony.conf# 与时间池对时# 时间池是包含多个时间服务器的服务器组pool2.rocky.pool.ntp.org iburst# 与单个服务器 ntp.aliyun.com 对时server ntp.aliyun.com iburst# 启用并启动chronyd服务[root@centos7 ~]# systemctl enable chronyd --now# 如果之前已经启动,需要重启[root@centos7 ~]# systemctl restart chronyd# 验证对时情况[root@centos7 ~]# chronyc sources -v.-- Source mode'^'=server,'='=peer,'#'=localclock. / .- Source state'*'=current best,'+'=combined,'-'=not combined,|/'x'=may beinerror,'~'=too variable,'?'=unusable.||.- 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===============================================================================^*203.107.6.8826779-1840us[-4378us]+/- 23ms

补充:vim 高级用法

vim 高级用法,可视化模式: 功能1:批量增加内容,例如注释多行内容

  1. 光标定位到目标位置,ctrl+v
  2. 向下移动光标,按需选中多行
  3. I 进行插入,写入#
  4. esc

功能2:列删除,例如批量解除注释 1. 光标定位到目标位置,ctrl+v 2. 向下移动光标,按需选中多行 3. x或d

功能3:列复制并粘贴,例如批量增加主机名后缀 1. 光标定位到目标位置,ctrl+v 2. 向下移动光标(可以上下左右同时移动,选冲矩形块),按需选中 3. y复制 4. 光标定位到目标位置,p粘贴

部署时间服务器

chrony 既可以作为客户端,也可以作为服务端(为客户端提供对时服务)。

服务端

[root@server ~]# vim /etc/chrony.conf# 最后添加两条记录# 配置监听地址bindaddress10.1.8.10# 配置允许哪些网段主机同步allow10.1.8.0/24[root@server ~]# systemctl restart chronyd# 停止防火墙服务[root@server ~]# systemctl stop firewalld.service

必须确保服务端时间准确性和有效性

客户端

# 修改对时服务器[root@client ~]# vim /etc/chrony.conf# 与单个服务器 10.1.8.10 对时server10.1.8.10 iburst[root@client ~]# systemctl restart chronyd[root@client ~13:40:02]# chronyc sources -v210Number of sources=5.-- Source mode'^'=server,'='=peer,'#'=localclock. / .- Source state'*'=current synced,'+'=combined ,'-'=not combined,|/'?'=unreachable,'x'=timemay beinerror,'~'=timetoo 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===============================================================================^* server.myz.cloud46170+2937us[-77us]+/- 260ms ^? ntp1.flashdance.cx060- +0ns[+0ns]+/- 0ns ^+111.230.189.1742671-998us[-4012us]+/- 49ms ^? a.chl.la2672+17ms[+19ms]+/- 107ms ^? ntp.wdc2.us.leaseweb.net3651-6707us[-4960us]+/- 114ms#验证,设置错误时间[root@client ~13:40:55]# sudo date -s 'Fri Jul 24 13:20:00 CST 2026'Fri Jul2413:20:00 CST2026#再次重启[root@client ~13:20:02]# systemctl restart chronyd#时间为正确时间[root@client ~13:41:42]# dateFri Jul2413:41:44 CST2026/- 114ms#验证,设置错误时间[root@client ~13:40:55]# sudo date -s 'Fri Jul 24 13:20:00 CST 2026'Fri Jul2413:20:00 CST2026#再次重启[root@client ~13:20:02]# systemctl restart chronyd#时间为正确时间[root@client ~13:41:42]# dateFri Jul2413:41:44 CST2026
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/24 23:34:32

从硬件到软件:深度解析ADS8353/7853 ADC评估板实战应用

1. 项目概述:从芯片到系统,如何用好一块ADC评估板 在信号链设计的漫长旅途中,模数转换器(ADC)的选择与评估往往是决定系统性能上限的关键一步。作为一名在工业测控领域摸爬滚打了十多年的工程师,我深知&…

作者头像 李华
网站建设 2026/7/24 23:34:28

直管200人AI底座负责人离职,小鹏自研芯片如何接棒

直管200人AI底座负责人离职,小鹏自研芯片如何接棒 最近从脉脉上看到一则消息:小鹏直管200人的AI Infra负责人离开,而自研芯片又被讨论者放在今年量产的时间压力里。这里必须先划清边界:这只是用户讨论和个人样本,信息有…

作者头像 李华
网站建设 2026/7/24 23:34:11

力扣-121-动态规划-买股票

1 你的直觉非常接近真相了,但有一个关键的“会计记账”差错! 如果不纠正这个差错,你写出的状态转移方程会把自己绕晕。 你提出的“计算股票的钱(现金市值)”和“不计算股票的钱(纯现金)”&#…

作者头像 李华
网站建设 2026/7/24 23:30:53

FastAPI做项目的后端准备

FastAPI做项目的后端准备 文章目录FastAPI做项目的后端准备前言一、理解框架之间的关系?二、Main里面的配置定义 lifespan 生命周期函数1. 引入必要的库代码说明:2. 多环境配置依赖相关文件一览三个环境对照表配置实现步骤1. 创建配置文件2. 创建环境文件…

作者头像 李华
网站建设 2026/7/24 23:27:56

大模型核心技术RAG讲解

一.RAG技术原理 1.什么是RAG? Retrieval-Augmented Generation (RAG-检索增强生成) 是帮助大模型更好地回答问题的一种方式,在大模型的应用场景中,一个非常常见的需求是:手里有一堆文档,当遇到一个问题时,希望通过其中…

作者头像 李华