【CentOS】有关时间的设置

本文详细介绍了在CentOS7中查看和管理时间的方法,包括使用date命令显示和设置时间,tzselect用于选择时区,timedatectl管理系统时间和时区,以及hwclock与ntpdate服务来同步硬件时钟和系统时钟。文章还强调了硬件时钟和系统时钟的区别以及如何在启动和关机时相互转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境信息

  • CentOS 7

date

语法信息

date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -I[TIMESPEC], --iso-8601[=TIMESPEC]  output date/time in ISO 8601 format.
                            TIMESPEC='date' for date only (the default),
                            'hours', 'minutes', 'seconds', or 'ns' for date
                            and time to the indicated precision.
  -r, --reference=文件          显示文件指定文件的最后修改时间
  -R, --rfc-2822                以RFC 2822格式输出日期和时间
                                例如:2006年8月7日,星期一 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC='date', 'seconds', or 'ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time (UTC)
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

给定的格式FORMAT 控制着输出,解释序列如下:

  %%    一个文字的 %
  %a    当前locale 的星期名缩写(例如: 日,代表星期日)
  %A    当前locale 的星期名全称 (如:星期日)
  %b    当前locale 的月名缩写 (如:一,代表一月)
  %B    当前locale 的月名全称 (如:一月)
  %c    当前locale 的日期和时间 (如:2005年3月3日 星期四 23:05:25)
  %C    世纪;比如 %Y,通常为省略当前年份的后两位数字(例如:20)
  %d    按月计的日期(例如:01)
  %D    按月计的日期;等于%m/%d/%y
  %e    按月计的日期,添加空格,等于%_d
  %F    完整日期格式,等价于 %Y-%m-%d
  %g    ISO-8601 格式年份的最后两位 (参见%G)
  %G    ISO-8601 格式年份 (参见%V),一般只和 %V 结合使用
  %h    等于%b
  %H    小时(00-23)
  %I    小时(00-12)
  %j    按年计的日期(001-366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n    换行
  %N    纳秒(000000000-999999999)
  %p    当前locale 下的"上午"或者"下午",未知时输出为空
  %P    与%p 类似,但是输出小写字母
  %r    当前locale 下的 12 小时时钟时间 (如:11:11:04 下午)
  %R    24 小时时间的时和分,等价于 %H:%M
  %s    自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数
  %S    秒(00-60)
  %t    输出制表符 Tab
  %T    时间,等于%H:%M:%S
  %u    星期,1 代表星期一
  %U    一年中的第几周,以周日为每星期第一天(00-53)
  %V    ISO-8601 格式规范下的一年中第几周,以周一为每星期第一天(01-53)
  %w    一星期中的第几日(0-6),0 代表周一
  %W    一年中的第几周,以周一为每星期第一天(00-53)
  %x    当前locale 下的日期描述 (如:12/31/99)
  %X    当前locale 下的时间描述 (如:23:13:48)
  %y    年份最后两位数位 (00-99)
  %Y    年份
  %z +hhmm              数字时区(例如,-0400)
  %:z +hh:mm            数字时区(例如,-04:00)
  %::z +hh:mm:ss        数字时区(例如,-04:00:00)
  %:::z                 数字时区带有必要的精度 (例如,-04,+05:30)
  %Z                    按字母表排序的时区缩写 (例如,EDT)

默认情况下,日期的数字区域以0 填充。
The following optional flags may follow '%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

在任何标记之后还允许一个可选的域宽度指定,它是一个十进制数字。
作为一个可选的修饰声明,它可以是E,在可能的情况下使用本地环境关联的
表示方式;或者是O,在可能的情况下使用本地环境关联的数字符号。

Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
  $ date --date='@2147483647'

Show the time on the west coast of the US (use tzselect(1) to find TZ)
  $ TZ='America/Los_Angeles' date

Show the local time for 9AM next Friday on the west coast of the US
  $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告date 的翻译错误
要获取完整文档,请运行:info coreutils 'date invocation'

查看时间

date
#以RFC 2822格式输出日期和时间  例如:2006年8月7日,星期一 12:34:56 -0600
date -R
#自定义格式显示
date +"%Y-%m-%d"

在这里插入图片描述
在这里插入图片描述

设置时间

不带日期时不会修改日期

#date -s
date -s 22:01:01

在这里插入图片描述

设置日期

不带时间时会设置时间为00:00:00

#date -s
date -s 2023-03-15
date -s 20230215
date -s "2023-02-14 23:31:01"

在这里插入图片描述
在这里插入图片描述

tzselect

交互式设置时区

tzselect

特别注意红色部分
在这里插入图片描述

以上修改并不会立即生效,需按照红色要求将TZ设置到环境变量中并重新登录

#以下内容添加到环境变量中,如 /etc/profile、~/.bash_profile等文件
TZ='Asia/Shanghai'; 
export TZ

timedatectl

语法

 timedatectl --help
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
#显示所有时区
 timedatectl list-timezones

在这里插入图片描述

修改时区
timedatectl set-timezone America/New_York

在这里插入图片描述

hwclock

语法

hwclock -h
用法:
 hwclock [功能] [选项...]

功能:
 -h, --help           显示此帮助并退出
 -r, --show           读取硬件时钟并打印结果
     --set            将 RTC 设置为 --date 指定的时间
 -s, --hctosys        从硬件时钟设置系统时间
 -w, --systohc        从当前系统时间设置硬件时钟
     --systz          基于当前时区设置系统时间
     --adjust         根据自上次时钟设置或调整后的系统漂移
                        来调整 RTC
 -c, --compare        定期将系统时钟与 CMOS 时钟相比较
     --getepoch       打印内核的硬件时钟纪元(epoch)值
     --setepoch       将内核的硬件时钟纪元(epoch)值设置为
                        --epoch 选项指定的值
     --predict        预测 --date 选项所指定时刻读取到的 RTC 值
 -V, --version        显示版本信息并退出

选项:
 -u, --utc            硬件时钟保持为 UTC 时间
     --localtime      硬件时钟保持为本地时间
 -f, --rtc <文件>     代替默认文件的特殊 /dev/... 文件
     --directisa      直接访问 ISA 总线,而非 /dev/rtc
     --badyear        忽略  RTC 年份(由于 BIOS 损坏)
     --date <时间>    指定要设置的硬件时钟时间
     --epoch <年>     指定作为硬件纪元(epoch)值起始的年份
     --noadjfile      不访问 /etc/adjtime;需要使用 --utc 或 --localtime 选项
     --adjfile <文件> 指定调整文件的路径;
                        默认为 /etc/adjtime
     --test           不更新,只显示将进行什么操作
 -D, --debug          调试模式

读取硬件时钟

hwclock -r

在这里插入图片描述

使用硬件时钟设置系统时间

 hwclock -s

在这里插入图片描述

使用系统时间设置硬件时钟

 hwclock -w

时间更新

使用ntpdate服务更新

#安装
yum install ntpdate
#更新
ntpdate ntp.aliyun.com
#安装(ntp自动更新-略)
yum install -y ntp

阿里云时间服务器清单:
ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com

如何理解硬件时钟和系统时钟

操作系统有两个时钟,分别是硬件时钟和系统时钟,大多数操作系统的时钟管理方式如下:

硬件时钟: 是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。
系统时钟: 则是指kernel中的时钟。

  • 启动时根据硬件时钟设置系统时钟
  • 关机时根据系统时钟设置硬件时钟
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值