一. date 查詢當前時間
[root@localhost ~]# date -R
Sat, 01 Jan 2000 12:02:12 -0400
後面的那個 -0400 表示時區,需要用 tzselect 進行設置。
二. tzselect 設置時區
輸入命令 tzselect 之後,會有 主時區 和 次時區 的選擇,選擇完畢其實沒有設置成功,會出現如下界面:
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
然後在 /etc/profile文件末尾加上這行: TZ='Asia/Shanghai'; export TZ 。保存文件後,用 source 執行一次。如下:
vi /etc/profile // 打開文件
TZ='Asia/Shanghai'; export TZ // 添加行
:wq // 保存退出
source /etc/profile // 執行 profile
然後設置軟鏈接:
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
重新登陸新的 session,調用 date 命令查看時區是否修改成功 (上海是東 8 區):
[root@localhost ~]# date -R
Sun, 02 Jan 2000 01:24:02 +0800
三、hwclock 修改硬件時間
Linux系統時鐘有兩個,一個是硬件時鐘,即 BIOS 時間;另一個是系統時鐘,是 Linux 系統 Kernel 時間。當 Linux 啓動時,系統 Kernel 會去讀取硬件時鐘的設置,然後系統時鐘就會獨立於硬件運作。有時我們會發現系統時鐘和硬件時鐘不一致,因此需要執行時間同步,這時候就需要對系統時間進行同步。
利用 hwclock 修改硬件時間,然後再同步到系統時間。
[root@localhost ~]# hwclock --set --date="2019/01/04 12:14:00"
[root@localhost ~]# hwclock --hctosys
[root@localhost ~]# date -R
Fri, 04 Jan 2019 12:15:22 +0800