使用正确的时区对于许多与系统相关的任务和流程至关重要。例如,cron 守护进程使用系统的时区来执行 cron 作业,并且日志文件中的时间戳基于同一系统的时区。
在 CentOS 上,系统的时区是在安装过程中设置的,但以后可以轻松更改。
本文介绍如何在 CentOS 8 系统上设置或更改时区。
检查当前时区#
timedatectl
是一个命令行实用程序,允许您查看和更改系统的时间和日期。它可在所有基于 systemd 的现代 Linux 系统上使用:
timedatectl
输出显示系统的时区。在此示例中,时区设置为 UTC:
Local time: Sat 2020-03-21 21:30:22 UTC
Universal time: Sat 2020-03-21 21:30:22 UTC
RTC time: Sat 2020-03-21 21:30:22
Time zone: UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
如果您收到警告“系统已配置为读取本地时区的 RTC 时间。”,请运行以下命令以使用 UTC 格式的 RTC:
timedatectl
系统时区通过符号链接配置/etc/localtime
到二进制时区标识符/usr/share/zoneinfo
目录。检查时区的另一个选项是使用以下命令显示符号链接指向的路径ls命令:
ls -l /etc/localtime
lrwxrwxrwx. 1 root root 23 Nov 21 23:30 /etc/localtime -> /usr/share/zoneinfo/UTC
在 CentOS 中更改时区#
在更改时区之前,您需要找出要使用的时区的长名称。时区使用“地区/城市”格式。
要列出所有可用时区,请调用timedatectl
命令与list-timezones
option:
timedatectl list-timezones
...
America/Tijuana
America/Toronto
America/Tortola
America/Vancouver
America/Whitehorse
America/Winnipeg
...
一旦您确定了哪个时区与您的位置相符,请以 root 身份运行以下命令或具有 sudo 权限的用户
:
sudo timedatectl set-timezone your_time_zone
例如,将系统的时区更改为America/Toronto
:
sudo timedatectl set-timezone America/Toronto
Run the timedatectl
命令来验证更改:
timedatectl
Local time: Sat 2020-03-21 17:43:39 EDT
Universal time: Sat 2020-03-21 21:43:39 UTC
RTC time: Sat 2020-03-21 21:43:40
Time zone: America/Toronto (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
通过创建符号链接更改时区#
如果您运行的是较旧的CentOS版本和timedatectl
您的系统上不存在该命令,您可以通过符号链接更改时区/etc/localtime
到时区文件/usr/share/zoneinfo
目录。
确定您要配置的时区并创建一个符号链接
:
sudo ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime
通过列出以下内容来验证更改/etc/localtime
归档或发出timedatectl
or date命令:
date
Sat Mar 21 17:46:10 EDT 2020
结论#
我们已经向您展示了如何更改 CentOS 系统的时区。
如果您遇到任何问题,请随时发表评论。