Change system clock on Amazon Linux2(systemd)

Page content

Motivation

When I migrate Amazon Linux to Amazon Linux2, I investigate how to change system clock on Linux server working with systemd. This post is technical memo for myself.

Change system clock

System clock is calculated by /etc/adjtime that uses hardware clock. If OS working with systemd dosen't have /etc/adjtime, UTC is default.

cat /etc/adjtime

> 0.0 0 0.0
> 0
> UTC

Now check the system clock with timedatectl command.

timedatectl

>       Local time: Sat 2018-06-09 05:16:29 UTC
>   Universal time: Sat 2018-06-09 05:16:29 UTC
>         RTC time: Sat 2018-06-09 05:16:28
>        Time zone: n/a (UTC, +0000)
>      NTP enabled: yes
> NTP synchronized: no
>  RTC in local TZ: no
>      DST active: n/a

UTC is default setting.

And change time zone to Asia/Tokyo with using timedatectl command.

timedatectl set-timezone Asia/Tokyo

Execute timedatectl command again, check timezone is JST.

timedatectl

>       Local time: Sat 2018-06-09 14:57:45 JST
>   Universal time: Sat 2018-06-09 05:57:45 UTC
>         RTC time: Sat 2018-06-09 05:57:45
>        Time zone: Asia/Tokyo (JST, +0900)
>      NTP enabled: yes
> NTP synchronized: no
>  RTC in local TZ: no
>       DST active: n/a

Timezone settings is configured at /etc/localtime. Check /etc/localtime with cat command.

cat /etc/localtime

> TZif2
>         ��>p��K����p��-���ۭ�����݌�����~~LMTJDTJSTTZif2
>
> �����e¤p�����>p������K��������p������-�����������ۭ�������������݌�����~~LMTJDTJST

zdump command can print time zone info in /etc/localtime without character corruption . The time zone is JST.

zdump /etc/localtime

> /etc/localtime  Sat Jun  9 14:57:54 2018 JST

Using set-time sub command can set datetime directly as follows, however I have never used.

timedatectl set-time "2018-06-09 12:00:00"

Check hardware clock

Linux refers hardware clock to set system clock when bootstrap. Now check hardware information with hwclock --debug command.

hwclock --debug

> hwclock from util-linux 2.30.2
> Trying to open: /dev/rtc0
> Using the rtc interface to the clock.
> Last drift adjustment done at 0 seconds after 1969
> Last calibration done at 0 seconds after 1969
> Hardware clock is on UTC time
> Assuming hardware clock is kept in UTC time.
> Waiting for clock tick...
> ...got clock tick
> Time read from Hardware Clock: 2018/06/09 06:09:22
> Hw clock time : 2018/06/09 06:09:22 = 1528524562 seconds since 1969
> Time since last adjustment is 1528524562 seconds
> Calculated Hardware Clock drift is 0.000000 seconds
> 2018-06-09 15:09:21.645250+0900

Without --debug option, print only dates time information recorded in hardware.

hwclock

> 2018-06-09 15:09:21.645250+0900