タイムゾーンの設定方法をメモ(RHEL6, RHEL7, Ubuntu編)

  • RHEL6(amazon Linux含む)
  • RHEL7(systemd)
  • Ubuntu

の各環境でタイムゾーンを設定する方法をメモ。

RHEL6

まずは RedHat 6系の場合。

amazon ec2 のドキュメントにわかりやすい説明があるので、これを読めばOK

Setting the Time for Your Linux Instance – Amazon Elastic Compute Cloud
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html

タイムゾーンの一覧を取得

/usr/share/zoneinfo 以下のタイムゾーン情報から設定したいタイムゾーンを調べる

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

日本の場合

  • Japan
  • Asia/Tokyo

と複数の選択肢があるが、実体は同じ

~]$ md5sum /usr/share/zoneinfo/Asia/Tokyo /usr/share/zoneinfo/Japan
9e165b3822e5923e4905ee1653a2f358  /usr/share/zoneinfo/Asia/Tokyo
9e165b3822e5923e4905ee1653a2f358  /usr/share/zoneinfo/Japan

候補の中には America/Indiana/Marengo のように Area/Location の2階層で完結しないものもあるので、タイムゾーンを操作するプログラムを書いている人は注意が必要。

ハードウェアクロック向け設定

ファイル /etc/sysconfig/clockZONE エントリーを先ほど決めたタイムゾーンに変更する

~]$ cat /etc/sysconfig/clock
ZONE="Asia/Tokyo"
UTC=true

システムクロック向け設定

システムワイドにタイムゾーン設定を管理する /etc/localtime を修正
シンボリックリンクの向き先をさきほど決めたタイムゾーンに変更する

~]$ sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

設定を有効にする

~]$ sudo reboot

RHEL7(systemd)

RHEL7 からはタイムゾーンの操作は systemd の1コマンドである timedatectl 経由でするのがお作法

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Configuring_the_Date_and_Time.html

タイムゾーンの一覧を取得

]$ sudo timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
...

設定を変更

~]$ sudo timedatectl set-timezone Europe/Prague

設定を確認

~]$ sudo timedatectl
      Local time: Sat 2015-02-07 09:35:20 CET
  Universal time: Sat 2015-02-07 08:35:20 UTC
        RTC time: Sat 2015-02-07 08:35:19
        Timezone: Europe/Prague (CET, +0100)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2014-10-26 02:59:59 CEST
                  Sun 2014-10-26 02:00:00 CET
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2015-03-29 01:59:59 CET
                  Sun 2015-03-29 03:00:00 CEST

Ubuntu

Ubuntu14.04 からは timedatectl が使える(systemd-services パッケージの1プログラム)。

timedatestl が使えないことを前提に、次の wiki を参考に dpkg-reconfigure 経由で設定する。

https://help.ubuntu.com/community/UbuntuTime

システムクロック向け設定

Debian/Ubuntu 系は /etc/timezone でシステムのタイムゾーンを管理している。
RHEL6 の時と同じく /usr/share/zoneinfo 以下のタイムゾーン情報から設定したいタイムゾーンを調べて設定する。

~]$ echo "Asia/Tokyo" | sudo tee /etc/timezone
Asia/Tokyo

設定を有効にする

~]$ sudo dpkg-reconfigure --frontend noninteractive tzdata

Current default time zone: 'Asia/Tokyo'
Local time is now:      Sat Feb  7 16:03:24 JST 2015.
Universal Time is now:  Sat Feb  7 07:03:24 UTC 2015.

dpkg-reconfigure コマンドから --frontend noninteractive オプションを省くと、タイムゾーンを選びながら設定できる。

/etc/timezone と /etc/localtime の優先順位

/etc/timezone は Debian/Ubuntu 固有で /etc/localtime は Linux 全般で利用されている。
Ubuntu の wiki には /etc/timezone を変更する手順しか書かれていないけれども、素直に /etc/localtime を変更してもタイムゾーンは切り替わる。
dpkg-reconfigure --frontend noninteractive tzdata を実行した時に /etc/timezone/etc/localtime が食い違った時、どのように振る舞うのか確認してみた。

dpkg-reconfigure-priority

まとめると /etc/timezone/etc/localtime のどちらかが現在のタイムゾーンと異なっていればタイムゾーンも新しいものに変更され、現在のタイムゾーンと /etc/timezone/etc/localtime がすべて異なる場合 /etc/localtime の設定が優先される

3 thoughts on “タイムゾーンの設定方法をメモ(RHEL6, RHEL7, Ubuntu編)

Leave a comment