修改
CentOS 7系统中的时间日期设置
timedatectl set-ntp no
timedatectl
timedatectl set-time 2022-06-04
timedatectl
在过去的CentOS版本里,要设置时区的话要手动修改 /etc/locale .conf文件,很是麻烦,不过CentOS 7 已经为我们准备好一个非常强大的工具了: localectl
显示当前时区
使用以下命令:
复制代码
代码如下:
~]$ localectl status
System Locale: LANG=en_US.UTF-8
VC Keymap: us
X11 Layout: n/a
可以看到,除了显示了系统的时区信息以外,还显示了键盘信息和X11布局信息
列出所有的时区
用以下命令显示所有的英文时区
复制代码
代码如下:
~]$ localectl list-locales | grep en_
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8
如果要显示中文的,只需要把grep en 改成grep zh就行了
设置本地时区
使用Root执行以下命令:
复制代码
代码如下:
localectl set-locale LANG=locale
把最后的 locale 替换成具体的时区,比如zh_CN.UTF-8就可以设置了。
是不是很强大啊。
timedatectl
下面隆重来介绍一下timedatectl命令~
在以前的CentOS版本里,时间设置有 date , hwclock 等一系列命令,但是CentOS 7 开始,使用了一个统一的命令:
复制代码
代码如下:
timedatectl
这个命令非常的强大,首先是直接使用可以显示当前的系统时间的一些信息:
复制代码
代码如下:
~]$ timedatectl
Local time: Mon 2013-09-16 19:30:24 CEST
Universal time: Mon 2013-09-16 17:30:24 UTC
Timezone: Europe/Prague (CEST, +0200)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2013-03-31 01:59:59 CET
Sun 2013-03-31 03:00:00 CEST
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2013-10-27 02:59:59 CEST
Sun 2013-10-27 02:00:00 CET
设置当前日期:
使用Root执行以下命令就可以了:
复制代码
代码如下:
timedatectl set-time YYYY-MM-DD
设置当前时间:
依旧是要Root权限
复制代码
代码如下:
timedatectl set-time HH:MM:SS
默认的,系统是使用UTC时间的,可以用以下命令打开和关闭UTC时间:
复制代码
代码如下:
timedatectl set-local-rtc boolean
把 boolean 替换成yes则表示使用本地时间,替换成no则表示是UTC时间
设置任意时区
可以用以下命令查看所有的时区:
复制代码
代码如下:
timedatectl list-timezones
然后用以下命令设置时区:
复制代码
代码如下:
timedatectl set-timezone time_zone
当然root权限是免不了的
与远程NTP服务器同步
timedatectl还可以设置是否打开NTP选项
复制代码
代码如下:
timedatectl set-ntp boolean
同样的,这里的boolean是yes或者no
# timedatectl status
Local time: 三 2020-06-03 16:16:54 CST
Universal time: 三 2020-06-03 08:16:54 UTC
RTC time: 三 2020-06-03 08:16:54
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@ls236 ~]# timedatectl
Local time: 三 2020-06-03 16:17:12 CST
Universal time: 三 2020-06-03 08:17:12 UTC
RTC time: 三 2020-06-03 08:17:12
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@ls236 ~]# timedatectl set-local-rtc boolean
Failed to parse local RTC setting: boolean
[root@ls236 ~]#
[root@ls236 ~]# timedatectl set-local-rtc yes
[root@ls236 ~]# timedatectl set-time 2022-06-04
Failed to set time: Automatic time synchronization is enabled
[root@ls236 ~]# 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@ls236 ~]# timedatectl set-ntp
Invalid number of arguments.
[root@ls236 ~]# timedatectl set-ntp --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@ls236 ~]# timedatectl set-ntp BOOL
Failed to parse NTP setting: BOOL
[root@ls236 ~]# timedatectl set-ntp YES
[root@ls236 ~]# timedatectl
Local time: 三 2020-06-03 16:19:05 CST
Universal time: 三 2020-06-03 08:19:05 UTC
RTC time: 三 2020-06-03 16:19:05
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: yes
DST active: n/a
Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.
[root@ls236 ~]# timedatectl set-ntp NO
[root@ls236 ~]# timedatectl
Local time: 三 2020-06-03 16:19:13 CST
Universal time: 三 2020-06-03 08:19:13 UTC
RTC time: 三 2020-06-03 16:19:13
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: yes
DST active: n/a
Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.
[root@ls236 ~]# timedatectl set-time 2022-06-04
[root@ls236 ~]# date
2022年 06月 04日 星期六 00:00:01 CST
[root@ls236 ~]#