centos7之关于时间和日期以及时间同步的应用

在CentOS 6版本,时间设置有date、hwclock命令,从CentOS 7开始,使用了一个新的命令timedatectl。

 

一、GMT、UTC、CST、DST 时间

  • UTC

   整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。

  • GMT

   格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)

  • CST

   中国标准时间 (China Standard Time)

1
GMT + 8 = UTC + 8 = CST
  • DST

   夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用)

 

二、硬件时间和系统时间

  • 硬件时间

  RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。

  • 系统时间

  一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

 

三、timedatectl 命令

[root@localhost ~]# timedatectl -h
timedatectl [OPTIONS...] COMMAND ...
 
Query or change system time and date settings.
 
  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode
 
Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled

命令示例(查看当前系统时间)

#timedatectl
#timedatectl status

 

 

设置当前时间

1 timedatectl set-time "YYYY-MM-DD HH:MM:SS"
2 timedatectl set-time "YYYY-MM-DD"
3 timedatectl set-time "HH:MM:SS"

 

查看所有可用的时区

timedatectl list-timezones
# 亚洲
timedatectl list-timezones |  grep  -E "Asia/S.*"

  

设置时区

timedatectl set-timezone Asia/Shanghai

 

设置硬件时间

# 硬件时间默认为UTC
timedatectl set-local-rtc 1
# hwclock --systohc --localtime
# 两条命令效果等同

  

启动自动同步时间

timedatectl set-ntp yes
# yes或no; 1或0也可以

 

posted @ 2020-07-09 11:37  _小豪豪  阅读(449)  评论(0编辑  收藏  举报