「掌握Linux服务器时间,实用命令大全」 (linux服务器时间命令)

掌握Linux服务器时间,实用命令大全

在Linux服务器上,时间是非常重要的一个因素。正确地设置时间可以确保更好地管理计算机系统——尤其是当要进行日志统计和备份等操作时。在这篇文章中,我们将为您介绍一些实用的命令,以帮助您在Linux服务器上轻松掌握时间。让我们开始吧!

1. date

Date命令是一个用于显示当前系统日期和时间的基本命令。只需在终端中输入“date”命令,即可显示当前系统时间和日期。例如:

“`

$ date

Wed Aug 18 17:35:31 CST 2023

“`

2. cal

Cal命令用于显示日历。只需在终端中输入“cal”命令,即可显示当前系统月份的日历。例如:

“`

$ cal

August 2023

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

“`

3. hwclock

Hwclock命令用于设置或读取系统硬件时钟的时间。只需在终端中输入“hwclock”命令,即可显示硬件时钟的时间。

要将系统时间与硬件时钟同步,可以使用以下命令:

“`

$ sudo hwclock –systohc

“`

4. timedatectl

Timedatectl命令用于设置和显示当前时间和日期的设置。只需在终端中输入“timedatectl”命令,即可显示当前时间和日期的设置。例如:

“`

$ timedatectl

Local time: Wed 2023-08-18 17:43:36 CST

Universal time: Wed 2023-08-18 09:43:36 UTC

RTC time: Wed 2023-08-18 09:43:36

Time zone: Asia/Shangh (CST, +0800)

System clock synchronized: yes

systemd-timesyncd.service active: yes

RTC in local TZ: no

“`

要更改系统时区,可以使用以下命令:

“`

$ sudo timedatectl set-timezone Asia/Shangh

“`

5. ntpdate

Ntpdate命令用于从NTP服务器上获取时间,并将其设置为系统时间。只需在终端中输入“ntpdate”命令,加上NTP服务器的地址,即可从该服务器获取时间。例如:

“`

$ sudo ntpdate ntp.ubuntu.com

“`

6. chronyc

Chronyc是一个时间管理工具,用于访问和管理时间服务器。只需在终端中输入“chronyc”命令,即可访问Chrony的配置文件。例如:

“`

$ sudo chronyc

chrony version 3.5

Copyright (C) 1997-2023, Linus Torvalds, Ingo Molnar, and others

chrony comes with ABSOLUTE NO WARRANTY. This is free software, and

you are welcome to redistribute it under the terms of the GNU Lesser

General Public License as published by the Free Software Foundation;

either version 2.1 of the License, or (at your option) any later version.

.

chronyc> sources

210 Number of sources = 3

MS Name/IP address Stratum Poll Reach LastRx Last sample

===============================================================================

^+ ntp.ubuntu.com 2 6m 377 24 +2906us[+2236us] +/- 51ms

^* ntp1.jst.mfeed.ad.jp 2 6m 377 25 -139us[ -106us] +/- 81ms

^+ ntp2.jst.mfeed.ad.jp 2 6m 377 36 -380us[ -349us] +/- 71ms

chronyc>

“`

7. tzselect

Tzselect命令用于选择系统时区。只需在终端中输入“tzselect”命令,然后按照提示进行操作即可。例如:

“`

$ tzselect

Please identify a location so that time zone rules can be set correctly.

Please select a continent, ocean, “coord”, or “TZ”.

1) Africa

2) Americas

3) Antarctica

4) Asia

5) Atlantic Ocean

6) Australia

7) Europe

8) Indian Ocean

9) Pacific Ocean

10) coord – I want to specify the coordinates.

11) TZ – I want to specify the time zone using the Posix TZ format.

#? 4

Please select a country whose clocks agree with yours.

……

Please enter an integer in the range 1 to 25.

#? 1

“`

8. timedatectl set-time

Timedatectl set-time命令用于设置系统时间。只需在终端中输入“timedatectl set-time”命令,加上所需的时间值(以YYYY-MM-DD HH:MM:SS格式)即可。例如:

“`

$ sudo timedatectl set-time “2023-08-18 18:00:00”

“`


数据运维技术 » 「掌握Linux服务器时间,实用命令大全」 (linux服务器时间命令)