date

date 命令

1
2
[root@ClockworkMonkey ~]# date
Tue Mar 29 22:58:19 CST 2022

时区

  • UTC:世界标准时间
  • GMT:格林威治标准时间
  • CST:中国标准时间,GMT + 8 = UTC + 8 = CST

格式

1
2
3
4
5
6
7
8
%F:显示年月日
%y:年份的最后两位数字,范围是00 ~ 99
%Y:年份
%m:月份,范围01 ~ 12
%d:当前是几号,按月计算的日期
%M:分钟
%H:小时
%S:秒
1
2
3
4
5
6
[root@ClockworkMonkey ~]# date "+%F"
2022-03-29
[root@ClockworkMonkey ~]# date "+%Y%m%d"
20220329
[root@ClockworkMonkey ~]# date "+%Y-%m-%d %H:%M:%S"
2022-03-29 23:00:41

修改系统时间

1
2
[root@ClockworkMonkey ~]# date -s "2019-09-09 09:09:09"
Mon Sep 9 09:09:09 CST 2019

timedatectl 命令

timedatectl

1
2
3
4
5
6
7
8
9
[root@ClockworkMonkey ~]# timedatectl
Local time: Mon 2019-09-09 09:10:00 CST
Universal time: Mon 2019-09-09 01:10:00 UTC
RTC time: Tue 2022-03-29 15:02:24
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

timedatectl set-timezone Asia/Shanghai

1
[root@ClockworkMonkey ~]# timedatectl set-timezone Asia/Shanghai

NTP

安装 ntpdate

1
2
3
4
5
[root@ClockworkMonkey ~]# yum install ntpdate
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package ntpdate-4.2.6p5-29.el7.centos.2.x86_64 already installed and latest version
Nothing to do

同步时间

1
2
3
4
5
6
7
root@localhost [10:27:41 AM] [~] 
-> # ntpdate cn.pool.ntp.org
29 Apr 10:27:55 ntpdate[62380]: adjust time server 124.108.20.1 offset -0.000588 sec
root@localhost [10:27:55 AM] [~]
-> # ntpdate -u cn.pool.ntp.org
29 Apr 10:28:08 ntpdate[62383]: adjust time server 119.28.206.193 offset 0.002792 sec

开机启动

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
32
root@localhost [10:28:08 AM] [~] 
-> # systemctl status ntpdate
● ntpdate.service - Set time via NTP
Loaded: loaded (/usr/lib/systemd/system/ntpdate.service; disabled; vendor preset: disabled)
Active: inactive (dead)
root@localhost [10:31:08 AM] [~]
-> # systemctl start ntpdate
root@localhost [10:31:28 AM] [~]
-> # systemctl status ntpdate
● ntpdate.service - Set time via NTP
Loaded: loaded (/usr/lib/systemd/system/ntpdate.service; disabled; vendor preset: disabled)
Active: active (exited) since Thu 2021-04-29 10:31:28 CST; 2s ago
Process: 62427 ExecStart=/usr/libexec/ntpdate-wrapper (code=exited, status=0/SUCCESS)
Main PID: 62427 (code=exited, status=0/SUCCESS)

Apr 29 10:31:23 localhost.localdomain systemd[1]: Starting Set time...
Apr 29 10:31:28 localhost.localdomain systemd[1]: Started Set time ...
Hint: Some lines were ellipsized, use -l to show in full.

root@localhost [10:31:43 AM] [~]
-> # systemctl enable ntpdate
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpdate.service to /usr/lib/systemd/system/ntpdate.service.
root@localhost [10:31:58 AM] [~]
-> # systemctl status ntpdate
● ntpdate.service - Set time via NTP
Loaded: loaded (/usr/lib/systemd/system/ntpdate.service; enabled; vendor preset: disabled)
Active: active (exited) since Thu 2021-04-29 10:31:28 CST; 41s ago
Main PID: 62427 (code=exited, status=0/SUCCESS)

Apr 29 10:31:23 localhost.localdomain systemd[1]: Starting Set time...
Apr 29 10:31:28 localhost.localdomain systemd[1]: Started Set time ...
Hint: Some lines were ellipsized, use -l to show in full.

参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@nginx02 ~]# ntpdate cn.pool.ntp.org
1 Apr 11:06:48 ntpdate[1516]: step time server 84.16.67.12 offset -28798.752783 sec
[root@nginx02 ~]# systemctl status ntpdate
● ntpdate.service - Set time via NTP
Loaded: loaded (/usr/lib/systemd/system/ntpdate.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@nginx02 ~]# systemctl start ntpdate
[root@nginx02 ~]# systemctl status ntpdate
● ntpdate.service - Set time via NTP
Loaded: loaded (/usr/lib/systemd/system/ntpdate.service; disabled; vendor preset: disabled)
Active: active (exited) since Fri 2022-04-01 11:07:20 CST; 1s ago
Process: 1524 ExecStart=/usr/libexec/ntpdate-wrapper (code=exited, status=0/SUCCESS)
Main PID: 1524 (code=exited, status=0/SUCCESS)

Apr 01 11:07:17 nginx02.centos.gg systemd[1]: Starting Set time via NTP...
Apr 01 11:07:20 nginx02.centos.gg systemd[1]: Started Set time via NTP.
[root@nginx02 ~]# systemctl enable ntpdate
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpdate.service to /usr/lib/systemd/system/ntpdate.service.