系统时间查看与设置
Linux系统最直接的时间查看方法是date命令。
[root@CentOS79 ~]# date
2025年 11月 05日 星期三 18:38:09 CST
date命令的完整语法为:date [选项] +[时间格式];
date的作用对象为系统时间(System Time);系统时间是操作系统内核维护的软件时钟,基于硬件时间初始化;但系统时间可通过网络协议动态校准,所以精度可以比硬件时间更高。
同时,系统时间是可以被更改的;如date命令的-s选项就可以自由设置系统时间。
| date命令常用选项 | 作用 |
|---|---|
| -d | 显示指定字符串描述的时间 |
| -s | 设置时间为指定字符串 |
| -R | 以RFC 5322格式输出 |
| -I | 以ISO 8601格式输出 |
| +[时间格式] | 按指定格式显示 |
时间格式设置中;%Y表示年,%m表示月,%d表示天,%H表示小时,%M表示分钟,%S表示秒。
[root@CentOS79 ~]# date
2025年 11月 05日 星期三 19:43:54 CST
[root@CentOS79 ~]# date -s "2024-01-01 12:00:00"
2024年 01月 01日 星期一 12:00:00 CST
[root@CentOS79 ~]# date +"%Y-%m-%d %H:%M:%S"
2024-01-01 12:00:10
[root@CentOS79 ~]# date -d "next Friday"
2024年 01月 05日 星期五 00:00:00 CST
CST代表 中国标准时间,在北美也指 中部标准时间;即UTC+8时区(东八区)。
除此之外还有以下常见时区符号:
- UTC:协调世界时,全球基准时间;
- GMT:格林尼治标准时间,与UTC基本等价;
- EST:东部标准时间,UTC-5;
- PST:太平洋标准时间,UTC-8;
- IST:印度标准时间,UTC+5:30;
- JST:日本标准时间,UTC+9;
- EDT:东部夏令时 ,UTC-4;
- PDT:太平洋夏令时,UTC-7;
- CDT:中部夏令时,UTC-5;
由于时区符号可能因地区重复,如上方CST可指中国或北美中部时间,所以时间系统会用明确时区名或偏移量避免歧义。
使用timedatectl命令可以查看更详细的时间信息,timedatectl命令是systemd的时间日期管理工具;
语法:timedatectl [选项] [命令];
| timedatectl常用命令及选项 | 作用 |
|---|---|
| status | 显示当前时间日期状态 |
| set-time [时间] | 设置系统时间 |
| set-timezone [时区] | 设置系统时区 |
| list-timezones | 列出所有可用时区 |
| set-ntp [yes/no] | 启用/禁用NTP时间同步 |
| –no-ask-password | 免密码执行 |
| -H, --host= | 远程操作 |
[root@CentOS79 ~]# timedatectl
Local time: 三 2025-11-05 19:14:18 CST
Universal time: 三 2025-11-05 11:14:18 UTC
RTC time: 三 2025-11-05 11:14:18
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
其中 RTC time 被称为硬件时间(Real-Time Clock);硬件时间是存储在计算机主板 CMOS芯片 中的物理时钟,由纽扣电池供电,即使断电也能持续运行,其本质是独立的计时电路。
系统启动时会从硬件时间加载初始值;系统在运行时,会通过NTP协议同步网络时间。
在Linux系统中,可以通过hwclock读取硬件时间:
[root@CentOS79 ~]# hwclock --show
2025年11月05日 星期三 19时38分01秒 -0.466202 秒
hwklock是专门访问硬件时间的命令,可以将系统时间设置与硬件时钟时间一致(或反之)。
语法:hwclock [选项];
| hwclock命令常用选项 | 作用 |
|---|---|
| -r --show | 读取并显示硬件时钟 |
| -s --hctosys | 将硬件时钟时间同步到系统时间 |
| -w --systohc | 将系统时间写入硬件时钟 |
| –set --date=“STRING” | 设置硬件时钟时间 |
| –localtime | 硬件时钟使用本地时间 |
[root@CentOS79 ~]# hwclock -r
2025年11月05日 星期三 20时04分39秒 -0.044695 秒
[root@CentOS79 ~]# hwclock -s
[root@CentOS79 ~]# date
2025年 11月 05日 星期三 20:04:49 CST
date、hwclock和timedatectl都是Linux系统中用于查看和设置时间/日期的命令工具,三者各司其职;date用于基本的日期时间操作,hwclock用于硬件时钟管理,timedatectl则提供更现代化的系统时间管理功能。
时区环境变量
可以通过tzselect命令查询指定时区的环境变量更改命令:
[root@CentOS79 ~]# 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: Wed Nov 5 21:36:39 CST 2025.
Universal Time is now: Wed Nov 5 13:36:39 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 /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
[root@CentOS79 ~]# TZ='Asia/Shanghai'; export TZ
tzselect支持选择需要的时区,并自动输出环境变量修改命令,命令在当前shell会话中立即生效,仅影响当前用户环境;
如上方TZ=‘Asia/Shanghai’; export TZ 这条命令用于设置当前shell会话的时区环境变量,将系统的时区设置为亚洲/上海时区(北京时间),只在当前打开的终端会话中有效;
该shell启动的所有子进程都会继承这个时区设置,也会影响date等命令的时间显示;
也可以使用echo “TZ=‘Asia/Shanghai’; export TZ” >>~/.profile命令将时区信息永久写入环境变量中,并使用source ~/.profile立即生效。
如果想要使时区变量对所有用户生效,需要使用timedatectl命令进行修改;如timedatectl set-timezone Asia/Shanghai;
自动校准与授时服务器
在timedatectl命令输出的时间信息中,还显示了时区和对时服务的状态等信息;
- Time zone:时区;当前系统设置的时区;
- NTP enabled:NTP服务状态;是否启用了网络时间协议同步;
- NTP synchronized:NTP同步状态;是否已成功通过NTP同步时间;
- RTC in local TZ:硬件时钟时区设置;硬件时钟是否使用本地时区;
- DST active:夏令时状态;是否启用夏令时,n/a表示不适用;
Linux系统时间可以通过NTP协议进行校准,即将本地的时间与设定的授时对象进行同步;校准服务通常称为 chronyd 服务。
[root@CentOS79 ~]# timedatectl set-ntp yes
[root@CentOS79 ~]# timedatectl
Local time: 三 2025-11-05 22:13:33 CST
Universal time: 三 2025-11-05 14:13:33 UTC
RTC time: 三 2025-11-05 14:13:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
如果设置了时区环境变量,可能出现 Warning: Ignoring the TZ variable. Reading the system’s time zone setting only. 警告,这是因为timedatectl 命令选择忽略TZ 环境变量,并直接读取系统的时区设置。
可以用unset TZ删除环境变量,然后使用 timedatectl set-timezone Asia/Shanghai 命令设置时区。
NTP enabled显示为yes,代表允许校准时间;但NTP synchronized: no表示还没有进行校准。此时可以在 /etc/chrony.conf 文件中设置授时对象。
[root@CentOS79 ~]# cat /etc/chrony.conf | head -n 6
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
/etc/chrony.conf的前6行代表时间池,时间池是包含多个时间服务器的服务器组;
这时可以修改授时对象,自定义授时服务器;下面以单个服务器ntp.aliyun.com为例。
修改/etc/chrony.conf:
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
完成后启动chronyd服务;如果已经启动则需要重启;
[root@CentOS79 ~]# systemctl enable chronyd --now
#重启
[root@CentOS79 ~]# systemctl restart chronyd
之后运行chronyc sources -v就可以进行校准结果查询;
最下方的IP地址表示校准对象,如果IP前是*号则代表校准成功。
[root@CentOS79 ~]# 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
chronyd既可以进行校准,也可以部署本地时间服务器,提供校准服务。
首先需要在/etc/chrony.conf中配置监听地址和访问子网范围:
# Allow NTP client access from local network.
#allow 192.168.0.0/16
bindaddress 10.1.8.10
allow 10.1.8.0/24
- bindaddress:监听地址;指定chrony服务器监听的IP地址,仅对监听的地址提供时间服务,防止在其他网络接口上暴露时间服务;
- allow:访问子网范围;只允许指定IP段内的客户端连接;
完成会需重启chronyd服务;有时防火墙也会导致连接失败。必要时也可以关闭:
[root@CentOS79 ~]# systemctl restart chronyd
[root@CentOS79 ~]# systemctl stop firewalld.service
之后在客户端配置授时对象,(如server 10.1.8.10 iburst);就可以进行校准了。
Linux时间管理详解
1万+

被折叠的 条评论
为什么被折叠?



