OS
CentOS 7 下关于时间和日期以及时间同步的应用
在CentOS 6版本,时间设置有date、hwclock命令,从CentOS 7开始,使用了一个新的命令timedatectl。 1. 基本概念 1.1 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) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用) 1.2 硬件时间和系统时间 硬件时间 RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。 系统时间 一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。 2. timedatectl 命令 2.1 使用帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@localhost ~]# timedatectl -h timedatectl [OPTIONS...] COMMAND ... Query or change system time and date settings. -h --help Show this help --version Show package version --adjust-system-clock Adjust system clock when changing local RTC mode --no-pager Do not pipe output into a pager -P --privileged Acquire privileges before execution --no-ask-password Do not prompt for password -H --host=[USER@]HOST Operate on remote host Commands: status Show current time settings set-time TIME Set system time set-timezone ZONE Set system timezone list-timezones Show known timezones set-local-rtc BOOL Control whether RTC is in local time set-ntp BOOL Control whether NTP is enabled |
2.2 命令示例 1.显示系统的当前时间和日期
1 2 3 |
timedatectl # timedatectl status # 两条命令效果等同 |
2.设置日期与时间
1 2 3 |
timedatectl set-time "YYYY-MM-DD HH:MM:SS" timedatectl set-time "YYYY-MM-DD" timedatectl set-time "HH:MM:SS" |
3.查看所有可用的时区
1 2 3 |
timedatectl list-timezones # 亚洲 timedatectl list-timezones | grep -E "Asia/S.*" |