
Linuxでtimedatectlコマンドで、時刻の確認と設定変更を行います。
記事の目次
timedatectlのヘルプを確認する!
timedatectlコマンドのヘルプを表示して使用方法を確認します。
$ timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
Options:
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
See the timedatectl(1) man page for details.
現在の時刻を表示する!
timedatectlコマンドで現在の時刻を表示します。「timedatectl status」と同じ結果になります。
$ timedatectl
Local time: Thu 2023-01-12 07:03:10 JST
Universal time: Wed 2023-01-11 22:03:10 UTC
RTC time: Wed 2023-01-11 22:03:10
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
showオプションを指定すると、シェルで使用しやすいフォーマットで表示することができます。
$ timedatectl show Timezone=Asia/Tokyo LocalRTC=no CanNTP=yes NTP=yes NTPSynchronized=yes TimeUSec=Thu 2023-01-12 07:09:55 JST RTCTimeUSec=Thu 2023-01-12 07:09:55 JST
時刻を設定しようとしてみる!
時刻同期をしている場合は、コマンドから設定できません。
$ sudo timedatectl set-time "2023-01-12 08:09:49 JST" Failed to set time: Automatic time synchronization is enabled
リアルタイムクロックをローカル時間扱いに設定してみる!
Windowsのようにリアルタイムクロックをローカル時間扱いに設定してみます。
$ sudo timedatectl set-local-rtc 1
usradmin@dpc001p1:~$ sudo timedatectl
Local time: Thu 2023-01-12 20:36:11 JST
Universal time: Thu 2023-01-12 11:36:11 UTC
RTC time: Thu 2023-01-12 20:36:11
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.
timedatectlコマンドで確認すると、おすすめでない旨のメッセージが表示されます。すぐ戻しましょう。
タイムゾーンを変更する!
変更可能なタイムゾーンを表示します。
$ timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa ...
タイムゾーンを変更します。
$ sudo timedatectl set-timezone Africa/Abidjan
usradmin@dpc001p1:~$ timedatectl
Local time: Thu 2023-01-12 11:43:57 GMT
Universal time: Thu 2023-01-12 11:43:57 UTC
RTC time: Thu 2023-01-12 11:43:57
Time zone: Africa/Abidjan (GMT, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
RHEL系は、「/etc/localtime」のリンク先が変更になります。
$ sudo ls -l /etc/localtime lrwxrwxrwx. 1 root root 36 Jan 12 11:46 /etc/localtime -> ../usr/share/zoneinfo/Africa/Abidjan
Debian系は、「/etc/timezone」ファイルの内容が変更されます。
$ cat /etc/timezone Africa/Abidjan
timedatectlコマンドを使用するとLinuxのディストリビューションの差異を吸収することもできます。
おわりに
timedatectlコマンドでは、時刻周りの設定を一括して変更できるようになっています。dateコマンドの代わりに使用するのもよいと思います。
関連記事
関連書籍(Amazon)

