Linux笔记:查看和修改系统时间

1. 查看系统当前时间

命令: date

[root@zjy ~]# date
Sun Nov 13 20:14:44 CST 2022

2. 修改系统时间

修改时间: date -s "21:12:00"

修改日期: date -s "2022-11-12" # 需要特别注意,单独修改日期,会导致时间信息自动设置为“00:00:00”

修改日期和时间: date -s "2022-11-12 21:13:00"

[root@zjy ~]# date
Sun Nov 13 21:12:06 CST 2022
[root@zjy ~]# date -s "21:12:00"
Sun Nov 13 21:12:00 CST 2022
[root@zjy ~]# date -s "2022-11-12"
Sat Nov 12 00:00:00 CST 2022
[root@zjy ~]# date -s "2022-11-12 21:13:00"
Sat Nov 12 21:13:00 CST 2022
[root@zjy ~]# date
Sat Nov 12 21:13:02 CST 2022

注: date命令只是修改系统时间,不会同步修改机器硬件时间(Linux时间分为系统时间和硬件时间),当机器下电之后再次上电时,系统会将硬件时间同步过来重新计算,之前通过date命令设置的时间就失效了,想要将设置的系统时间同步到硬件时间,可以使用命令 clock -w ,这样系统时间和硬件时间就一致了。

3. 查看系统的时区信息

命令: ll /etc/localtime

[root@zjy ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Jul 25  2021 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

通过执行结果可以看出,文件/etc/localtime就是一个软链接,它指向哪个时区文件,当前系统的时区就是该时区,所以如果想要修改当前系统的时区,只需要修改该软链接,重新指向对应的时区文件即可。

4. 修改系统的时区信息

命令: ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime

[root@zjy ~]# ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime 
[root@zjy ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 34 Nov 13 20:09 /etc/localtime -> /usr/share/zoneinfo/Asia/Chongqing

系统支持的时区都在/usr/share/zoneinfo下,想要修改当前系统的时区,只需要将软链接文件/etc/localtime指向对应的时区文件即可。(ln表示创建一个链接(一个文件指向另一个文件),s参数表示创建软链接,f参数表示强制执行,后面再跟上源文件(目录)和目标文件(目录)即可)

注: 部分软件获取系统的时区信息时会去读取 /etc/sysconf/clock 文件里的时区信息,所以修改手动修改时区时需要同时修改这个文件;如果系统默认没有生成这个文件,但是软件需要的话也可以自己手动创建,权限通常为644,文件内容如下:

TIMEZONE="Asia/Shanghai"
DEFAULT_TIMEZONE="Asia/Shanghai"

5. 查看和同步硬件时间

查看硬件时间: clock

同步系统时间到硬件时间: clock -w

同步硬件事件到系统时间: clock -s

[root@zjy ~]# clock --help

Usage:
 clock [function] [option...]

Time clocks utility.

Functions:
 -r, --show           display the RTC time
     --get            display drift corrected RTC time
     --set            set the RTC according to --date
 -s, --hctosys        set the system time from the RTC
 -w, --systohc        set the RTC from the system time
     --systz          send timescale configurations to the kernel
 -a, --adjust         adjust the RTC to account for systematic drift
     --predict        predict the drifted RTC time according to --date

Options:
 -u, --utc            the RTC timescale is UTC
 -l, --localtime      the RTC timescale is Local
 -f, --rtc <file>     use an alternate file to /dev/rtc0
     --directisa      use the ISA bus instead of /dev/rtc0 access
     --date <time>    date/time input for --set and --predict
     --update-drift   update the RTC drift factor
     --noadjfile      do not use /etc/adjtime
     --adjfile <file> use an alternate file to /etc/adjtime
     --test           dry run; implies --verbose
 -v, --verbose        display more details

 -h, --help           display this help
 -V, --version        display version

For more details see hwclock(8).
[root@zjy ~]#

注: 另外还有一个命令hwclock,它的作用和用法从help信息和网上查找的资料来看,是一样的,使用其中一个就行。本文之所以一起介绍了clock命令,是因为Linux上的时间分为硬件时钟(RTC时间)与Kernel时钟,即BIOS界面的硬件时间和Linux的系统时间,系统第一次启动时会将硬件时间同步到系统中,然后两个时钟独立运行,但是当设备下电(断电或关闭电源)后,再次启动设备,系统会再次将硬件时间同步系统中,然后各自独立运行,这种情况下,如果下电之前我们使用date命令单独设置了系统时间,但是没有同步到硬件时间,设备再次上电后之前设置的系统时间就会失效,此时想要硬件时间和系统时间保持一致,就可以在date命令设置时间后使用命令 clock -wclock -s 进行同步。

posted @ 2022-11-15 00:55  山上下了雪-bky  阅读(1133)  评论(0编辑  收藏  举报