linux时间

使用linux时,时间这一块不是很清楚,本篇博客记录网上搜集的教程并整理

时间概述

首先需要清楚两个时间概念:硬件时间系统时间

windows中的时间

Windows中的系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不间断。

linux系统中的时间

linux系统在默认情况下,系统时间和硬件时间并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行,是靠BIOS电池来维持,而系统时间,是用CPU Tick来维持的。在系统开机的时候,会自动从BIOS中取得硬件时间,设置为系统时间。

date

date命令方便查看当前系统时间和设置系统时间

  • 查看系统时间
1
2
3
4
5
6
root@linux:~# date       # 打印当前时区时间
Sat Jul 3 17:10:02 CST 2021
root@linux:~# date "+%s" # 显示当前时间戳
1625303413
root@linux:~# date "+Time Now: %Y-%m-%d %H:%M:%S" # 按指定格式打印时间
Time Now: 2021-07-03 17:11:22
  • 设置系统时间
1
date -s "20210703 17:11:22"

hwclock

硬件时间的设置,可以用hwclock或者clock命令。两者基本相同,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

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
root@linux:~# hwclock -h

Usage:
hwclock [function] [option...]

Time clocks utility.

Functions:
-r, --show display the RTC time
--get display drift corrected RTC time
--set set the RTC according to --date
-s, --hctosys set the system time from the RTC
-w, --systohc set the RTC from the system time
--systz send timescale configurations to the kernel
-a, --adjust adjust the RTC to account for systematic drift
--predict predict the drifted RTC time according to --date

Options:
-u, --utc the RTC timescale is UTC
-l, --localtime the RTC timescale is Local
-f, --rtc <file> use an alternate file to /dev/rtc0
--directisa use the ISA bus instead of /dev/rtc0 access
--date <time> date/time input for --set and --predict
--update-drift update the RTC drift factor
--noadjfile do not use /etc/adjtime
--adjfile <file> use an alternate file to /etc/adjtime
--test dry run; implies --debug
-D, --debug display more details

-h, --help display this help
-V, --version display version
  • 查看硬件时钟

    1
    2
    root@linux:~# hwclock --show
    2021-07-03 17:17:35.708633+080
  • 同步系统时间到硬件时间

    1
    2
    3
    4
    5
    [root@controller ~]# hwclock
    Thu 06 Apr 2017 11:46:35 AM CST -0.028650 seconds
    [root@controller ~]# hwclock -w
    [root@controller ~]# hwclock
    Thu 06 Apr 2017 11:47:00 AM CST -0.223930 seconds
  • 同步硬件时间到系统时间

    1
    2
    3
    4
    5
    [root@controller ~]# date
    Thu Apr 6 11:48:31 CST 2017
    [root@controller ~]# hwclock -s
    [root@controller ~]# date
    Thu Apr 6 11:48:41 CST 2017
  • 修改硬件时间

    1
    2
    3
    [root@controller ~]# hwclock --set --date="12/12/16 10:19"
    [root@controller ~]# hwclock
    Mon 12 Dec 2016 10:19:05 AM CST -0.107052 seconds

timedatectl

timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系统和服务管理器的一部分,代替旧的传统的用在基于Linux分布式系统的sysvinit守护进程的date命令。timedatectl命令可以查询和更改系统时钟和设置,你可以使用此命令来设置或更改当前的日期,时间和时区,或实现与远程NTP服务器的自动系统时钟同步。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root@linux:~# timedatectl -h
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
status Show current time settings
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization

  • 查看当前时间设置

    1
    2
    3
    4
    5
    6
    7
    8
    root@linux:~# timedatectl status
    Local time: Sat 2021-07-03 17:26:02 CST
    Universal time: Sat 2021-07-03 09:26:02 UTC
    RTC time: Sat 2021-07-03 09:26:06
    Time zone: Asia/Shanghai (CST, +0800)
    System clock synchronized: yes
    systemd-timesyncd.service active: yes
    RTC in local TZ: no
    name description
    Local time 本地时区时间
    Universal time 世界协调时
    RTC time 硬件时间(real-time clock)
    Time zone 当前设置的时区
    System clock synchronized 如果和远程NTP服务器成功同步,显示为yes
    systemd-timesyncd.service active NTP时间同步是否开启,yes表示是,systemd-timesyncd服务为活动也就是开启了NTP时间同步
    RTC in local TZ no表示硬件时钟设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间
  • 设置系统时间

    1
    2
    3
    timedatectl set-time 15:58:30  #只设置时间
    timedatectl set-time 20151120 #只设置日期
    timedatectl set-time '16:10:40 2015-11-20' #同时设置日期和时间
  • 显示可用时区

    1
    timedatectl list-timezones
  • 设置系统时区

    1
    2
    timedatectl set-timezone "Asia/Shanghai"
    timedatectl set-timezone UTC
  • 设置硬件时钟时区

    1
    2
    timedatectl set-local-rtc 0 #将硬件时钟设置为协调世界时
    timedatectl set-local-rtc 1 #将硬件时钟设置为本地时区
  • 设置系统时间同步到远程NTP服务器

    1
    2
    timedatectl set-ntp true # 开启同步
    timedatectl set-ntp false # 关闭同步

ntpdate

NTP即Network Time Protocol(网络时间协议),是一个互联网协议,用于同步计算机之间的系统时钟。

为了避免主机时间因为长期运行下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。

Linux系统下,一般使用ntp服务器来同步不同机器的时间。一台机器,可以同时是ntp服务端和ntp客户端。在生产系统中,推荐使用像DNS服务器一样分层的时间服务器来同步时间。

不同机器间同步时间,可以使用ntpdate命令,也可以使用ntpd服务。

1
2
3
[root@node1 ~]# ntpdate [NTP IP/hostname]
[root@node1 ~]# ntpdate 192.168.0.1
[root@node1 ~]# ntpdate time.ntp.org

常见问题

1. timedatectl

报错1

1
2
$ timedatectl
Failed to create bus connection: No such file or directory

解决方案:

1
apt-get install dbus

参考博客

  1. https://www.cnblogs.com/zhi-leaf/p/6282301.html
  2. https://www.jianshu.com/p/da8f9b6e832f
  3. https://www.cnblogs.com/williamjie/p/10768657.html
  4. https://www.cnblogs.com/pipci/p/12833228.html