时间篇之读取bios时间命令

前言:

我们可以使用 date命令重置当前linux系统时间.

一、linux系统时间
 1、首先要明白,系统时间和硬件时间的区别,系统指linux系统时间,硬件时间是指的bios时间
 2、linux系统可以用date设定,但是重启

  date -s '2015-01-14 12:00'    

3、手工更新时间ntpdate来更新一次系统时间(非硬件时间)

[root@fp-web-112 ~]# ntpdate time.ntp.org

4、如果想一直更新时间用nptdate命令 ,可以放入crontab 计划任务中( nptdate命令随ntp包一块安装。)

  1). ntpdate可以同步我们搭建的时间服务器 (服务器多的情况下推荐)
  2)ntpdate可以同步互联网上的时间服务器

在crontab中添加:
[root@fp-web-178 ~]# crontab -l
0 12 * * * * /usr/sbin/ntpdate  1.cn.pool.ntp.org //外部的时间服务器
0 12 * * * * /usr/sbin/ntpdate  1.cn.pool.ntp.org
0 12 * * *  /usr/sbin/ntpdate -u 1.cn.pool.ntp.org
##每天12点执行一次
[root@fp-web-178 ~]# crontab -l
*/2 * * * *  /usr/sbin/ntpdate -u 1.cn.pool.ntp.org
##每两分钟执行一次

5、利用timedatectl服务(时间同步还是利用的ntp服务)

//查看时间情况
root@fp-web-112 ~]# timedatectl Local time: Tue 2009-04-28 19:40:08 CST Universal time: Tue 2009-04-28 11:40:08 UTC RTC time: Mon 2022-03-07 05:40:41 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a

使用timedatectl 命令开启ntp同步就可以了

[root@fp-web-112 ~]# timedatectl set-ntp yes

 

6、硬件时间查看(实际说的就是bios芯片里面的时间程序)

这里说的详细点
//我重置了时间

 [root@fp-web-112 ~]#  date 042817252009.00   

 //读取当前时间是2009年时间,这个读取的是linux系统时间

 [root@fp-web-112 ~]# date 
 Tue Apr 28 18:55:52 CST 2009

读取硬件时间
[root@fp-web-178 ~]#  hwclock -r
Mon 07 Mar 2022 12:55:41 PM CST  -0.875529 seconds
注:我们注意到这两个时间明显不同。 一个是设定的系统时间,一个是来自于bios的硬件的时间

二、hwclock命令
 读取硬件(bios)的时间命令

[root@fp-web-112 ~]# hwclock  -h

Usage:
 hwclock [function] [option...]

Functions:
 -h, --help           show this help text and exit
 -r, --show           read hardware clock and print result
     --set            set the RTC to the time given with --date
 -s, --hctosys        set the system time from the hardware clock
 -w, --systohc        set the hardware clock from the current system time
     --systz          set the system time based on the current timezone
     --adjust         adjust the RTC to account for systematic drift since
                        the clock was last set or adjusted
 -c, --compare        periodically compare the system clock with the CMOS clock
     --getepoch       print out the kernel's hardware clock epoch value
     --setepoch       set the kernel's hardware clock epoch value to the 
                        value given with --epoch
     --predict        predict RTC reading at time given with --date
 -V, --version        display version information and exit

Options:
 -u, --utc            the hardware clock is kept in UTC
     --localtime      the hardware clock is kept in local time
 -f, --rtc <file>     special /dev/... file to use instead of default
     --directisa      access the ISA bus directly instead of /dev/rtc
     --badyear        ignore RTC's year because the BIOS is broken
     --date <time>    specifies the time to which to set the hardware clock
     --epoch <year>   specifies the year which is the beginning of the
                        hardware clock's epoch value
     --noadjfile      do not access /etc/adjtime; this requires the use of
                        either --utc or --localtime
     --adjfile <file> specifies the path to the adjust file;
                        the default is /etc/adjtime
     --test           do not update anything, just show what would happen
 -D, --debug          debugging mode

三、系统时间和硬件时间的同步

1、以系统时间为准(同步硬件时间为系统时间, 使用hwclock --systohc 命令)
//读取硬件当前时间
[root@fp-web-112 ~]# hwclock -r Mon 07 Mar 2022 12:42:01 PM CST -0.192627 seconds
//以系统时间为基准,修改硬件时间,把系统时间写到bios里面,执行两条命令
[root@localhost ~]# hwclock --systohc
[root@localhost ~]# hwclock -w 
 

2、以硬件时间为基准,调整为系统时间一致 (bios时间同步,使用hwclock --hctosys

//将系统时钟调整为与目前的硬件时钟一致
[root@localhost ~]# hwclock --hctosys [root@localhost ~]# hwclock -s [root@localhost ~]# hwclock --systohc –-utc

 3、系统日期强制写入CMOS

//强制把系统日期直接写入cmos
[root@fp-web-112 ~]# clock -w
//clock命令显示硬件时间
[root@fp-web-112 ~]# clock
Mon 07 Mar 2022 01:09:52 PM CST  -0.786444 seconds
//当前的系统日期
[root@fp-web-112 ~]# date
Tue Apr 28 19:16:02 CST 2009
//我们对比下clock和hwclock的时间是几乎一致的 
[root@fp-web-112 ~]# hwclock -r
Mon 07 Mar 2022 01:17:21 PM CST -0.145576 seconds
[root@fp-web-112 ~]# clock
Mon 07 Mar 2022 01:17:30 PM CST -0.458057 seconds

//更新机器的硬件时间

[root@fp-web-112 ~]# hwclock --adjust
//查看硬件时间
[root@fp-web-112 ~]# hwclock --show

4、修改时间
    4.1 直接修改系统时间

//1: 显示系统时间:
[root@fp-web-112 ~]# date
Tue Apr 28 18:55:52 CST 2009
//2:修改系统时间:
[root@fp-web-112 ~]# date -s '2022-02-15 12:10'  
//ntpdate和ntpd另外章节说明(当然我们也可以通过硬件时间同步到系统时间完成这个需求)
4.2 修改硬件时间
//3:显示硬件时间:
# hwclock --show
//4:设置硬件时间:
# hwclock --set --date ‘08/02/2012 12:00:00//5:将硬件时间同步到系统时间:
# hwclock --hctosys
//6:将系统时间同步到硬件时间
# hwclock --systohc
//7:强制把系统时间写入CMOS:
# clock -w

 

三、更改时区

 1、查看时区信息

[root@fp-web-112 ~]# date -R
Tue, 28 Apr 2009 19:20:05 +0800

//查看时区,看到当前的时区是上海时间.

[root@fp-web-112 ~]# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 35 Jun 7 2017 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

 

2、第一种方法更改 Linux 系统整个系统范围的时区,可以使用如下命令:

  rm -f /etc/localtime
  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
即时生效

3、第二种方法,利用获取时区TZ值,然后进行更改设置

要更改 Linux 系统时区首先得获知你所当地时区的 TZ 值,
使用 tzselect 命令即可查看并选择已安装的时区文件。
[root@fp-web-112 ~]# 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. #? 4^H^H^H^H^H Please enter a number in range. #? 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
注意:/usr/share/zoneinfo/Asia/Shanghai 中的具体时区请用自己获取到的 TZ 值进行替换。

4、多用户的情况下的,时区设置问题

Linux 用户一个多用户系统,每个用户都可以配置自己所需的时区,你可以为自己新增一个 TZ 环境变量:

export TZ='Asia/Shanghai'

 

执行完成之后需要重新登录系统或刷新 ~/.bashrc 生效。

source ~/.bashrc

5、第三种方法使用Systemd更改Linux系统时区

如果你使用的 Linux 系统使用 Systemd,还可以使用 timedatectl 命令来更改 Linux 系统范围的时区。


我们可以使用 timedatectl 命令对此系统服务进行配置(在 Systemd 下有一个名为 systemd-timedated 的系统服务负责调整系统时钟和时区)。

[root@fp-web-112 ~]# timedatectl set-timezone 'Asia/Shanghai'

 

posted @ 2022-03-07 13:26  jinzi  阅读(24)  评论(0编辑  收藏  举报