日常003:timedatectl在Linux系统中查看和设置系统时间和日期
timedatectl
是一个用于在 Linux 系统中查看和设置系统时间和日期的命令行工具。通过 timedatectl
命令,你可以查看当前系统的时间、日期、时区等信息,并且可以对这些信息进行调整和设置。
以下是一些常用的 timedatectl
命令及其功能:
常用命令示例:
-
查看当前系统时间和日期信息:
t i m e d a t e c t l L o c a l t i m e : M o n 2024 − 03 − 1117 : 28 : 47 C S T U n i v e r s a l t i m e : M o n 2024 − 03 − 1109 : 28 : 47 U T C R T C t i m e : M o n 2024 − 03 − 1109 : 28 : 47 T i m e z o n e : A s i a / S h a n g h a i ( C S T , + 0800 ) S y s t e m c l o c k s y n c h r o n i z e d : y e s N T P s e r v i c e : a c t i v e R T C i n l o c a l T Z : n o < / c o d e > < / p r e > < / l i > < l i > < p > < s p a n s t y l e = " f o n t − w e i g h t : b o l d ; " d a t a − t y p e = " s t r o n g " > 查看系统时区信息: < / s p a n > < / p > < p r e > < c o d e c l a s s = " l a n g u a g e − b a s h " > timedatectl Local time: Mon 2024-03-11 17:28:47 CST Universal time: Mon 2024-03-11 09:28:47 UTC RTC time: Mon 2024-03-11 09:28:47 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no </code></pre> </li> <li> <p><span style="font-weight: bold;" data-type="strong">查看系统时区信息:</span></p> <pre><code class="language-bash"> timedatectlLocaltime:Mon2024−03−1117:28:47CSTUniversaltime:Mon2024−03−1109:28:47UTCRTCtime:Mon2024−03−1109:28:47Timezone:Asia/Shanghai(CST,+0800)Systemclocksynchronized:yesNTPservice:activeRTCinlocalTZ:no</code></pre></li><li><p><spanstyle="font−weight:bold;"data−type="strong">查看系统时区信息:</span></p><pre><codeclass="language−bash"> timedatectl | grep "Time zone" Time zone: Asia/Shanghai (CST, +0800)
-
设置系统时区:
sudo timedatectl set-timezone <时区>
例如:
sudo timedatectl set-timezone Asia/Shanghai
-
手动设置系统时间:
sudo timedatectl set-time 'YYYY-MM-DD HH:MM:SS'
例如:
sudo timedatectl set-time '2024-03-11 16:30:00'
-
将系统时钟设置为本地时间或 UTC 时间:
sudo timedatectl set-local-rtc 1 # 设置为本地时间 sudo timedatectl set-local-rtc 0 # 设置为 UTC 时间
-
启用/禁用网络时间同步(NTP):
sudo timedatectl set-ntp true # 启用 NTP sudo timedatectl set-ntp false # 禁用 NTP