6

如何在CentOS 8上设置或更改时区

 3 years ago
source link: https://www.myfreax.com/how-to-set-or-change-timezone-on-centos-8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

对于许多与系统相关的任务和过程,使用正确的时区至关重要。 例如,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中更改时区

更改时区之前,您需要找出要使用的时区的长名称。 时区使用“地区/城市”格式。

要列出所有可用时区,请使用list-timezones选项调用timedatectl命令:

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

运行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 date 命令来验证更改:

date
Sat Mar 21 17:46:10 EDT 2020

我们向您展示了如何更改CentOS系统的时区。

如果您遇到任何问题,请随时发表评论。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK