LoginSignup
4
2

More than 1 year has passed since last update.

Raspberry piでNTPとRTCモジュールの時刻設定をうまいことやりたい

Last updated at Posted at 2022-03-29

目的

Raspberry piにRTCモジュールをつけるけど、NTPサーバーにつながる場合はNTPで時刻同期するようにしたい

経緯

Raspberry piでRTCモジュールをつけてみたが、NTPサーバーに繋がる場合は、そちらを参照するように設定したいと思いスクリプトを作ってRTCモジュールの時刻書き換えを行っていました。

ntpdateを使ってごにょごにょしてましたが、Raspberry piでは使わなくてもいいということを知ったので、その方法を記載します。

結論

timedatectlを使う!

設定の手順

RTCモジュールの接続手順とNTPサーバーの設定手順を残します。

RTCモジュールの接続

RTCモジュールを接続する手順です。

i2cの有効化

RTCモジュールは、I2C接続のためI2Cを有効化します。

コマンド実行
$ sudo raspi-config
「Interface Options」を選択します。

image.png

「I2C」を選択します。

image.png

I2C の有効か確認メッセージが表示されるのを確認し「Yes」を選択します。

image.png

メッセージが出てきたら「OK」を選択します。

image.png

これも入れておきます。
$ sudo apt-get install -y i2c-tools

RTCモジュールをラズパイにセット

InkedFPBOazsaUAI7Dyd_LI.jpg
使用するRTCモジュールを設定
/boot/config.txt
dtoverlay=i2c-rtc,ds1307
接続確認

I2Cアドレス0x68になっている。

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
timedatactlで現状を確認

RTC time: n/aになっている。

$ timedatectl
               Local time: Tue 2022-03-29 22:16:55 JST
           Universal time: Tue 2022-03-29 13:16:55 UTC
                 RTC time: n/a
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
再起動
$ sudo reboot
もう一度、接続確認

0x68がUUにっていることを確認

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
timedatectlで確認

RTC time:に時刻が表示されるようになっている。

$ timedatectl
               Local time: Tue 2022-03-29 22:30:02 JST
           Universal time: Tue 2022-03-29 13:30:02 UTC
                 RTC time: Tue 2022-03-29 13:30:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
RTCモジュールの時刻も確認
$ sudo hwclock -r
2022-03-29 22:32:16.728296+09:00

※システム時刻と同じになってるのはNTPが有効になっており、再起動時にRTCモジュールに書き込まれているためです。

NTPサーバーの設定

NTPサーバーを指定する。

systemd-timesyncdサービスのステータスを確認

warningが出ています。
2.debian.pool.ntp.orgのNTPサーバーと時刻同期しているようです。

$ sudo systemctl status systemd-timesyncd
Warning: The unit file, source configuration file or drop-ins of systemd-timesyncd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /usr/lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Tue 2022-03-29 22:29:18 JST; 5min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 334 (systemd-timesyn)
   Status: "Synchronized to time server for the first time 194.0.5.123:123 (2.debian.pool.ntp.org)."
    Tasks: 2 (limit: 2059)
   CGroup: /system.slice/systemd-timesyncd.service
           └─334 /lib/systemd/systemd-timesyncd

Mar 29 22:29:17 raspberrypi systemd[1]: Starting Network Time Synchronization...
Mar 29 22:29:18 raspberrypi systemd[1]: Started Network Time Synchronization.
Mar 29 22:29:58 raspberrypi systemd-timesyncd[334]: Synchronized to time server for the first time 194.0.5.123:123 (2.debian.pool.ntp.org).

confファイルを作成

  1. /etc/systemd/timesyncd.conf.d フォルダを作成
  2. /etc/systemd/timesyncd.conf.d/*.conf ファイルを作成
    ファイル名は自分で決めていいみたい。
/etc/systemd/timesyncd.conf.d/*.conf
[Time]
NTP=ntp.jst.mfeed.ad.jp ntp.nict.jp
FallbackNTP=time.google.com

timedatectlでNTPを有効にする

$ sudo timedatectl set-ntp true

systemd-timesyncdサービスを再起動する

$ sudo systemctl daemon-reload
$ sudo systemctl restart systemd-timesyncd

systemd-timesyncdサービスのステータスを確認

ntp.jst.mfeed.ad.jpに同期している。

$ sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /usr/lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Tue 2022-03-29 22:47:49 JST; 6s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 835 (systemd-timesyn)
   Status: "Synchronized to time server for the first time 210.173.160.57:123 (ntp.jst.mfeed.ad.jp)."
    Tasks: 2 (limit: 2059)
   CGroup: /system.slice/systemd-timesyncd.service
           └─835 /lib/systemd/systemd-timesyncd

Mar 29 22:47:49 raspberrypi systemd[1]: Starting Network Time Synchronization...
Mar 29 22:47:49 raspberrypi systemd[1]: Started Network Time Synchronization.
Mar 29 22:47:49 raspberrypi systemd-timesyncd[835]: Synchronized to time server for the first time 210.173.160.57:123

動作を確認してみた

RTCの時刻をずらし以下のパターンで、システム時刻やRTCモジュールの時刻がどのようになっているのかを確認してみる。

No. NTP ネットワーク
1 無効 -
2 有効 ネットワークなし
3 有効 ネットワークあり
NTPサーバーに繋がらない
4 有効 ネットワークあり
NTPサーバーに繋がる

No.1 NTPが無効の場合

NTPを無効化

$ sudo timedatectl set-ntp false

RTCの時刻をずらす

$ sudo hwclock --set --date '2022-03-30 00:00:00.000000+09:00'

時刻確認

RTC time: Tue 2022-03-29 15:00:03でRTCの時刻が手動更新されたことを確認。

$ timedatectl
               Local time: Tue 2022-03-29 22:55:51 JST
           Universal time: Tue 2022-03-29 13:55:51 UTC
                 RTC time: Tue 2022-03-29 15:00:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: inactive
          RTC in local TZ: no

再起動

$ sudo reboot

時刻確認

  • System clock synchronized: noとなっておりNTPサーバーに同期はされていない。
  • Local time: Wed 2022-03-30 00:01:50 JSTLocal timeがRTCモジュールに設定した時刻で更新されていることを確認。
$ timedatectl
               Local time: Wed 2022-03-30 00:01:50 JST
           Universal time: Tue 2022-03-29 15:01:50 UTC
                 RTC time: Tue 2022-03-29 15:01:50
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

No.2 NTPが有効、ネットワークなし

NTPを有効化

$ sudo timedatectl set-ntp true

systemd-timesyncdサービスを再起動してNTPサーバの時刻に同期させる

$ sudo systemctl daemon-reload
$ sudo systemctl restart systemd-timesyncd

時刻確認

(NTPサーバーと同期されている)

$ timedatectl
               Local time: Tue 2022-03-29 23:23:41 JST
           Universal time: Tue 2022-03-29 14:23:41 UTC
                 RTC time: Tue 2022-03-29 14:23:42
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

RTCの時刻をずらす

$ sudo hwclock --set --date '2022-03-30 00:00:00.000000+09:00'

時刻確認

$ timedatectl
               Local time: Tue 2022-03-29 23:24:08 JST
           Universal time: Tue 2022-03-29 14:24:08 UTC
                 RTC time: Tue 2022-03-29 15:00:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

ネットワーク接続を切っておく

再起動

$ sudo reboot

時刻確認

RTCモジュールの時刻で、Local timeが更新されていることを確認。

$ timedatectl
               Local time: Tue 2022-03-30 00:06:41 JST
           Universal time: Tue 2022-03-29 15:06:41 UTC
                 RTC time: Tue 2022-03-29 15:06:41 
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

No.3 NTPが有効、ネットワークあり、NTPサーバーに繋がらない

NTPを有効化

$ sudo timedatectl set-ntp true

systemd-timesyncdサービスを再起動してNTPサーバの時刻に同期させる

$ sudo systemctl daemon-reload
$ sudo systemctl restart systemd-timesyncd

時刻確認

(NTPサーバーと同期されている)

$ timedatectl
               Local time: Tue 2022-03-29 23:13:41 JST
           Universal time: Tue 2022-03-29 14:13:41 UTC
                 RTC time: Tue 2022-03-29 14:13:42
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

RTCの時刻をずらす

$ sudo hwclock --set --date '2022-03-30 00:00:00.000000+09:00'

時刻確認

$ timedatectl
               Local time: Tue 2022-03-29 23:16:20 JST
           Universal time: Tue 2022-03-29 14:16:20 UTC
                 RTC time: Tue 2022-03-29 15:00:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

NTPサーバーを存在しないものに変更

/etc/systemd/timesyncd.conf.d/*.conf
[Time]
NTP=timetest.co.jp
#NTP=ntp.jst.mfeed.ad.jp ntp.nict.jp
#FallbackNTP=time.google.com

再起動

$ sudo reboot

時刻確認

System clock synchronized: noなので、NTPサーバーと同期できていない。
RTCモジュールの時刻で、Local timeが更新されていることを確認。

$ timedatectl
               Local time: Wed 2022-03-30 00:02:23 JST
           Universal time: Tue 2022-03-29 15:02:23 UTC
                 RTC time: Tue 2022-03-29 15:02:23
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

No.4 NTPが有効、ネットワークあり、NTPサーバーに繋がる

NTPを有効化

$ sudo timedatectl set-ntp true

systemd-timesyncdサービスを再起動してNTPサーバの時刻に同期させる

$ sudo systemctl daemon-reload
$ sudo systemctl restart systemd-timesyncd

時刻確認

(NTPサーバーと同期されている)

$ timedatectl
               Local time: Tue 2022-03-29 23:03:47 JST
           Universal time: Tue 2022-03-29 14:03:47 UTC
                 RTC time: Tue 2022-03-29 14:03:47
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

RTCの時刻をずらす

$ sudo hwclock --set --date '2022-03-30 00:00:00.000000+09:00'

時刻確認

RTCが1時間ずれてる

$ timedatectl
               Local time: Tue 2022-03-29 23:07:15 JST
           Universal time: Tue 2022-03-29 14:07:15 UTC
                 RTC time: Tue 2022-03-29 15:00:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

再起動

$ sudo reboot

時刻確認

RTC timeがシステム時刻で更新されていることを確認。
NTPサーバーと同期が入るとRTCモジュールの時刻を更新している。

$ timedatectl
               Local time: Tue 2022-03-29 23:09:03 JST
           Universal time: Tue 2022-03-29 14:09:03 UTC
                 RTC time: Tue 2022-03-29 14:09:03
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

NTPサーバーから時刻同期できる場合は、RTCモジュールの時刻も書き換えてくれていることがわかりました。
これで、Raspberry pi起動時に独自にRTCモジュールの時刻を更新するとかやらなくてよくなっています。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2