Linux 修改系统时间

一. 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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章