How to use the command line to change the date and time of the Linux system

  

The time of the Linux system is sometimes inaccurate. Inaccurate time will cause errors in the time in the log file, so the exact time is very important, then How do I change the time of my Linux system? The following small series introduces you to the steps to change the Linux system time and date using the command line.

Question: On Linux, how can I change the date and time via the command line?

Keeping date and time synchronized on Linux systems is an important responsibility for every Linux user and system administrator. Many programs rely on accurate time information to work properly. In addition, inaccurate dates and times can make timestamps in log files meaningless, reducing their role in system inspection and troubleshooting. For production systems, precise dates and times are even more important. For example, in a retail company, all products must be accurately counted (and stored in a database server) at all times to allow the finance department to calculate daily and weekly, monthly, and yearly expenses and revenue.

We must note that there are two kinds of clocks on a Linux machine: the software clock maintained by the kernel (also known as the system clock) and the (battery-powered) hardware clock that records the time after the machine is turned off. At startup, the kernel synchronizes the system clock with the hardware clock. After that, the two clocks operate independently of each other.

Method One: Date command

In Linux, you can change the date and time of the system by the date command:

# date --set = & lsquo ;NEW_DATE’

where NEW_DATE is a date string such as "Sun, 28 Sep 2014 16:21:42" or ""2014-09-29 16:21:42”

Date formats can also be manually specified for more accurate results:

# date +FORMAT --set=‘NEW_DATE’

For example:

# date + & rsquo;% Y% m% d% H% m & rsquo; --set = & lsquo; 20140928 1518 & rsquo;

you can also use the opposite manner to increase or decrease certain number of days, Weeks, months and seconds, minutes, hours. You can also put the date and time parameters into a single command.

# date --set=‘+5 minutes’# date --set=‘-2 weeks’# date --set=‘+3 months’# date --set=‘ -3 months +2 weeks -5 minutes & rsquo;

Finally, the hardware clock to the current system clock:

# hwclock --systohc

run hwclock - The purpose of -systohc is to synchronize the hardware clock with the software clock, which corrects the system drift of the hardware clock (that is, the clock goes faster or slower at a certain speed).

On the other hand, if the hardware clock is correct but the system clock is incorrect, you can correct it with the following command:

# hwclock --hctosys

In this case, the hwclock command synchronizes the two clocks. Otherwise, the time after the restart will be wrong, because the hardware clock will remember the time when the power is turned off. However, this does not apply to virtual machines because the virtual machine does not have access to the hardware clock.

Method 2: NTP

Another way to keep the system date and time accurate is to use NTP (Network Time Protocol). On Linux, the ntpdate command synchronizes the system clock with a public NTP server via NTP.

You can install ntpdate using the following command:

On Debian and Debian-based distributions:

# aptitude install ntpdate

Based on Ret On the distribution of Hat:

# yum install ntpdate

Synchronizing the system clock with NTP:

# ntpdate -u "NTP server name or IP"# hwclock --systohc

in addition to one-time use ntpdate to synchronize the clock, you can also use the NTP daemon (ntpd), it will always run in the background, continually adjust the system clock via NTP.

The above is a way to change the date and time of the Linux system using the command line. If you want to use the command line to modify the Linux system, try the method steps described above.

Copyright © Windows knowledge All Rights Reserved