RTC
The Real-Time Clock (RTC) is a computer clock that keeps track of the current time, even when the system is powered off. It is typically used by the operating system to maintain time and date consistency. In Linux, the RTC can be accessed and managed via various commands and interfaces. Here's a detailed guide on how to use the RTC on a Linux machine.
1. Checking the RTC Time
You can check the current RTC time using the hwclock command, which interacts with the hardware clock:
sudo hwclock --show
This will display the current date and time stored in the RTC.
2. Sync System Time with RTC Time
If you want to synchronize the system time with the RTC time, you can use the following command:
sudo hwclock --hctosys
This command sets the system time from the RTC. The hctosys stands for "hardware clock to system."
3. Sync RTC Time with System Time
To set the RTC to match the current system time, use:
sudo hwclock --systohc
This command sets the hardware clock (RTC) from the system time. The systohc stands for "system to hardware clock."
4. Adjusting the RTC Time
If the RTC is not showing the correct time, you can manually set it:
sudo hwclock --set --date="YYYY-MM-DD HH:MM:SS"
Replace YYYY-MM-DD HH:MM:SS with the desired date and time.
5. Checking RTC Drift
Over time, the RTC might drift, causing it to become inaccurate. You can check how much the hardware clock has drifted compared to the system clock using:
sudo hwclock --compare
This command will show the difference between the system time and the RTC.
6. Understanding RTC with Time Zones
The RTC can be configured to use either local time or UTC (Coordinated Universal Time). By default, Linux prefers using UTC for the RTC. You can configure this preference in the /etc/adjtime file.
Checking the current configuration:
cat /etc/adjtime
The file will show something like:
0.000000 1629604786 0.000000
1629604786
UTC
The last line indicates whether the RTC is set to UTC or LOCAL.
Changing the RTC to Local Time:
If you prefer to set the RTC to local time instead of UTC:
sudo hwclock --localtime --systohc
This command will configure the RTC to use local time and synchronize it with the system clock.
Reverting to UTC:
If you want to revert back to using UTC:
sudo hwclock --utc --systohc
This will set the RTC to UTC and synchronize it with the system clock.
7. Enabling Automatic Synchronization
Most Linux distributions automatically synchronize the system clock with an NTP (Network Time Protocol) server. However, you can also ensure that the RTC is synchronized at startup by adding the hwclock command to your startup scripts.
For systems using systemd, you can enable time synchronization services:
sudo timedatectl set-ntp tr
This ensures the system clock is always synchronized with an NTP server, and by extension, the RTC remains accurate.
8. Dealing with Multiple RTCs
Some systems, especially servers, might have more than one RTC. You can list all RTCs available on your system using:
ls /dev/rtc*
If there are multiple RTCs, you can interact with a specific one using the hwclock command by specifying the device:
sudo hwclock --rtc=/dev/rtc1 --show
9. Checking Kernel Logs for RTC Messages
Sometimes, the kernel logs can provide additional information about the RTC and any issues it might be experiencing:
dmesg | grep rt
This will show any messages related to the RTC that were logged during system startup.
10. Hardware Support and RTC Drivers
Linux supports various RTC chips through kernel drivers. You can check if the RTC driver is loaded and working by inspecting the output of:
dmesg | grep rtc
You can also check the loaded kernel modules:
lsmod | grep rtc
If the RTC driver is not loaded, you may need to manually load it or compile it into your kernel.
Conclusion
This guide covers the essential tasks for managing the RTC on a Linux system, including checking, synchronizing, and adjusting the clock. By using these commands and configurations, you can ensure that the RTC remains accurate, providing reliable timekeeping even when your system is powered off.