如何在Debian 9上设置或更改时区

发布时间:2020-02-28 09:24:44,浏览5650次,转载自:互联网

如何在Debian 9上设置或更改时区

对于许多与系统相关的任务和过程,使用正确的时区很重要。例如,cron守护程序使用系统的时区执行cron作业,而日志文件中的时间戳基于同一系统的时区。

系统的时区是在安装过程中设置的,但以后可以轻松更改。本教程介绍了如何在Debian 9上设置或更改时区。

先决条件

您所登录的用户必须具有 sudo权限,才能更改系统的时区。

检查当前时区

在Debian和其他现代Linux发行版中,timedatectl命令允许您显示和设置当前系统的时间和时区。

timedatectl

如下面的输出所示,系统的时区设置为“美国/芝加哥”:

      Local time: Mon 2019-03-11 16:46:45 CDT
  Universal time: Mon 2019-03-11 21:46:45 UTC
        RTC time: Mon 2019-03-11 21:46:45
       Time zone: America/Chicago (CDT, -0500)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no

通过将/etc/localtime符号链接到/usr/share/zoneinfo目录中的二进制时区标识符来配置系统时区。检查时区的另一个选项是使用 ls命令:

显示符号链接指向的路径。

ls -l /etc/localtime
lrwxrwxrwx 1 root root 37 Jan 23 03:19 /etc/localtime -> ../usr/share/zoneinfo/America/Chicago

更改Debian中的时区

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

要列出所有可用时区,可以列出/usr/share/zoneinfo目录中的文件,也可以使用timedatectl命令。

timedatectl list-timezones
...
Europe/Lisbon
Europe/Ljubljana
Europe/London
Europe/Luxembourg
Europe/Madrid
Europe/Malta
...

确定了您所在位置的时区后,以sudo用户身份运行以下命令:

sudo timedatectl set-timezone your_time_zone

例如,要将系统的时区更改为Europe/Ljubljana,您可以运行:

sudo timedatectl set-timezone Europe/Ljubljana

通过发出timedatectl命令验证更改:

timedatectl
      Local time: Mon 2019-03-11 22:51:27 CET
  Universal time: Mon 2019-03-11 21:51:27 UTC
        RTC time: Mon 2019-03-11 21:51:26
       Time zone: Europe/Ljubljana (CET, +0100)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

通过创建符号链接来更改时区

如果您正在运行Debian 的较旧版本,并且系统上没有timedatectl命令,则可以通过将/etc/localtime符号链接到/usr/share/zoneinfo目录中的时区来更改时区。

删除当前的/etc/localtime文件或符号链接:

sudo rm -f /etc/localtime

确定要配置的时区,然后创建符号链接:

sudo ln -s /usr/share/zoneinfo/Europe/Ljubljana /etc/localtime

您可以通过列出/etc/localtime文件或发出date命令来确认更改:

date
Mon Mar 11 22:55:04 CET 2019

结论

在本指南中,您学习了如何更改Debian系统的时区。如有任何疑问,请随时发表评论。

如果你喜欢我们的内容可以选择在下方二维码中捐赠我们,或者点击广告予以支持,感谢你的支持

评论