How to check system time in Linux

System time is the basis for the operation of computer hardware and software. In a Linux system, checking the system time is a basic task. This article will introduce the detailed method of checking the system time in Linux from many aspects.

1. Use the date command to check the system time

Date is a command used in Linux systems to display or set the system time and date. It is often used in scripting, system management and other operations.

date

Executing the above command, the system will output the current system time in the format of "week, month, day, hour:minute:second, time zone, year", for example:

Fri Oct 15 14:56:34 CST 2021

To view the system time at a specific moment, you can use the date command with the time parameter. For example, to view the system time at midnight on January 1, 2021, you can use the following command:

date -d "2021-01-01 00:00:00"

If you want to obtain the system time and perform operations in a script, you can use the date command combined with the formatting parameter to format the system time into a specified format. For example, to get the hour of the current system time, you can use the following command:

date +%H

2. Check the system time through the /var/log/syslog file

/var/log/syslog is a file in the Linux system that saves system log information, which contains detailed information about various system events.

To see when the system was started, look for the "systemd[1]: Started" string in the /var/log/syslog file, which indicates when the system was started.

grep "systemd[1]: Started" /var/log/syslog

After executing the above command, the system will output log information including the system startup time.

3. Check the system running time through the /proc/uptime file

The /proc/uptime file records the running time of the system from startup to the current time.

To see how long your system has been up, you can use the following command:

cat /proc/uptime

The system will output two numbers. The first number indicates the running time of the system from startup to the present, in seconds; the second number indicates the running time of the system in idle state, in seconds.

4. Check the hardware time through the hwclock command

hwclock is the command used in Linux systems to read and set the hardware clock.

To view the hardware time, you can use the following command:

sudo hwclock --show

Executing the above command, the system will output the current hardware time.

5. Set the system time zone through the timedatectl command

timedatectl is a command used in Linux systems to view and set system time, time zone and other information.

To view the current system time zone, you can use the following command:

timedatectl

To set your system's time zone, you can use the following command:

sudo timedatectl set-timezone Asia/Shanghai

Execute the above command to set the system's time zone to Shanghai.

6. Synchronize network time through NTP protocol

NTP (Network Time Protocol) is a protocol used to synchronize network time.

To synchronize network time via NTP protocol, you can use the following command:

sudo apt-get install ntp
sudo service ntp stop
sudo ntpdate cn.pool.ntp.org
sudo service ntp start

Executing the above command, the system will synchronize the network time through the Chinese NTP server cn.pool.ntp.org.

7. Check NTP synchronization status through ntpstat command

ntpstat is a command used in Linux systems to check the NTP synchronization status.

To check the NTP synchronization status, you can use the following command:

ntpstat

The system will output the current NTP synchronization status, for example:

synchronised to NTP server (210.72.145.44) at stratum 2
   time correct to within 34 ms
   polling server every 64 s

8. Use the cal command to view the calendar

cal is the command used to display the calendar in Linux systems.

To view the calendar for the current month, you can use the following command:

cal

The system will output the calendar for the current month.

9. Use the clock command to display the clock

clock is the command used to display the clock in Linux systems.

To display the clock in the terminal, you can use the following command:

watch -n1 -t "date +%T"

After executing the above command, the system will refresh the system time in the terminal every second.

10. Summary

This article introduces a variety of methods for viewing the system time in Linux systems, from the date command, system log file, /proc/uptime file, hwclock command, timedatectl command, NTP protocol, ntpstat command, cal command, clock command, etc. . These methods can not only help us understand the system time and date, but also facilitate script writing, system management and other operations.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/132766628