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

Hello,

I run the docker container with “CAP_SYS_TIME” and running “date” command from my app (user ‘torizon’) changes the time on host.
But on reboot, the host has the old date/time (the device is not connected to the internet, so no ntp-server overrides the time).
What do I wrong? Do I need to use another command for changing the RTC of the system?

Torizon OS is 6.8 (latest)

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

@ToradexAI
timedatectl is not available, I tried hwclock. The output from hwclock is:

Trying to open: /dev/rtc0
hwclock: cannot open /dev/rtc0: Permission denied
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.

The /dev folder is binded to the container. Any tips?

Thanks for your response.

Please remember to tick the “Solution” checkbox below if my response solves your question. If you have follow-up questions, just continue to tag me using @ToradexAI in your new posts.

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


Container RTC Access Issue

The permission denied error indicates that your container needs additional device access to interact with the RTC devices. Even with /dev bind-mounted and CAP_SYS_TIME, you need to explicitly grant access to the RTC device files.

Solution: Grant RTC Device Access

You need to add the --device flag to your Docker run command to grant access to the RTC devices(1). Here’s how to modify your container startup:

docker run -it --rm --device /dev/rtc0 --device /dev/rtc1 --cap-add=SYS_TIME <your_container>

Alternative: Grant Access to All RTC Devices

If you have multiple RTC devices and want to access all of them, you can use device cgroup rules(1):

docker run -it --rm -v /dev:/dev --device-cgroup-rule='c 254:* rmw' --cap-add=SYS_TIME <your_container>

Identifying Available RTC Devices

First, check which RTC devices are available on your system by running this command on the host:

ls -la /dev/rtc*

On Toradex modules, you’ll typically see multiple RTC devices. For example, on i.MX 8M Mini/Plus based modules, you’ll find(2):

  • rtc0 - Internal low power RX8130CE real-time clock
  • rtc1 - SoC internal RTC

Testing RTC Access

Once you’ve granted proper device access, test the RTC functionality:

# Read hardware clock
hwclock -r

# Set hardware clock from system time  
hwclock -w

# Set system time from hardware clock
hwclock -s

Specifying a Particular RTC Device

If you need to work with a specific RTC device, you can use the -f flag with hwclock(2):

hwclock -r -f /dev/rtc0
hwclock -w -f /dev/rtc0

The key issue was that even with /dev bind-mounted, containers need explicit device access permissions for hardware devices like RTC. The --device flag or device cgroup rules provide the necessary access rights for your container to interact with the RTC hardware.

Best regards, Toradex AI Assistant.

Hello @Lars.ceta !

The AI response is correct, using date only changes the kernel/system clock, not the hardware RTC. To make the change persistent across reboots, you’ll need to write the updated system time back to the RTC with hwclock.

Regarding the permission errors, the second message from the bot is also correct: you’ll need to explicitly pass the RTC device into your container (--device /dev/rtc0) and run the command with sufficient privileges.

If you try this and still face issues, please share the outputs of ls -l /dev/rtc* and dmesg | grep -i rtc from the host so we can guide you further.