centos 7 时钟配置:
timedatectl 命令:
[root@localhost ~]# 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 [root@localhost ~]#
1.显示当前系统的时间和日期时区信息:
[root@localhost ~]# timedatectl Local time: 四 2019-04-04 14:51:24 CST Universal time: 四 2019-04-04 06:51:24 UTC RTC time: 四 2019-04-04 06:04:20 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@localhost ~]#
2.根据地址位置查询当前可用的时区设置:
root@localhost ~]# timedatectl list-timezones |grep 'Asia/S' Asia/Sakhalin Asia/Samarkand Asia/Seoul Asia/Shanghai Asia/Singapore Asia/Srednekolymsk [root@localhost ~]#
3.在Linux系统中设置北京时区:
[root@localhost ~]# timedatectl set-timezone 'Asia/Shanghai' [root@localhost ~]#
4.设置时间
[root@localhost ~]# timedatectl set-time '2019-04-04 14:59'
5.hwclock
hwclock 硬件时钟,顾名思义,这个时间时存储在服务器主板上的时钟。硬件时钟存储的值包括:年、月、日、时、分、秒。不存储时间标准、本地时间、UTC、DST。这些设置存放在/etc/adjtime中,当我们修改时间设置(包括手动设置和自动同步)时该文件被创建。在RHEL6或CentOS6系统中,系统每次关机或重启都会自动执行这个命令。在RHEL7或CentOS7中,当系统时间向NTP服务器或PTP服务器同步时间后,每隔11分钟内核自动同步硬件时钟到系统时钟。
(1)显示硬件时钟。
[root@localhost ~]# hwclock 2019年04月04日 星期四 14时17分03秒 -0.400305 秒 [root@localhost ~]#
( 2 )将硬件时钟同步给系统时钟。
-s和–hctosys效果是一样的。
root@localhost ~]# hwclock --systohc [root@localhost ~]# hwclock -s [root@localhost ~]# hwclock 2019年04月04日 星期四 15时05分01秒 -0.319360 秒 [root@localhost ~]#
( 3 ) 将系统时间写入硬件时钟
[root@localhost ~]# hwclock -w
6.crontab 定时执行时间同步
min hour dayofmonth month dayofweek command
minute: 表示分钟,可以是从0到59之间的任何整数。
hour:表示小时,可以是从0到23之间的任何整数。
day:表示日期,可以是从1到31之间的任何整数。
month:表示月份,可以是从1到12之间的任何整数。
week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。