系统时间设置

date 命令

[root@centos7 ~]# LANG=en_US.utf8 date
Thu Nov 10 11:30:59 CST 2022
# 设置为特定时间,字符串以英文格式表达
[root@centos7 ~]# date -s '2022年 11月 11日 星期四 11:30:10 CST'
date: 无效的日期"2022年 11月 11日 星期四 11:30:10 CST"
[root@centos7 ~]# date -s 'Thu Nov 11 11:30:59 CST 2022'
2022年 11月 11日 星期五 11:30:59 CST

hwclock 命令

# 读取硬件时钟
[root@centos7 ~]# hwclock -r
2022年11月11日 星期五 11时35分56秒  -0.320836 秒
# 将硬件时钟时间设置与系统时间一致
[root@centos7 ~]# hwclock -w
# 将系统时间设置与硬件时钟时间一致
[root@centos7 ~]# hwclock -s

timedatectl 命令

[root@centos7 ~]# timedatectl
Local time: 五 2022-11-11 11:38:00 CST
 Universal time: 五 2022-11-11 03:38:00 UTC
RTC time: 五 2022-11-11 03:38:07
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
DST active: n/a
# RTC real time clock,也就是硬件时钟时间。
# NTP enabled: yes,代表对时服务chronyd应开机自启。
# 关闭自动对时
[root@centos7 ~]# timedatectl set-ntp no
[root@centos7 ~]# timedatectl 
     Local time: Thu 2022-11-10 11:43:16 CST
 Universal time: Thu 2022-11-10 03:43:16 UTC
       RTC time: Thu 2022-11-10 03:43:16
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
     DST active: n/a
[root@centos7 ~]# LANG=en_US.utf8
[root@centos7 ~]# timedatectl set-time '2022-11-10 11:42:54'
# 如果自动对时未关闭,显示如下
[root@centos7 ~]# timedatectl set-time '2022-11-10 11:42:54'
Failed to set time: Automatic time synchronization is enabled
# 设置时区
[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai

tzselect 命令

查询时区名称。

[root@centos7 ~]# tzselect 
Please identify a location so that time zone rules can be set 
correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
 1) Afghanistan  18) Israel    35) Palestine
 2) Armenia  19) Japan    36) Philippines
 3) Azerbaijan  20) Jordan    37) Qatar
 4) Bahrain  21) Kazakhstan    38) Russia
 5) Bangladesh  22) Korea (North)    39) Saudi Arabia
 6) Bhutan  23) Korea (South)    40) Singapore
 7) Brunei  24) Kuwait    41) Sri Lanka
 8) Cambodia  25) Kyrgyzstan    42) Syria
 9) China  26) Laos    43) Taiwan
10) Cyprus  27) Lebanon    44) Tajikistan
11) East Timor  28) Macau    45) Thailand
12) Georgia  29) Malaysia    46) Turkmenistan
13) Hong Kong  30) Mongolia    47) United Arab Emirates
14) India  31) Myanmar (Burma)    48) Uzbekistan
15) Indonesia  32) Nepal    49) Vietnam
16) Iran  33) Oman    50) Yemen
17) Iraq  34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
 China
 Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Mon Jul 28 16:03:22 CST 2025.
Universal Time is now: Mon Jul 28 08:03:22 UTC 2025.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
 TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in 
again.
Here is that TZ value again, this time on standard output so that you
can use the /bin/tzselect command in shell scripts:
Asia/Shanghai

自动对时-chronyd 服务

# 修改对时服务器
[root@centos7 ~]# vim /etc/chrony.conf
# 与时间池对时
# 时间池是包含多个时间服务器的服务器组
pool 2.rocky.pool.ntp.org iburst
# 与单个服务器 ntp.aliyun.com 对时
server ntp.aliyun.com iburst
# 启用并启动chronyd服务
[root@centos7 ~]# systemctl enable chronyd --now
# 如果之前已经启动,需要重启
[root@centos7 ~]# systemctl restart chronyd
# 验证对时情况
[root@centos7 ~]# chronyc sources -v
 .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not 
combined,
| /             'x' = may be in error, '~' = too variable, '?' = 
unusable.
||                                                 .- xxxx [ yyyy ] +/- 
zzzz
||     Reachability register (octal) -.           | xxxx = adjusted 
offset,
||     Log2(Polling interval) --.     |         | yyyy = measured 
offset,
||                               \     |         | zzzz = estimated 
error.
||                                 |   |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample       
        
=======================================================================
========
^* 203.107.6.88                2   6    77     9  -1840us[-4378us] +/- 
  23ms

部署时间服务器

chrony 既可以作为客户端,也可以作为服务端(为客户端提供对时服务

服务端

[root@server ~]# vim /etc/chrony.conf
# 最后添加两条记录
# 配置监听地址
bindaddress 10.1.8.10
# 配置允许哪些网段主机同步
allow 10.1.8.0/24
[root@server ~]# systemctl restart chronyd
# 停止防火墙服务
[root@server ~]# systemctl stop firewalld.service

  客户端

# 修改对时服务器
[root@client ~]# vim /etc/chrony.conf
# 与单个服务器 10.1.8.10 对时
server 10.1.8.10 iburst
[root@client ~]# systemctl restart chronyd
[root@client ~]# chronyc sources -v
210 Number of sources = 1
 .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not 
combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too 
variable.
||                                                 .- xxxx [ yyyy ] +/- 
zzzz
||     Reachability register (octal) -.           | xxxx = adjusted 
offset,
||     Log2(Polling interval) --.     |         | yyyy = measured 
offset,
||                               \     |         | zzzz = estimated 
error.
||                                 |   |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample       
        
=======================================================================
========
^* server.laoma.cloud            3   6     7     1    -10us[  +43ms] 
+/-   76ms

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐