linux系统下date命令修改时间及hwclock硬件时间

linux系统下date命令修改时间及hwclock硬件时间

  • 6794

修改日期: 
时间设定成2016年5月18日的命令如下:

date -s 05/18/2016

修改时间: 
将系统时间设定成上午10点10分40秒的命令如下。

date -s 10:10:40

修改时区: 
找到相应的时区文件 /usr/share/zoneinfo/Asia/Shanghai替换当前的/etc/localtime。 
修改/etc/sysconfig/clock文件的内容为: 
ZONE=”Asia/Shanghai” 
UTC=false 
ARC=false

hwclock:硬件时钟 
-w:同步系统时间到硬件时间 
-s:同步硬件时间到系统时间

date命令的功能是显示和设置系统日期和时间。 
输入date 查看目前系统时间。

修改时间需要 date -功能字符 修改内容

命令中各选项的含义分别为: 
-d datestr, –date datestr 显示由datestr描述的日期 
-s datestr, –set datestr 设置datestr 描述的日期 
-u, –universal 显示或设置通用时间

时间域 
%H 小时(00..23) 
%I 小时(01..12) 
%k 小时(0..23) 
%l 小时(1..12) 
%M 分(00..59) 
%p 显示出AM或PM 
%r 时间(hh:mm:ss AM或PM),12小时 
%s 从1970年1月1日00:00:00到目前经历的秒数 
%S 秒(00..59) 
%T 时间(24小时制)(hh:mm:ss) 
%X 显示时间的格式(%H:%M:%S) 
%Z 时区 日期域 
%a 星期几的简称( Sun..Sat) 
%A 星期几的全称( Sunday..Saturday) 
%b 月的简称(Jan..Dec) 
%B 月的全称(January..December) 
%c 日期和时间(Wed May 18 10:27:35 PDT 2016) 
%d 一个月的第几天(01..31) 
%D 日期(mm/dd/yy) 
%h 和%b选项相同 
%j 一年的第几天(001..366) 
%m 月(01..12) 
%w 一个星期的第几天(0代表星期天) 
%W 一年的第几个星期(00..53,星期一为第一天) 
%x 显示日期的格式(mm/dd/yy) 
%y 年的最后两个数字( 2016则是16) 
%Y 年(例如:1970,2016等)

样例: date +%Y-%m-%d\ %H:%M:%S 显示:2016-05-18 10:20:23 
需要特别说明的是,只有超级用户才能用date命令设置时间,一般用户只能用date命令显示时间。

例1:用指定的格式显示时间。 
date ‘+This date now is =>%x ,time is now =>%X ,thank you !’ 
This date now is =>05/18/2016 ,time is now =>10:25:53 AM ,thank you ! 
例2:用预定的格式显示当前的时间。

date

Wed May 18 10:27:35 PDT 2016 
例3:设置时间为上午10点36分。

date -s 10:36:00

Wed May 18 10:36:00 PDT 2016 
例4:设置时间为2016年5月18号。

date -s 160518

Wed May 18 00:00:00 PDT 2016

实例:设置时间伟2016年5月18号12:00

date -s “2016-05-18 12:00:00”

修改完时间后,记得使用hwclock –w将系统时间同步到硬件时间。




[wangshumin@centoshostnameKL1 ~]$ date -s “2018-2-11 8:37:00”

date: 参数"8:37:00”" 缺少前导的"+";
当使用选项来描述日期是,任何非选项参数都必须以"+"所引导的字符串出现
请尝试执行"date --help"来获取更多信息。
[wangshumin@centoshostnameKL1 ~]$ date -s "2018-2-11 8:37:00"
date: 无法设置日期: 不允许的操作
2018年 02月 11日 星期日 08:37:00 CST
[wangshumin@centoshostnameKL1 ~]$ sudo date -s "2018-2-11 8:37:00"
[sudo] password for wangshumin: 
2018年 02月 11日 星期日 08:37:00 CST
[wangshumin@centoshostnameKL1 ~]$  "hwclock -w"
-bash: hwclock -w: command not found
[wangshumin@centoshostnameKL1 ~]$ clock -w
Sorry, only the superuser can change the Hardware Clock.
[wangshumin@centoshostnameKL1 ~]$ sudo clock -w
[sudo] password for wangshumin: 
[wangshumin@centoshostnameKL1 ~]$ hwclock –w
hwclock - query and set the hardware clock (RTC)


Usage: hwclock [function] [options...]


Functions:
  -h | --help         show this help
  -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 to 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
  -v | --version      print out the version of hwclock to stdout


Options: 
  -u | --utc          the hardware clock is kept in UTC
       --localtime    the hardware clock is kept in local time
  -f | --rtc=path     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         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. Requires the use of
                      either --utc or --localtime
       --adjfile=path specifies the path to the adjust file (default is
                      /etc/adjtime)
       --test         do everything except actually updating the hardware
                      clock or anything else
  -D | --debug        debug mode


hwclock takes no non-option arguments.  You supplied 1.
[wangshumin@centoshostnameKL1 ~]$ hwclock -w
Sorry, only the superuser can change the Hardware Clock.
[wangshumin@centoshostnameKL1 ~]$ sudo hwclock -w
[sudo] password for wangshumin: 
[wangshumin@centoshostnameKL1 ~]$ date
2018年 02月 11日 星期日 08:47:05 CST
[wangshumin@centoshostnameKL1 ~]$ 
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值