如何在CentOS 7上设置或更改时区
在CentOS上,系统的时区是在安装过程中设置的,但以后可以轻松更改。
对于许多与系统相关的任务和过程,使用正确的时区非常重要。 例如,cron守护程序使用系统的时区执行cron作业,而日志文件中的时间戳基于同一系统的时区。
本教程说明了如何在CentOS 7上设置或更改时区。
先决条件
为了能够更改系统的时区,您需要以root用户或具有sudo特权的用户身份登录。
检查当前时区
在CentOS和其他现代Linux发行版中,您可以使用 timedatectl
命令以显示和设置当前系统的时间和时区。
timedatectl
以下输出显示系统的时区设置为UTC:
Local time: Wed 2019-02-06 22:43:42 UTC
Universal time: Wed 2019-02-06 22:43:42 UTC
RTC time: Wed 2019-02-06 22:43:42
Time zone: Etc/UTC (UTC, +0000)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
通过符号链接配置系统时区 /etc/localtime
到 /usr/share/zoneinfo
目录。 因此,检查时区的另一种方法是使用ls命令显示symlink指向的路径:
ls -l /etc/localtime
lrwxrwxrwx. 1 root root 29 Dec 11 09:25 /etc/localtime -> ../usr/share/zoneinfo/Etc/UTC
在CentOS中更改时区
在更改时区之前,您需要找出要使用的时区的长名称。 时区命名约定通常使用“地区/城市”格式。
要列出所有可用时区,您可以在 /usr/share/zoneinfo
目录或使用 timedatectl
命令。
timedatectl list-timezones
...
America/Tijuana
America/Toronto
America/Tortola
America/Vancouver
America/Whitehorse
America/Winnipeg
...
一旦确定了哪个时区适合您的位置,请以sudo用户身份运行以下命令:
sudo timedatectl set-timezone your_time_zone
例如,将系统的时区更改为 America/Toronto
:
sudo timedatectl set-timezone America/Toronto
跑过 timedatectl
命令来验证更改:
timedatectl
Local time: Wed 2019-02-06 17:47:10 EST
Universal time: Wed 2019-02-06 22:47:10 UTC
RTC time: Wed 2019-02-06 22:47:10
Time zone: America/Toronto (EST, -0500)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2018-11-04 01:59:59 EDT
Sun 2018-11-04 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2019-03-10 01:59:59 EST
Sun 2019-03-10 03:00:00 EDT
通过创建符号链接来更改时区
如果您正在运行旧版本的CentOS,并且 timedatectl
命令不存在于您的系统上,您可以通过符号链接更改时区 /etc/localtime
到时区文件中 /usr/share/zoneinfo
目录。
删除当前 /etc/localtime
文件或符号链接:
sudo rm -rf /etc/localtime
确定要配置的时区并创建符号链接:
sudo ln -s /usr/share/zoneinfo/America/Toronto /etc/localtime
您可以通过列出以下内容来进行验证 /etc/localtime
归档或签发 date
命令:
date
Wed Feb 6 17:52:58 EST 2019
结论#
在本指南中,我们向您展示了如何更改CentOS系统的时区。
如有任何疑问,请随时发表评论。
centos终端