时区
概述
时区配置是系统部署和运维中的基础操作,不同 Linux 发行版的管理方式有所差异。本文档涵盖 Alpine、Ubuntu/Debian、CentOS 7 和 Rocky Linux 9 四种主流系统的时区配置方法。
适用场景:
- 容器或虚拟机部署后修改默认时区
- 跨时区服务器统一时间标准
- 应用日志时间戳本地化
Alpine 系统
Alpine 基于 BusyBox,默认使用 busybox-timezone 工具管理时区,操作步骤如下:
查看当前时区
# 方法1:通过日期命令查看
date
# 示例输出:Wed Oct 18 10:30:00 UTC 2023(UTC 表示当前为世界标准时间)
# 方法2:查看时区配置文件
cat /etc/timezone # 若文件存在,直接显示时区;若不存在,默认为 UTC修改为东八区
# 步骤1:安装时区依赖包(若未安装)
apk add -U tzdata
# 步骤2:设置时区为 Asia/Shanghai
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 步骤3:写入时区信息到 /etc/timezone(可选,部分应用依赖该文件)
echo "Asia/Shanghai" > /etc/timezone验证修改结果
date
# 正确输出示例:Wed Oct 18 18:30:00 CST 2023(CST 即中国标准时间,东八区)Ubuntu & Debian 系统
Ubuntu(16.04+)和 Debian(9+)均支持 timedatectl 工具(系统自带),操作步骤完全一致:
查看当前时区
# 方法1:使用 timedatectl(推荐)
timedatectl
# 示例输出:Time zone: UTC (UTC, +0000)
# 方法2:通过日期命令查看
date
# 示例输出:Wed Oct 18 10:35:00 UTC 2023修改为东八区
# 方法1:使用 timedatectl 直接设置(推荐)
sudo timedatectl set-timezone Asia/Shanghai
# 方法2:手动创建时区软链接(兼容旧版本系统)
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo dpkg-reconfigure -f noninteractive tzdata # 刷新时区配置(可选)验证修改结果
timedatectl
# 正确输出示例:Time zone: Asia/Shanghai (CST, +0800)
date
# 正确输出示例:Wed Oct 18 18:35:00 CST 2023CentOS 7 系统
CentOS 7 支持 timedatectl(系统自带),也可通过手动修改时区文件实现:
查看当前时区
# 方法1:使用 timedatectl
timedatectl
# 示例输出:Time zone: UTC (UTC, +0000)
# 方法2:查看 /etc/localtime 软链接指向
ls -l /etc/localtime
# 示例输出:lrwxrwxrwx 1 root root 25 Oct 18 10:00 /etc/localtime -> /usr/share/zoneinfo/UTC修改为东八区
# 方法1:使用 timedatectl(推荐)
sudo timedatectl set-timezone Asia/Shanghai
# 方法2:手动替换时区软链接
sudo rm -rf /etc/localtime
sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime验证修改结果
timedatectl
# 正确输出示例:Time zone: Asia/Shanghai (CST, +0800)
date
# 正确输出示例:Wed Oct 18 18:40:00 CST 2023Rocky Linux 9 系统
Rocky Linux 9 基于 RHEL 9,默认自带 timedatectl,操作与 CentOS 7 类似:
查看当前时区
timedatectl
# 示例输出:Time zone: UTC (UTC, +0000)修改为东八区
sudo timedatectl set-timezone Asia/Shanghai验证修改结果
timedatectl
# 正确输出示例:Time zone: Asia/Shanghai (CST, +0800)
date
# 正确输出示例:Wed Oct 18 18:45:00 CST 2023各系统命令速查表
| 操作 | Alpine | Ubuntu/Debian | CentOS 7 | Rocky 9 |
|---|---|---|---|---|
| 查看时区 | date 或 cat /etc/timezone |
timedatectl |
timedatectl |
timedatectl |
| 安装时区包 | apk add tzdata |
apt install tzdata |
yum install tzdata |
dnf install tzdata |
| 设置时区 | ln -sf ... /etc/localtime |
timedatectl set-timezone Asia/Shanghai |
timedatectl set-timezone Asia/Shanghai |
timedatectl set-timezone Asia/Shanghai |
| 验证时区 | date |
timedatectl 或 date |
timedatectl 或 date |
timedatectl 或 date |
常见问题与注意事项
权限不足
操作时若提示 “Permission denied”,在命令前添加 sudo(需当前用户有 sudo 权限),或切换至 root 用户:
su - root时区文件不存在
若提示 /usr/share/zoneinfo/Asia/Shanghai 不存在,需安装时区数据包:
# Alpine
apk add tzdata
# Ubuntu/Debian
sudo apt install tzdata
# CentOS 7 / Rocky 9
sudo yum install tzdata # 或 dnf install tzdata修改后时间仍不正确
可通过 NTP 同步时间(以 chrony 为例):
# 安装 chrony
sudo yum install chrony -y # CentOS 7 / Rocky 9
sudo apt install chrony -y # Ubuntu/Debian
sudo apk add chrony -y # Alpine
# 启动并设置开机自启
sudo systemctl start chronyd
sudo systemctl enable chronyd
# 查看同步状态
sudo chronyc sources -v