Linux中分爲系統時間和硬件時間,系統時間使用date
命令和機器時間使用clock
命令,本文就簡單介紹一下Linux中的時間和日期的命令。
Date
直接在命令行中輸入date
命令,顯示的是系統時間。系統時間是由Linux內通過cpu的工作頻率的計時。
命令格式:
(1)顯示日期和時間
date [OPTION]... [+FORMAT]
FORMAT:
%F:按照指定的格式顯示。如:2017-07-15
%D:直接顯示日期mm/dd/yy
%H:顯示小時,如:0-23
%M:顯示分鐘
%S:顯示秒
%s:顯示從1970年1月1日到現在所經過的秒數
%m:顯示月份
[root@CentOS7 ~]$date
Thu Aug 31 22:09:47 CST 2017
[root@CentOS7 ~]$date +"%F %T"
2017-08-31 22:09:50
(2)修改日期和時間
date [-u|-utc|--universal] [MMMDDHHMM[[CC]YY][.ss]]
MM:月份
DD:幾號
hh:小時
mm:分鐘
CC:兩位年份
CCYY:四位年份
ss:秒
[root@CentOS7 ~]$date 111109302020.35
Wed Nov 11 09:30:35 CST 2020
Clock
clock
或者hwclock
的輸出結果是硬件時間。硬件時間就是主板BIOS提供的時鐘有主板電池供電來維持運行。
時鐘的修改
修改時鐘包括系統時間和硬件時間
hwlock用法
-s , –hctosys 以硬件時間爲準同步到系統時間
-w, –systohc 以系統時間爲準同步到硬件時間
[root@CentOS7 ~]$date 111109302020.35
Wed Nov 11 09:30:35 CST 2020
[root@CentOS7 ~]$clock -s
[root@CentOS7 ~]$date
Thu Aug 31 22:12:09 CST 2017
ntpdate
ntpdate
命令後面跟時鐘服務器的IP地址,可以讓系統同步來自服務器的時間
如:ntpdate 172.18.0.1
[root@CentOS7 ~]$ntpdate 172.18.0.1
31 Aug 22:23:55 ntpdate[27159]: step time server 172.18.0.1 offset 37.242806 sec
更改時區
要更改 Linux 系統時區首先得獲知你所當地時區的 TZ 值,使用 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: Thu Aug 31 22:17:07 CST 2017.
Universal Time is now: Thu Aug 31 14:17:07 UTC 2017.
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
可以通過選擇數字的方式按照大洲-國家-城市,一步一步選擇到你想設置的時區。
Cal
cal
命令是顯示當前日曆,默認是當前月份的日曆。
[root@CentOS7 ~]$cal
August 2017
Su Mo Tu We Th Fr Sa
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
cal 年份:可以顯示年份日曆
[root@CentOS7 ~]$cal 2022
2022
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31