311 lines
5.6 KiB
Markdown
311 lines
5.6 KiB
Markdown
## 1、时间管理概览
|
||
|
||
Linux 中的时间管理,核心需要分清 4 个概念:
|
||
|
||
- **系统时间**:当前操作系统正在使用的时间
|
||
- **硬件时钟**:主板上的 RTC 时间,也叫 BIOS 时间
|
||
- **时区**:决定时间以哪个地区标准显示
|
||
- **时间同步**:通过 NTP 等方式自动校准系统时间
|
||
|
||
平时排查“服务器时间不准”“日志时间对不上”“定时任务执行异常”等问题时,通常都绕不开这几个概念。
|
||
|
||
## 2、系统时钟与硬件时钟
|
||
|
||
Linux 中通常会涉及两种时间:
|
||
|
||
### 2.1 系统时钟
|
||
|
||
系统时钟是当前 Linux 内核运行时维护的时间。
|
||
|
||
- `date` 命令查看和设置的主要是**系统时间**
|
||
- 应用程序、日志、定时任务通常主要依赖系统时间
|
||
|
||
### 2.2 硬件时钟
|
||
|
||
硬件时钟是主板上的 RTC(Real-Time Clock)时间。
|
||
|
||
- 机器关机后,硬件时钟通常仍能继续走时
|
||
- 系统启动时,通常会从硬件时钟读取初始时间
|
||
- 现代 Linux 中通常使用 `hwclock` 查看硬件时钟
|
||
|
||
### 2.3 两者关系
|
||
|
||
可以简单理解为:
|
||
|
||
- **开机后主要使用系统时间**
|
||
- **硬件时钟更像底层基准时间**
|
||
- 系统时间和硬件时钟不一致时,可能会导致时间混乱
|
||
|
||
## 3、`date` 命令
|
||
|
||
`date` 是 Linux 中最常用的时间命令,用于显示、格式化、转换和设置系统时间。
|
||
|
||
### 3.1 查看当前时间
|
||
|
||
```shell
|
||
date
|
||
```
|
||
|
||
### 3.2 自定义格式输出
|
||
|
||
```shell
|
||
date +"%F %T"
|
||
```
|
||
|
||
输出示例:
|
||
|
||
```shell
|
||
2026-04-08 14:18:00
|
||
```
|
||
|
||
常见格式符:
|
||
|
||
- `%F`:完整日期,等价于 `%Y-%m-%d`
|
||
- `%Y`:四位年份
|
||
- `%m`:月份
|
||
- `%d`:日期
|
||
- `%T`:完整时间,等价于 `%H:%M:%S`
|
||
- `%H`:小时
|
||
- `%M`:分钟
|
||
- `%S`:秒
|
||
|
||
### 3.3 常用参数
|
||
|
||
- `-r`:显示指定文件的时间戳
|
||
- `-d`:进行时间计算或时间转换
|
||
- `-s`:设置系统时间
|
||
|
||
### 3.4 常见示例
|
||
|
||
#### 查看文件修改时间
|
||
|
||
```shell
|
||
date -r /etc/passwd
|
||
```
|
||
|
||
#### 时间转换
|
||
|
||
```shell
|
||
date -d "2026-04-08 08:00:00" +"%F %T"
|
||
```
|
||
|
||
#### 查看昨天的时间
|
||
|
||
```shell
|
||
date -d "yesterday" +"%F %T"
|
||
```
|
||
|
||
#### 设置系统时间
|
||
|
||
```shell
|
||
date -s "2026-04-08 14:30:00"
|
||
```
|
||
|
||
注意:手动设置时间通常需要 root 权限,并且在生产环境中要谨慎操作。
|
||
|
||
## 4、`hwclock` 命令
|
||
|
||
很多旧资料里会提到 `clock`,但在现代 Linux 中更常见的是 `hwclock`。
|
||
|
||
### 4.1 查看硬件时钟
|
||
|
||
```shell
|
||
hwclock
|
||
```
|
||
|
||
输出示例:
|
||
|
||
```shell
|
||
2026-04-08 14:20:00.123456+08:00
|
||
```
|
||
|
||
### 4.2 常见用途
|
||
|
||
#### 查看硬件时钟
|
||
|
||
```shell
|
||
hwclock --show
|
||
```
|
||
|
||
#### 把硬件时钟同步到系统时间
|
||
|
||
```shell
|
||
hwclock --hctosys
|
||
```
|
||
|
||
#### 把系统时间写入硬件时钟
|
||
|
||
```shell
|
||
hwclock --systohc
|
||
```
|
||
|
||
### 4.3 和 `date` 的区别
|
||
|
||
- `date`:查看或设置**系统时间**
|
||
- `hwclock`:查看或设置**硬件时钟**
|
||
|
||
如果系统时间和硬件时钟不一致,系统启动后可能出现时间偏差。
|
||
|
||
## 5、`timedatectl` 命令
|
||
|
||
在基于 systemd 的 Linux 发行版中,`timedatectl` 是时间管理的常用工具。
|
||
|
||
### 5.1 查看当前时间状态
|
||
|
||
```shell
|
||
timedatectl status
|
||
```
|
||
|
||
它通常可以显示:
|
||
|
||
- 本地时间
|
||
- 世界标准时间(UTC)
|
||
- RTC 时间
|
||
- 当前时区
|
||
- 是否启用 NTP 自动同步
|
||
|
||
### 5.2 常见子命令
|
||
|
||
```shell
|
||
timedatectl status
|
||
```
|
||
|
||
查看当前时间设置。
|
||
|
||
```shell
|
||
timedatectl show
|
||
```
|
||
|
||
查看 `systemd-timedated` 的详细属性。
|
||
|
||
```shell
|
||
timedatectl set-time "2026-04-08 14:30:00"
|
||
```
|
||
|
||
设置系统时间。
|
||
|
||
```shell
|
||
timedatectl set-timezone Asia/Shanghai
|
||
```
|
||
|
||
设置时区。
|
||
|
||
```shell
|
||
timedatectl list-timezones
|
||
```
|
||
|
||
列出系统支持的时区。
|
||
|
||
```shell
|
||
timedatectl set-local-rtc 0
|
||
```
|
||
|
||
设置 RTC 是否使用本地时间。一般 Linux 环境更常使用 UTC。
|
||
|
||
```shell
|
||
timedatectl set-ntp true
|
||
```
|
||
|
||
启用网络时间同步。
|
||
|
||
## 6、时区管理
|
||
|
||
时间正确不只取决于“几点几分”,还取决于系统使用的是哪个时区。
|
||
|
||
### 6.1 查看当前时区
|
||
|
||
```shell
|
||
timedatectl
|
||
```
|
||
|
||
### 6.2 设置时区
|
||
|
||
```shell
|
||
timedatectl set-timezone Asia/Shanghai
|
||
```
|
||
|
||
中国大陆服务器一般常设为:
|
||
|
||
```shell
|
||
Asia/Shanghai
|
||
```
|
||
|
||
### 6.3 为什么时区重要
|
||
|
||
如果时区设置错误,可能会出现:
|
||
|
||
- 日志时间和实际时间不一致
|
||
- 定时任务执行时间偏移
|
||
- 应用程序显示时间异常
|
||
- 多台服务器之间时间显示混乱
|
||
|
||
## 7、时间同步
|
||
|
||
在生产环境中,通常不建议长期依赖手动设置时间,而是应通过 NTP 自动同步。
|
||
|
||
### 7.1 为什么要同步时间
|
||
|
||
- 保证日志时间一致
|
||
- 保证集群节点时间一致
|
||
- 避免证书校验异常
|
||
- 避免定时任务和监控系统误判
|
||
|
||
### 7.2 启用自动同步
|
||
|
||
```shell
|
||
timedatectl set-ntp true
|
||
```
|
||
|
||
### 7.3 检查是否启用同步
|
||
|
||
```shell
|
||
timedatectl status
|
||
```
|
||
|
||
关注输出中的 NTP 状态是否已开启。
|
||
|
||
## 8、常见排障思路
|
||
|
||
### 8.1 系统时间不对
|
||
|
||
优先检查:
|
||
|
||
```shell
|
||
date
|
||
timedatectl status
|
||
```
|
||
|
||
### 8.2 怀疑硬件时钟有问题
|
||
|
||
检查:
|
||
|
||
```shell
|
||
hwclock --show
|
||
```
|
||
|
||
### 8.3 日志时间不一致
|
||
|
||
重点检查:
|
||
|
||
- 系统时间是否正确
|
||
- 时区是否正确
|
||
- 是否开启 NTP 自动同步
|
||
|
||
### 8.4 定时任务执行时间异常
|
||
|
||
重点检查:
|
||
|
||
- 系统时间是否正确
|
||
- 时区是否正确
|
||
- `cron` 或应用自身是否使用了其他时区设置
|
||
|
||
## 9、小结
|
||
|
||
- `date` 主要操作的是**系统时间**
|
||
- `hwclock` 主要操作的是**硬件时钟**
|
||
- `timedatectl` 是 systemd 系统中最常用的时间管理工具
|
||
- 时间管理不仅要看“时间”,还要看“时区”和“同步状态”
|
||
- 排查时间问题时,建议先看 `date`、`hwclock --show`、`timedatectl status`
|
||
|
||
理解这几个命令后,Linux 中绝大多数基础时间问题都能快速定位。
|