常常不用,用的时候又忘记,所以专门记录修改系统时间的方法。
显示当前日期和时间:
- date -R
-
或者
- date
-
设置日期和时间(需要超级用户权限):
- sudo date MMDDhhmmYYYY
-
注意这里的时间格式是月日时分年:
MM:月份
DD:日期
hh:小时(24小时制)
mm:分钟
YY:年份
例如,将时间设置为 2023 年 12 月 6 日 14 点 30 分:
- sudo date 120614302023
-
- sudo tzselect
-
- Please identify a location so that time zone rules can be set correctly.
- Please select a continent, ocean, "coord", or "TZ".
- 1) Africa
- 2) Americas
- 3) Antarctica
- 4) Asia
- 5) Atlantic Ocean
- 6) Australia
- 7) Europe
- 8) Indian Ocean
- 9) Pacific Ocean
- 10) coord - I want to use geographical coordinates.
- 11) TZ - I want to specify the time zone using the Posix TZ format.
- #? 4
- Please select a country whose clocks agree with yours.
- 1) Afghanistan 18) Iraq 35) Pakistan
- 2) Antarctica 19) Israel 36) Palestine
- 3) Armenia 20) Japan 37) Philippines
- 4) Azerbaijan 21) Jordan 38) Qatar
- 5) Bahrain 22) Kazakhstan 39) Russia
- 6) Bangladesh 23) Korea (North) 40) Saudi Arabia
- 7) Bhutan 24) Korea (South) 41) Singapore
- 8) Brunei 25) Kuwait 42) Sri Lanka
- 9) Cambodia 26) Kyrgyzstan 43) Syria
- 10) China 27) Laos 44)
- 11) Cyprus 28) Lebanon 45) Tajikistan
- 12) East Timor 29) Macau 46) Thailand
- 13) Georgia 30) Malaysia 47) Turkmenistan
- 14) 31) Mongolia 48) United Arab Emirates
- 15) India 32) Myanmar (Burma) 49) Uzbekistan
- 16) Indonesia 33) Nepal 50) Vietnam
- 17) Iran 34) Oman 51) Yemen
- #? 10
- 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.
- Selected time is now: Wed Dec 7 17:27:16 CST 2022.
- Universal Time is now: Wed Dec 7 09:27:16 UTC 2022.
- 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
-
以上为选择Asia\China\Shanghai 时间。
- sudo cp /usr/share/zoneinfo/Asia/Chongqing /etc/localtime
-
替换以前的配置文件。
- sudo hwclock --systohc
-
将时间写入cmos硬件时钟。
- sudo ntpdate NTP SERVER
-
启用/禁用网络时间同步(NTP):
- sudo timedatectl set-ntp true # 启用NTP
- sudo timedatectl set-ntp false # 禁用NTP
-
- sudo date -R
-
查看系统时间是否更新为正确时间。这里需要注意“R”为大写。
除了使用命令行还可以通过图形界面修改,因为比较简单,就不做分享了。注意更改系统时间都需要管理员权限(sudo)。
随着系统版本的不同,可能还有其他命令和配置文件用于管理时间。无论你选择哪个命令,确保你有足够的权限来修改系统时间,并小心操作,以避免对系统产生不良影响。