Linux 时间管理:date、hwclock、timedatectl 与时区
- 1Linux 系统概述:历史、发行版与 Ubuntu
- 2Linux引入
- 3Ubuntu Linux 安装:镜像、虚拟机与初始化
- 4Linux之系统服务
- 5Shell 基础:命令行与常用命令
- 6Linux之进程管理
- 7Vim 编辑器:模式、编辑与配置
- 8Linux之计划任务
- 9Linux 环境变量:作用域、配置与生效
- 10Linux初识
- 11Linux 软件包管理:RPM、DEB 与 APT
- 12Linux之网络
- 13Linux 用户与权限基础
- 14Linux之Shell
- 15Linux 文件管理:目录、文件类型、权限与常用操作
- 16Linux之文件管理
- 17Linux 磁盘管理:分区、格式化、挂载与空间查看
- 18Linux之用户管理
- 19Linux 进程管理:ps、kill、pstree 与作业控制
- 20Linux之基本权限
- 21Linux 网络管理:接口、路由、连通性与 NetworkManager
- 22Linux之特殊权限
- 23Linux 时间管理:date、hwclock、timedatectl 与时区本文
- 24Linux之软件管理
- 25Linux 系统服务管理:SysV init 与 systemd
- 26修改Linux登录时提示信息
- 27Shell 编程:变量、流程控制、函数与脚本
- 28【VIM编辑功能总结】
- 29Makefile 与 CMake:构建自动化基础
- 30简洁好用的Linux运维管理面板
- 31Linux 常用服务搭建:TFTP、NFS、Samba 与 SSH
- 32Linux 安装 fd , 替代 find 命令
- 33Linux知识点总结
【一】Linux时间介绍
Linux 时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称 RTC)时钟。
在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。
当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。
多数用户态命令和程序读取系统时钟。常用查看/设置工具包括 date、hwclock 和 timedatectl;clock 是一些系统中保留的旧命令或 hwclock 的兼容名称。
【二】Linux时间设置命令
【1】date 显示与设置时间命令
date 用于显示或设置系统日期与时间。手动修改时间会影响日志、TLS 证书、构建系统和网络服务;生产环境通常应使用 NTP 同步而非频繁手工设置。
运维人员可以根据想要的格式来输出系统时间信息,时间格 MMDDhhmm[CC][YY][.ss] ,其中 MM 为月份,DD 为日,hh 为小时,mm 为分钟,CC 为年份前两位数字,YY 为年份后两位数字,ss 为秒数。
语法格式:date [选项] [+输出形式]
常用参数:
-d <字符串>:按给定字符串解析并显示时间,不修改系统时间。-s <字符串>:设置系统时间,需要管理员权限。-u:以 UTC 显示或设置时间。+<格式>:自定义输出格式,例如+%F、+%T。
参考实例
linux@ubuntu:~$ date2022年 08月 26日 星期五 15:41:15 CSTlinux@ubuntu:~$ date "+%Y-%m-%d"2022-08-26linux@ubuntu:~$ date "+%Y-%m-%d %H:%M:%S"2022-08-26 15:43:11linux@ubuntu:~$ sudo date -s "2022-08-27 18:10:30"[sudo] linux 的密码:2022年 08月 27日 星期六 18:10:30 CSTlinux@ubuntu:~$ date2022年 08月 27日 星期六 18:10:31 CST若启用了自动时间同步,服务可能随后校正手工设置的时间。可先用 timedatectl status 检查同步状态;排障时临时关闭同步后,完成操作应及时恢复。
【2】hwclock命令
hwclock命令是一个硬件时钟访问工具,它可以显示当前硬件时间、设置硬件时钟的时间和设置硬件时钟为系统时间,也可设置系统时间为硬件时钟的时间。
语法格式:hwclock [参数]
常用参数: —debug 显示hwclock执行时详细的信息。 —directisa hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时 钟。 —hctosys 将系统时钟调整为与目前的硬件时钟一致。 —set —date=“日期与时间” 设定硬件时钟。
—show 显示硬件时钟的时间与日期。 —systohc 将硬件时钟调整为与目前的系统时钟一致。 —test 仅测试程序,而不会实际更改硬件时钟。 —utc 若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作。 —version 显示版本信息。
参考实例
linux@ubuntu:~$ sudo hwclock # 查看硬件时间2023-08-10 12:00:07.760036+0800linux@ubuntu:~$ sudo hwclock --hctosys # 把硬件时钟写入系统时钟linux@ubuntu:~$ date2023年 08月 10日 星期四 12:00:08 CSTlinux@ubuntu:~$ sudo hwclock2022-08-26 16:02:40.743598+0800linux@ubuntu:~$ sudo hwclock --set --date="2023-08-10 12:00:00" # 关闭网络和自动校正时间选项 , 并设置时间linux@ubuntu:~$ sudo hwclock2023-08-10 12:00:05.009811+0800linux@ubuntu:~$ sudo hwclock --hctosys # hwclock to system:硬件时间同步到系统时间linux@ubuntu:~$ date # 系统时间改变2023年 08月 10日 星期四 12:00:19 CSTlinux@ubuntu:~$ sudo date -s "2022-08-27 18:10:30" #需要使用sudo[sudo] linux 的密码:2022年 08月 27日 星期六 18:10:30 CSTlinux@ubuntu:~$ sudo hwclock2023-08-10 12:01:11.103429+0800linux@ubuntu:~$ sudo hwclock --systohc # system to hwclock 把系统时间同步到硬件时间内linux@ubuntu:~$ sudo hwclock2022-08-27 18:11:01.400677+0800【3】tzselect时区的设置命令
tzselect : time zone select
tzselect 会交互式推荐一个 TZ 值,但不会直接修改系统时区。它适合查询;在使用 systemd 的发行版上,设置系统时区优先使用 timedatectl set-timezone。
语法格式:tzselect [参数]
常用参数: -c COORD 根据自定义数值设置时区 -n LIMIT 当使用-c时,显示大多数LIMIT位置(默认为10)
参考实例
linux@ubuntu:~$ tzselectPlease identify a location so that time zone rules can be set correctly.Please select a continent, ocean, "coord", or "TZ".1) Africa2) Americas3) Antarctica4) Asia5) Atlantic Ocean6) Australia7) Europe8) Indian Ocean9) Pacific Ocean10) coord - I want to use geographical coordinates.11) TZ - I want to specify the time zone using the Posix TZ format.#? 4Please select a country whose clocks agree with yours.1) Afghanistan 18) Iraq 35) Pakistan2) Antarctica 19) Israel 36) Palestine3) Armenia 20) Japan 37) Philippines4) Azerbaijan 21) Jordan 38) Qatar5) Bahrain 22) Kazakhstan 39) Russia6) Bangladesh 23) Korea (North) 40) Saudi Arabia7) Bhutan 24) Korea (South) 41) Singapore8) Brunei 25) Kuwait 42) Sri Lanka9) Cambodia 26) Kyrgyzstan 43) Syria10) China 27) Laos 44) Taiwan11) Cyprus 28) Lebanon 45) Tajikistan12) East Timor 29) Macau 46) Thailand13) Georgia 30) Malaysia 47) Turkmenistan14) Hong Kong 31) Mongolia 48) United Arab Emirates15) India 32) Myanmar (Burma) 49) Uzbekistan16) Indonesia 33) Nepal 50) Vietnam17) Iran 34) Oman 51) Yemen#? 10Please select one of the following time zone regions.1) Beijing Time2) Xinjiang Time#? 1The following information has been given:ChinaBeijing TimeTherefore TZ='Asia/Shanghai' will be used.Selected time is now: 2022年 08月 26日 星期五 16:20:08 CST.Universal Time is now: 2022年 08月 26日 星期五 08:20:08 UTC.Is the above information OK?1) Yes2) No#? 1You can make this change permanent for yourself by appending the lineTZ='Asia/Shanghai'; export TZto 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 youcan use the /usr/bin/tzselect command in shell scripts:Asia/Shanghailinux@ubuntu:~$ sudo timedatectl set-timezone Asia/Shanghai # 持久设置系统时区linux@ubuntu:~$ timedatectl status【4】NTP 时间同步与 ntpdate 命令
ntpdate 可向 NTP 服务器查询并一次性校正本地时间,但在许多发行版中已不再是推荐的长期同步方案。日常同步应使用 systemd-timesyncd、chrony 或发行版提供的 NTP 服务,避免多个同步服务同时修改时钟。
长期同步服务会持续选择时间源并平滑校正时钟。应优先使用组织或发行版配置的可信 NTP 源,不要随意依赖来历不明的公网服务器。
语法格式:ntpdate [参数]
常用参数: -aKeyid 使用 Keyid 来认证全部数据包 -b 通过调用 settimeofday 子例程来增加时钟的时间
先检查并启用系统的自动时间同步:
timedatectl statussudo timedatectl set-ntp true以下为仅在需要一次性校时、且确认系统没有其他时间同步服务竞争时使用的 ntpdate 示例:
linux@ubuntu:~$ ntpdate找不到命令 “ntpdate”,但可以通过以下软件包安装它:sudo apt install ntpdate # version 1:4.2.8p15+dfsg-1ubuntu2, orsudo apt install ntpsec-ntpdate # version 1.2.1+dfsg1-4linux@ubuntu:~$ sudo apt install ntpdate # 安装 ntpdate 工具包
linux@ubuntu:~$ sudo ntpdate ntp.tencent.com24 Sep 20:09:15 ntpdate[5685]: step time server 106.55.184.199 offset -31598117.776736 seclinux@ubuntu:~$ date2024年 09月 24日 星期二 20:10:15 CSTlinux@ubuntu:~$ sudo date -s "2023-10-01 12:00:00"2023年 10月 01日 星期日 12:00:00 CSTlinux@ubuntu:~$ date2023年 10月 01日 星期日 12:00:01 CSTlinux@ubuntu:~$ sudo ntpdate ntp.tencent.com24 Sep 20:10:56 ntpdate[5751]: step time server 106.55.184.199 offset +31047042.458986 seclinux@ubuntu:~$ date2024年 09月 24日 星期二 20:10:58 CST【补充】验证与故障排查
用 timedatectl status 核对时区、系统时间与同步状态。自动同步开启时不要同时运行多套校时服务;时间仍漂移时检查所用 NTP 服务日志、网络连通性及虚拟机宿主时间。
参考资料
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!














