Linux hardware clock time sync
When operating a Linux system, you sometimes need to manually sync the clock because the hardware clock and the OS clock are out of sync. In that case, first check the sync state with the timedatectl command.
[root@centos7-1 ~]# timedatectl
Local time: Sat 2021-11-06 18:55:29 JST
Universal time: Sat 2021-11-06 09:55:29 UTC
RTC time: Sat 2021-11-06 11:55:51
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/aRunning the timedatectl command shows the Local time and the RTC (hardware clock). Because RTC in local TZ (whether the hardware clock is shown in the local time zone) is “no,” you should know that the RTC is being shown in UTC.
[root@centos7-1 ~]# timedatectl
Local time: Sat 2021-11-06 18:55:29 JST
Universal time: Sat 2021-11-06 09:55:29 UTC
RTC time: Sat 2021-11-06 11:55:51
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/aMy system’s Time zone is set to Asia/Tokyo, so adding 9 hours to the UTC time gives the local time. You can also see that the Universal Time (UTC) and the RTC time don’t match. Since the hardware clock is the accurate one, let’s sync the OS clock to the hardware clock.
To sync the OS clock with the hardware clock, run the following command.
[root@centos7-1 ~]# hwclock --hctosysLet’s check the sync state again with the timedatectl command.
[root@centos7-1 ~]# timedatectl
Local time: Sat 2021-11-06 21:16:18 JST
Universal time: Sat 2021-11-06 12:16:18 UTC
RTC time: Sat 2021-11-06 12:16:18
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/aYou can confirm they’re synced.
Conversely, when you want to sync the hardware clock to the OS clock, run the following command.
hwclock --systohcTo change both the hardware clock and the OS clock to the same time at once, it’s convenient to use the timedatectl command as follows.
timedatectl set-time "2021-11-06 21:20:00"Let’s run it.
[root@centos7-1 ~]# timedatectl set-time "2021-11-06 21:20:00"; timedatectl
Local time: Sat 2021-11-06 21:20:00 JST
Universal time: Sat 2021-11-06 12:20:00 UTC
RTC time: Sat 2021-11-06 12:20:00
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/aYou can confirm that both the OS clock and the hardware clock changed at the same time.