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'
2022年 11月 11日 星期五 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,实时时钟)是主板上的时钟芯片,断电后由电池供电;系统时钟是操作系统运行时维护的时钟,依赖系统。
- 查看硬件时钟:
hwclock
或clock
。
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