Cannot set RTC clock with hwclock



I can’t set the RTC clock with the command “sudo hwclock -w”. The steps are as follows:

sudo date -s “2019-11-26 12:00:00”
sudo hwclock -w
sudo hwclock -r

I used the command “sudo hwclock -r” to read the RTC time, but found that the RTC clock has not changed.


Have a try below command.

sudo timedatectl set-time ‘2019-11-26 12:00:00’

Hello ShaneCCC
thanks for your suggestion.

After running this command, the system prompts “Failed to set time: Automatic time synchronization is enabled”

Have below command to disable the auto time sync first.

sudo timedatectl set-ntp no

Hello ShaneCCC
thanks for your suggestion.

First disable automatic time synchronization with the command and then set the RTC time, so that the RTC clock can be modified successfully.

If i use the function to set RTC, how to operate /dev/rtc0?
I operate /dev/rtc0 with the following code, but I can’t set RTC

int rtc_set(struct rtc_time *rtc)
	int fd, retval;

	fd = open("/dev/rtc0", O_WRONLY);
	if (fd == -1)
		return -1;

	retval = ioctl(fd,RTC_SET_TIME, rtc);
	if (retval == -1)
		return -2;

	return 0;

Did you run it with supervisor mode?

Hello ShaneCCC

I run it with root privileges,How to use supervisor mode?

root privileges is what I said supervisor mode.
You may need to trace why the RTC_SET_TIME failed. Also have try to open /dev/rtc.

thanks for your suggestion.

I try to open /dev/rtc, and I can set RTC successfully.