如何在CentOS 7上设置或者更改时区
时间:2020-03-05 15:27:30 来源:igfitidea点击:
在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
例如,要将系统的时区更改为“美国/多伦多”:
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 2016-11-04 01:59:59 EDT Sun 2016-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 Nov 6 17:52:58 EST 2019