当前位置: 首页 > news >正文

15.Linux时间管理

文章目录

  • Linux时间管理
    • 系统时间设置
      • date命令
      • hwclock 命令
      • timedatectl 命令
      • tzselect 命令
      • windows 自动对时
      • 自动对时-chronyd 服务
    • 部署时间服务器
      • 服务端
      • 客户端

Linux时间管理

系统时间设置

date命令

正常显示当前时间

[root@server ~ 11:47:29]# date
2025年 09月 16日 星期二 11:49:08 CST[root@server ~ 11:49:08]# LANG=en_US.utf8 date
Tue Sep 16 11:49:39 CST 2025# 设置为特定时间,字符串以英文格式表达
[root@server ~ 11:49:51]# date -s "2022年 11月 11日 星期四 11:30:10 CST"
date: 无效的日期"2022年 11月 11日 星期四 11:30:10 CST"[root@server ~ 11:50:37]# date -s 'Thu Nov 11 11:30:59 CST 2022'
20221111日 星期五 11:30:59 CST

hwclock 命令

# 读取硬件时钟
[root@server ~ 13:26:01]# hwclock -r
2025年09月16日 星期二 13时26分08秒  -0.192821 秒# 将硬件时钟时间设置与系统时间一致
# 或者使用`clock -w`命令
[root@server ~ 13:26:17]# hwclock -w# 将系统时间设置与硬件时钟时间一致
[root@server ~ 13:26:33]# hwclock -s

Linux 中 “硬件时钟” 和 “系统时钟” 有何区别?如何查看硬件时钟的当前时间?

答案:

  • 区别:硬件时钟(RTC,实时时钟)是主板上的时钟芯片,断电后由电池供电;系统时钟是操作系统运行时维护的时钟,依赖系统。
  • 查看硬件时钟:hwclockclock

timedatectl 命令

[root@server ~ 13:26:56]# timedatectl Local time: 二 2025-09-16 13:27:02 CSTUniversal time: 二 2025-09-16 05:27:02 UTCRTC time: 二 2025-09-16 05:27:02Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes
NTP synchronized: noRTC in local TZ: noDST active: n/a# RTC real time clock,也就是硬件时钟时间。
# NTP enabled: yes,代表对时服务chronyd应开机自启。# 关闭自动对时
[root@server ~ 13:27:12]# timedatectl set-ntp no
[root@server ~ 13:27:24]# timedatectl Local time: 二 2025-09-16 13:27:27 CSTUniversal time: 二 2025-09-16 05:27:27 UTCRTC time: 二 2025-09-16 05:27:27Time zone: Asia/Shanghai (CST, +0800)NTP enabled: no
NTP synchronized: noRTC in local TZ: noDST active: n/a[root@server ~ 13:27:43]# LANG=en_US.utf8
[root@server ~ 13:27:58]# timedatectl set-time '2022-11-10 11:42:54'# 如果自动对时未关闭,显示如下
[root@centos7 ~ 13:27:58]# timedatectl set-time '2022-11-10 11:42:54'
Failed to set time: Automatic time synchronization is enabled# 设置时区
[root@server ~ 11:43:22]# timedatectl set-timezone Asia/Shanghai

tzselect 命令

查询时区名称。

[root@centos7 ~ 14:22:53]# tzselect 
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.1) Africa2) Americas3) Antarctica4) Arctic Ocean5) Asia6) Atlantic Ocean7) Australia8) Europe9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.1) Afghanistan		  18) Israel		    35) Palestine2) Armenia		  19) Japan		    36) Philippines3) Azerbaijan		  20) Jordan		    37) Qatar4) Bahrain		  21) Kazakhstan	    38) Russia5) Bangladesh		  22) Korea (North)	    39) Saudi Arabia6) Bhutan		  23) Korea (South)	    40) Singapore7) Brunei		  24) Kuwait		    41) Sri Lanka8) Cambodia		  25) Kyrgyzstan	    42) Syria9) China		  26) Laos		    43) Taiwan
10) Cyprus		  27) Lebanon		    44) Tajikistan
11) East Timor		  28) Macau		    45) Thailand
12) Georgia		  29) Malaysia		    46) Turkmenistan
13) Hong Kong		  30) Mongolia		    47) United Arab Emirates
14) India		  31) Myanmar (Burma)	    48) Uzbekistan
15) Indonesia		  32) Nepal		    49) Vietnam
16) Iran		  33) Oman		    50) Yemen
17) Iraq		  34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1The following information has been given:ChinaBeijing TimeTherefore TZ='Asia/Shanghai' will be used.
Local time is now:	Mon Jul 28 16:03:22 CST 2025.
Universal Time is now:	Mon Jul 28 08:03:22 UTC 2025.
Is the above information OK?
1) Yes
2) No
#? 1You can make this change permanent for yourself by appending the lineTZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.Here is that TZ value again, this time on standard output so that you
can use the /bin/tzselect command in shell scripts:
Asia/Shanghai

该命令仅生成时区设置建议,不会自动应用,需手动将建议的时区配置(如TZ='Asia/Shanghai'; export TZ)添加到~/.bashrc等文件中生效。

windows 自动对时

在这里插入图片描述

自动对时-chronyd 服务

主配置路径:/etc/chrony.conf

立即同步时间:chronyc -a makestep

# 修改对时服务器
[root@centos7 ~ 14:27:31]# vim /etc/chrony.conf
# 与时间池对时
# 时间池是包含多个时间服务器的服务器组
pool 2.rocky.pool.ntp.org iburst# 与单个服务器 ntp.aliyun.com 对时
server ntp.aliyun.com iburst# 启用并启动chronyd服务
[root@centos7 ~ 14:32:50]# systemctl enable chronyd --now# 如果之前已经启动,需要重启
[root@centos7 ~ 14:33:00]# systemctl restart chronyd# 验证对时情况
[root@server ~ 14:33:33]# chronyc sources -v
210 Number of sources = 1.-- Source mode  '^' = server, '=' = peer, '#' = local clock./ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too 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               
===============================================================================
^* 203.107.6.88                  2   6    17    20   +468us[  +64us] +/-   22ms

补充:

  • chronyd是 CentOS 7 默认的 NTP(网络时间协议)客户端服务,用于同步系统时间与远程时间服务器。
  • 与传统ntpd的优势:占用资源更少,同步速度更快,在网络不稳定时表现更好,支持更小的时间调整粒度。

部署时间服务器

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

服务端

[root@server ~ 14:34:30]# vim /etc/chrony.conf
# 最后添加两条记录# 配置监听地址
bindaddress 10.1.8.10# 配置允许哪些网段主机同步
allow 10.1.8.0/24[root@server ~ 14:35:13]# systemctl restart chronyd# 停止防火墙服务
[root@server ~ 14:35:26]# systemctl stop firewalld.service

客户端

# 修改对时服务器
[root@client ~ 14:35:42]# vim /etc/chrony.conf
# 与单个服务器 10.1.8.10 对时
server 10.1.8.10 iburst[root@client ~ 14:36:00]# systemctl restart chronyd
[root@client ~ 14:36:13]# chronyc sources -v
210 Number of sources = 1.-- Source mode  '^' = server, '=' = peer, '#' = local clock./ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too 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.hxl.cloud              3   6   377    11  -7294us[  -15ms] +/-   22ms

文章转载自:

http://E8X8WfR8.mcjxq.cn
http://eQKR4ihn.mcjxq.cn
http://C9K3hjY4.mcjxq.cn
http://PUVKgSEf.mcjxq.cn
http://UY2nz4A5.mcjxq.cn
http://N1dnytIt.mcjxq.cn
http://Y2GwOWwU.mcjxq.cn
http://Qj4JFWjl.mcjxq.cn
http://WXIYBra1.mcjxq.cn
http://bJhwUtFB.mcjxq.cn
http://0p3WZySj.mcjxq.cn
http://JVsRowRn.mcjxq.cn
http://PKhv7m6m.mcjxq.cn
http://TI5S3ldD.mcjxq.cn
http://0mr3oZag.mcjxq.cn
http://I8MBZ4in.mcjxq.cn
http://jyaCaEd4.mcjxq.cn
http://R4KuiXZ6.mcjxq.cn
http://LTFWk7tE.mcjxq.cn
http://j8andKhM.mcjxq.cn
http://ZJn2shxU.mcjxq.cn
http://nVlDSWRB.mcjxq.cn
http://aUk1HiJC.mcjxq.cn
http://ipsfryqD.mcjxq.cn
http://1AztyELE.mcjxq.cn
http://t8iSnsgm.mcjxq.cn
http://umf5bZLd.mcjxq.cn
http://gjFlJDLJ.mcjxq.cn
http://GYbSYKEU.mcjxq.cn
http://YD83O0Ur.mcjxq.cn
http://www.dtcms.com/a/387219.html

相关文章:

  • Linux 系统中的 Crond 服务:定时任务管理全指南
  • JDBC学习笔记
  • LoRA翻译
  • Linux 内存管理章节十五:内核内存的侦探工具集:深入Linux内存调试与检测机制
  • Mysql-主从复制与读写分离
  • bevformer 網絡結構
  • MySQL 基础与实战操作
  • 系统架构设计(二)
  • 【Day 58】Redis的部署
  • UVM验证工具--gvim
  • 《C++ spdlog高性能日志库快速上手》
  • 代码随想录学习(二)——二分查找
  • 【代码随想录day 27】 力扣 53. 最大子序和
  • Zynq开发实践(SDK之第一个纯PS工程)
  • 【Spring生态】Spring Cloud
  • HarmonyOS应用拉起系列(三):如何直接拉起腾讯/百度/高德地图进行导航
  • Redis的主从库与切片集群机制
  • 打工人日报#20250916
  • WASM逆向
  • 如何计算最大公约数和最小公倍数
  • 我们设计时间戳的更新时间的时候通常将字段类型设置为int或者bigint 这样能避免2038的问题吗
  • 超越“防被告”:2025跨境电商IPR战略赋能与品牌升值之道
  • Scrapy进阶:POST请求模拟登录实战与管道的使用
  • Zabbix 7.0 配置钉钉告警
  • 知识拓展-智能体和数字人
  • 飞牛NAS部署影视站MooncakeTV
  • yolov8 和OPENCV 自带的目标检测模型 对比
  • 课前练习题-20250916-复习题
  • 基于Transformer-卷积神经网络和度量元学习的高压断路器小样本机械故障诊断
  • 基于 Rust 的 IoT 平台基础功能设计(一)