When you need to modify the Linux system time, in addition to the time settings at the software level, you also need to consider the impact of the hardware clock (RTC).
(1) Check the current time and time zone
Before starting modification, use the following command to confirm the current system time and time zone settings:
datetimedatectl
(2) Set system time
Method 1. Manual modification
If you need to manually adjust the system time, you can use the date command. For example, to adjust the system time to 12:00 on February 21, 2024, you can execute the following command:
sudo date +%Y%m%d%H%M -s "202402211200"
Method 2. Use Network Time Protocol (NTP)
It is recommended to use Network Time Protocol (NTP) to ensure system time synchronization. Please confirm that the NTP service has been installed and running on the system, and then execute the corresponding command to manually synchronize time.
sudo ntpdate time.nist.gov
Alternatively, configure the NTP client to automatically synchronize time:
sudo timedatectl set-ntp true
(3) Modify time zone settings
If you need to modify the system's time zone settings, you can use the timedatectl command. For example, set the time zone to Beijing time (East Eighth District):
sudo timedatectl set-timezone Asia/Shanghai
In addition to software-level time settings, the impact of the hardware clock (RTC) also needs to be considered. Hardware clocks typically run continuously to keep time even when the computer is turned off or loses power. Therefore, it is necessary to ensure the consistency of software time and hardware time.
(1) Write software time to hardware clock
Use the following command to write the software time to the hardware clock to ensure that they are consistent:
sudo hwclock --systohc
(2) Read time from hardware clock
If you need to read the time from the hardware clock to synchronize the system time, you can use the following command:
sudo hwclock --hctosys
After completing the modification of the time and time zone, run the date, timedatectl and hwclock commands again to verify whether the modification takes effect.
The above is the detailed content of Why does changing the Linux system time not take effect?. For more information, please follow other related articles on the PHP Chinese website!