Change host date/time (RTC) from inside docker container

Hi, I’m the Toradex AI Assistant.

I might make mistakes. Our support team will step in if needed. If I answered your question, please tick the “Solution” checkbox. For follow-up questions to me, please use @ToradexAI in your post.

Did you like my answer? :+1: :-1:


RTC Time Persistence Issue

The issue you’re experiencing is common when the system time changes aren’t being synchronized to the hardware Real-Time Clock (RTC). When you change the time using the date command, it only updates the system time but doesn’t automatically write to the hardware RTC(1).

Solution: Synchronize System Time to Hardware RTC

To ensure the time persists after reboot, you need to write the system time to the hardware RTC using the hwclock command(1).

After changing the time with the date command in your container (with CAP_SYS_TIME), run:

hwclock -w

This command writes the current system time to the hardware RTC(1).

Alternative: Using timedatectl

You can also use timedatectl to set both system time and RTC simultaneously:

timedatectl set-time "YYYY-MM-DD HH:MM:SS"

This approach sets both the system time and synchronizes it to the RTC in one command(2).

Verification Steps

  1. Check current RTC time: hwclock -r(2)
  2. Set system time: date -s "your desired time"
  3. Write to RTC: hwclock -w(1)
  4. Verify RTC updated: hwclock -r(2)

Troubleshooting

If you encounter “Failed to read RTC: Invalid argument” errors after reboot, this typically indicates RTC synchronization issues(2). The solution is to ensure proper time synchronization as described above.

The key point is that changing the system time alone (via date command) doesn’t persist across reboots - you must explicitly write to the hardware RTC using hwclock -w or use timedatectl for persistent time changes(1)(2).

Best regards, Toradex AI Assistant.

1 Like