资讯中心

16.Linux 时间管理(从零开始学)

📅 2026/7/24 21:15:44
16.Linux 时间管理(从零开始学)
在开启一天的学习的时候先做好快照过程中如果出现意外的报错解决不了的就立即恢复快照作为初学者省时省力不要过于纠结哪里错了浪费时间1.系统时间设置1.1 date 命令# 设置语言为英语 [rootserver ~ 20:09:06]# LANGen_US.utf8 date Fri Jul 24 20:15:23 CST 2026 # 中文语言代码为zh_CN.utf-8 [rootserver ~ 20:15:23]# LANGzh_CN.utf8 date 2026年 07月 24日 星期五 20:15:41 CST # 设置为特定时间时间字符串必须是英文格式 [rootserver ~ 20:15:41]# date -s 2022年 11月 11日 星期四 11:30:10 CST date: 无效的日期2022年 11月 11日 星期四 11:30:10 CST [rootserver ~ 20:17:19]# date -s Thu Nov 11 11:30:10 CST 2026年 11月 11日 星期三 11:30:10 CST [rootserver ~ 11:30:10]# date 2026年 11月 11日 星期三 11:30:13 CST1.2 hwclock 命令# 读取硬件时钟 [rootserver ~ 11:30:13]# hwclock -r 2026年07月24日 星期五 20时18分42秒 -0.797149 秒 # 将硬件时钟时间设置与系统时间一致 [rootserver ~ 11:30:42]# hwclock -w # 将系统时间设置与硬件时钟时间一致 [rootserver ~ 11:31:01]# hwclock -s1.3 timedatectl 命令[rootserver ~ 11:31:06]# timedatectl Local time: 三 2026-11-11 11:31:34 CST Universal time: 三 2026-11-11 03:31:34 UTC RTC time: 三 2026-11-11 03:31:34 Time zone: Asia/Shanghai (CST, 0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a # RTC real time clock也就是硬件时钟时间。 # NTP enabled: yes代表对时服务chronyd应开机自启。 # 关闭自动对时 [rootserver ~ 11:31:34]# timedatectl set-ntp no [rootserver ~ 11:32:22]# timedatectl Local time: 三 2026-11-11 11:32:24 CST Universal time: 三 2026-11-11 03:32:24 UTC RTC time: 三 2026-11-11 03:32:24 Time zone: Asia/Shanghai (CST, 0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a [rootserver ~ 11:32:24]# timedatectl set-time 2022-11-10 11:42:52 # 如果自动对时未关闭显示如下 [rootserver ~ 11:42:56]# timedatectl set-ntp yes [rootserver ~ 11:44:27]# timedatectl set-time 2022-11-10 11:42:52 Failed to set time: Automatic time synchronization is enabled # 设置时区 [rootserver ~ 20:24:29]# timedatectl set-timezone Asia/Shanghai1.4 tzselect 命令查询时区名称。[rootserver ~ 20:24:46]# tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) 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) Palestine 2) Armenia 19) Japan 36) Philippines 3) Azerbaijan 20) Jordan 37) Qatar 4) Bahrain 21) Kazakhstan 38) Russia 5) Bangladesh 22) Korea (North) 39) Saudi Arabia 6) Bhutan 23) Korea (South) 40) Singapore 7) Brunei 24) Kuwait 41) Sri Lanka 8) Cambodia 25) Kyrgyzstan 42) Syria 9) 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 #? 1 The following information has been given: China Beijing Time Therefore TZAsia/Shanghai will be used. Local time is now: Fri Jul 24 20:25:29 CST 2026. Universal Time is now: Fri Jul 24 12:25:29 UTC 2026. Is the above information OK? 1) Yes 2) No #? 1 You can make this change permanent for yourself by appending the line TZAsia/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/Shanghai1.5 windows 自动对时1.6 自动对时-chronyd 服务# 安装软件包 [rootserver ~ 20:25:35]# yum install chrony 已加载插件fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 软件包 chrony-3.4-1.el7.x86_64 已安装并且是最新版本 无须任何处理 # 修改对时服务器 [rootcentos7 ~]# vim /etc/chrony.conf # 与时间池对时 # 时间池是包含多个时间服务器的服务器组 3 server 0.centos.pool.ntp.org iburst 4 server 1.centos.pool.ntp.org iburst 5 server 2.centos.pool.ntp.org iburst 6 server 3.centos.pool.ntp.org iburst # 与单个服务器 ntp.aliyun.com 对时 server ntp.aliyun.com iburst # 启用并启动chronyd服务 [rootserver ~ 20:28:57]# systemctl enable chronyd --now # 如果之前已经启动需要重启 [rootserver ~ 20:29:39]# systemctl restart chronyd # 验证对时情况 [rootserver ~ 20:29:53]# 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 -1367us[ -55ms] /- 22ms1.7 补充vim 高级用法vim 高级用法可视化模式 功能1批量增加内容例如注释多行内容光标定位到目标位置ctrlv向下移动光标按需选中多行I 进行插入写入#esc功能2列删除例如批量解除注释1. 光标定位到目标位置ctrlv2. 向下移动光标按需选中多行3. x或d功能3列复制并粘贴例如批量增加主机名后缀1. 光标定位到目标位置ctrlv2. 向下移动光标可以上下左右同时移动选冲矩形块按需选中3. y复制4. 光标定位到目标位置p粘贴2.部署时间服务器chrony 既可以作为客户端也可以作为服务端为客户端提供对时服务。2.1 服务端[rootserver ~ 20:30:19]# vim /etc/chrony.conf # 最后添加两条记录 # 配置监听地址 bindaddress 10.1.8.10 # 配置允许哪些网段主机同步 allow 10.1.8.0/24 [rootserver ~ 20:34:04]# systemctl restart chronyd # 停止防火墙服务 [rootserver ~ 20:35:12]# systemctl stop firewalld.service必须确保服务端时间的准确性、有效性。2.2 客户端# 修改对时服务器 [rootclient ~ 20:36:07]# vim /etc/chrony.conf # 与单个服务器 10.1.8.10 对时注释掉其他的服务器 3 # server 0.centos.pool.ntp.org iburst 4 # server 1.centos.pool.ntp.org iburst 5 # server 2.centos.pool.ntp.org iburst 6 # server 3.centos.pool.ntp.org iburst 7 server 10.1.8.10 iburst [rootclient ~ 20:37:10]# systemctl restart chronyd [rootclient ~ 20:37:26]# 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.fengkai.cloud 3 6 17 8 75us[ -124us] /- 30ms3.一句话总结date、timedatectl用来查看 / 手动修改系统时间与时区hwclock负责同步系统时间与主板硬件 RTC 时钟必须关闭 chronyd 自动对时才能手动改时间chronyd既是客户端对接公网 NTP 服务器校准本机时间也可配置成内网时间服务器给其他主机提供时钟同步搭配 vim 块编辑可以快速批量修改配置文件内容统一集群所有服务器时间一致是业务日志、集群服务正常运行的基础保障。