[Linux] 设置系统时区

时间:2023-03-09 09:33:05
[Linux] 设置系统时区

1. 检查当前时区

  以 root 身份登录。

# date
Fri Sep :: UTC

  其中 UTC 是指当前使用的时间系统为世界标准时间,也称世界协调时间。英文名称为 Coordinated Universal Time,法文名称为 Temps Universel Coordonné。作为英文缩写 CUT 和法文缩写 TUC 的妥协方案,简称 UTC。中国所处时区为 UTC+8。

2. 查看可用时区

  查看目录 /usr/share/zoneinfo,可以看到所有时区。(根据 Linux 发行不同而可以时区列表不同。)

# ls /usr/share/zoneinfo
Africa Australia Cuba Etc GMT+ Iceland Kwajalein NZ Poland Turkey WET right
America Brazil EET Europe GMT- Indian Libya NZ-CHAT Portugal UCT Zulu zone.tab
Antarctica CET EST Factory GMT0 Iran MET Navajo ROC US iso3166.tab
Arctic CST6CDT EST5EDT GB Greenwich Israel MST PRC ROK UTC localtime
Asia Canada Egypt GB-Eire HST Jamaica MST7MDT PST8PDT Singapore Universal posix
Atlantic Chile Eire GMT Hongkong Japan Mexico Pacific SystemV W-SU posixrules

3. 设置时区

3.1. 手动设置时区

  首先备份一下当前的时区配置。

# mv /etc/localtime /etc/localtime-old

  使用新的时区。你可以在地区列表中找到你所处的城市,如果没有你所处的城市,那么选择另一个同时区的城市。

# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  配置完成后你可以使用 date 命令查看是否已经生效。

3.2. 自动设置时区

  在 Ubuntu 中自带了设置时区工具,tzconfig 不推荐使用,使用 dpkg-reconfigure tzdata 命令调出设置 UI,跟随界面指示设置即可完成时区设置。

$ sudo dpkg-reconfigure tzdata

  弹出对话框:

[Linux] 设置系统时区

  选择大洲后,然后选择时区城市,北京时间选择 Shanghai 即可:

Current default time zone: 'Asia/Shanghai'
Local time is now: Sat Sep :: CST .
Universal Time is now: Sat Sep :: UTC .

4. 设置服务器与互联网时间服务器同步

  大多数现代 Linux 发行版本都已经预装 NTP 程序包。如果你的服务器没有安装它,你可以手动安装。

  1. Ubuntu/Debian:

$ sudo aptitude install ntp

  2. CentOS:

$ sudo yum install ntp
$ sudo /sbin/chkconfig ntpd on

  3. Fedora/RedHat:

$ sudo yum install ntp
$ sudo chkconfig ntpd on

  安装完毕后执行:

$ ntpdate && hwclock -w

  或者与 cn.pool.ntp.org 同步一下时间,该站点是位于中国的 NTP 公共服务器:

$ sudo ntpdate cn.pool.ntp.org
Sep :: ntpdate[]: adjust time server 202.112.29.82 offset 0.017623 sec

 


附:

1. 3 Ways to Change the Timezone in Linux

2. How To Set Up Synchronization on Ubuntu 12.04

3. tzconfig (deprecated)

4. dpkg-reconfigure tzdata