一,centos8中默认使用chronyd来做时间服务

1,查看chronyd服务的状态

[root@blog ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-01-10 10:03:49 CST; 2 months 19 days ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Main PID: 671 (chronyd)
Tasks: 1 (limit: 26213)
Memory: 2.8M
CGroup: /system.slice/chronyd.service
└─671 /usr/sbin/chronyd

2,如果找不到chronyd服务,可以用yum安装:

[root@blog ~]# yum install chrony

3,查看chronyd服务是否设置为了自启动

[root@blog ~]# systemctl is-enabled chronyd
enabled

如果chronyd服务启动状态是disabled,用下面的命令设置为自启动

[root@blog ~]# systemctl enable chronyd
Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service. [root@blog ~]# systemctl is-enabled chronyd
enabled

4,用下面的命令启动chronyd

[root@blog ~]# systemctl start chronyd 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,查看timedatectl所属的包:

[root@blog ~]# whereis timedatectl
timedatectl: /usr/bin/timedatectl /usr/share/man/man1/timedatectl.1.gz [root@blog ~]# rpm -qf /usr/bin/timedatectl
systemd-239-13.el8_0.5.x86_64

默认情况下应该已安装到centos系统内

三,timedatectl的用法例子:

1,查看时间的配置状态

[root@blog ~]# timedatectl status
Local time: Mon 2020-03-30 12:37:07 CST
Universal time: Mon 2020-03-30 04:37:07 UTC
RTC time: Mon 2020-03-30 12:37:06
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes

说明:只用 timedatectl 不加后面的status效果也是一样

2,各时间项的说明:

1.Local time:电脑上的本地时间
2.Universal time:通用协调时(UTC):
整个地球分为二十四时区,每个时区都有自己的本地时间。
在国际无线电通信场合,为了统一起见,使用一个统一的时间,
称为通用协调时(UTC, Universal Time Coordinated)
3.RTC time:RTC时间(实时时钟(Real-Time Clock))
RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,
服务器断电后也会继续运行。
仅保存日期时间数值,无法保存时区和夏令时设置
4.Time zone:时区
5. NTP service:NTP(网络时间协议{Network Time Protocol})启用
6.System clock synchronized:NTP同步情况
7.RTC in local TZ:RTC是否使用本地时间

3,列出所有可用的时区

[root@blog ~]# timedatectl list-timezones

4,修改时区

下面的例子中,我们修改时区为美国的纽约

[root@blog ~]# date
Mon Mar 30 12:46:43 CST 2020
[root@blog ~]# timedatectl set-timezone America/New_York
[root@blog ~]# date
Mon Mar 30 00:47:01 EDT 2020
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 00:47:14 EDT
Universal time: Mon 2020-03-30 04:47:14 UTC
RTC time: Mon 2020-03-30 00:47:14
Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes

再改回上海:

[root@blog ~]# timedatectl set-timezone Asia/Shanghai
[root@blog ~]# date
Mon Mar 30 12:48:47 CST 2020
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 12:48:49 CST
Universal time: Mon 2020-03-30 04:48:49 UTC
RTC time: Mon 2020-03-30 12:48:49
Time zone: Asia/Shanghai (CST, +0800)

因为ntp是生效状态,所以修改完时区后时间立刻生效

5,设置是否启用ntp

[root@blog ~]# timedatectl set-ntp no
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 12:51:22 CST
Universal time: Mon 2020-03-30 04:51:22 UTC
RTC time: Mon 2020-03-30 12:51:22
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: inactive

注意: NTP service: 的值变成了 inactive

而且它还把chronyd给关闭了:

[root@blog ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:chronyd(8)

再次启用ntp

[root@blog ~]# timedatectl set-ntp yes
[root@blog ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-03-30 12:51:45 CST; 2s ago
Docs: man:chronyd(8)

...

[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 12:53:15 CST
Universal time: Mon 2020-03-30 04:53:15 UTC
RTC time: Mon 2020-03-30 12:53:15
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active

这个操作有点危险,因为会把chronyd服务关闭,大家操作时要注意

说明:目前的server基本上都会连接到公网,所以建议时间同步服务一定要打开

6,不连接到公网的情况下,可以手动修改日期时间

说明; set-time该命令同时更新系统时间和硬件时钟

ntp服务(chronyd)打开的情况下,手动修改时间会提示失败

看下面的例子:

[root@blog ~]# date
Mon Mar 30 12:58:24 CST 2020
[root@blog ~]# timedatectl set-time 11:58:30
Failed to set time: NTP unit is active

我们先手动关闭chronyd服务再修改时间:

[root@blog ~]# systemctl stop chronyd
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 13:01:26 CST
Universal time: Mon 2020-03-30 05:01:26 UTC
RTC time: Mon 2020-03-30 13:01:26
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: inactive

此时timedatectl也会输出NTP service的状态为inactive

再次修改时间:

[root@blog ~]# date
Mon Mar 30 13:02:50 CST 2020
[root@blog ~]# timedatectl set-time 11:02:30
[root@blog ~]# date
Mon Mar 30 11:02:31 CST 2020
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 11:02:36 CST
Universal time: Mon 2020-03-30 03:02:36 UTC
RTC time: Mon 2020-03-30 11:02:36
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: yes

此时的时间修改会成功

也可以同时修改日期时间:

[root@blog ~]# date
Mon Mar 30 11:04:53 CST 2020
[root@blog ~]# timedatectl set-time '2020-03-30 12:04:30'
[root@blog ~]# date
Mon Mar 30 12:04:33 CST 2020

也可只修改日期:

[root@blog ~]# timedatectl set-time 2020-03-29
[root@blog ~]# date
Sun Mar 29 00:00:02 CST 2020

上面的修改在启动时间同步服务chronyd后会自动同步回来

[root@blog ~]# systemctl start chronyd
[root@blog ~]# date
Mon Mar 30 13:07:37 CST 2020

7,设置rtc是否使用本地时间或utc时间

[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 13:28:43 CST
Universal time: Mon 2020-03-30 05:28:43 UTC
RTC time: Sun 2020-03-29 00:21:50
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot 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'.

注意这个末尾的warning信息,系统建议rtc使用utc时间,

所以我们应该按这个要求去做

设置rtc使用utc时间

[root@blog ~]# timedatectl set-local-rtc 0
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 13:29:10 CST
Universal time: Mon 2020-03-30 05:29:10 UTC
RTC time: Mon 2020-03-30 05:29:10
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

设置rtc使用本地时间

[root@blog ~]# timedatectl set-local-rtc 1
[root@blog ~]# timedatectl
Local time: Mon 2020-03-30 13:29:25 CST
Universal time: Mon 2020-03-30 05:29:25 UTC
RTC time: Mon 2020-03-30 13:29:25
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot 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'.

警告信息又回来了,所以建议rtc使用utc时间

四,查看centos的版本:

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)

centos8使用timedatectl管理时间的更多相关文章

  1. Ubuntu 16.04使用timedatectl进行管理时间(UTC/CST)(服务器/桌面)

    说明:16.04开始,systemd接管了系统之后就不再使用/etc/default/rcS和ntpdate.dpkg-reconfigure tzdata进行时间的管理,所以在这些地方设置是无效的, ...

  2. Linux下使用timedatectl命令时间时区操作详解

    timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系统和服务管理器的一部分,代替旧的传统的用在基于Linux分布 ...

  3. 使用moment.js管理时间

    如果在nodejs下 npm install moment 引用模块: var moment = require('moment'); 用法: 当前时间:2015-11-07 18:00:51 mom ...

  4. linux(centos8):用systemctl管理war包形式的jenkins(java 14 / jenkins 2.257)

    一,如何安装jenkins? 参见: https://www.cnblogs.com/architectforest/p/13685904.html 说明:刘宏缔的架构森林是一个专注架构的博客,地址: ...

  5. centos8使用systemctl管理运行级别

    一,什么是systemd的target? 1,关于systemd/systemctl的相关知识,请移步到这一篇 https://www.cnblogs.com/architectforest/p/12 ...

  6. 管理时间TED语录

    When people find out I write about time management, They assume two things. One is that I'm always o ...

  7. Linux_系统时间管理

    目录 目录 时间管理 date指令 系统时间设置timedatectl指令 本地时间同步 时间服务器NTP RHEL6 RHEL7 计划任务 一次性计划任务 at指令 限制用户建立一次性计划任务 周期 ...

  8. GTD时间管理(1)---捕获搜集

    前一段时间感觉自己的整个思路很混乱,每一天觉得自己有很多事情很多,但是坐着做着不知道自己做了多少,做项目的时候做着做着时常东想西想.我个人觉得这种想法是不对经的. 于是在google上都出去寻找这方面 ...

  9. (笔记)Linux内核学习(八)之定时器和时间管理

    一 内核中的时间观念 内核在硬件的帮助下计算和管理时间.硬件为内核提供一个系统定时器用以计算流逝的时间.系 统定时器以某种频率自行触发,产生时钟中断,进入内核时钟中断处理程序中进行处理. 墙上时间和系 ...

随机推荐

  1. [LeetCode] 221. 最大正方形(DP)

    题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 ...

  2. 内存管理初始化源码1:setup_arch

    源码声明:基于Linux kernel 3.08 1. 在kernel/arch/mips/kernel/head.S中会做一些特定硬件相关的初始化,然后会调用内核启动函数:start_kernel: ...

  3. 尚硅谷阳哥JVM笔记

    JVM体系结构 类加载器(快递员): 只负责加载java文件,编译后的class文件在文件开头有特定的文件表示,将class文件字节码内容从硬盘加载到JVM内存中并将这些内容转换成方法区的运行时数据结 ...

  4. 使用swagger遇到的问题

    1.定义全局的请求参数时, defaultValue不能是中文,不然一直是请求中 ParameterBuilder userName = new ParameterBuilder(); Paramet ...

  5. canvas的简单绘制及设置

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> ...

  6. python爬取虎牙直播颜值区美女主播照片

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...

  7. 深入理解HBase

    深入理解HBase: https://www.jianshu.com/p/b23800d9b227

  8. 坚果云+svn实现异地非局域网个人代码版本管理

    原理大概是A地的设备作为服务端创建仓库,将仓库传上坚果云,同步到B地,再拉取仓库的代码

  9. BUU reverse xxor

    下载下来的是个elf文件,因为懒得上Linux,直接往IDA里扔, 切到字符串的那个窗口,发现Congratulation!,应该是程序成功执行的表示, 双击,按'x',回车跟入 找到主函数: 1 _ ...

  10. 使用redis来调用iptables,封禁恶意IP

    话不多说,通常大多数站点都会有被薅羊毛的情况,防护无非也就是业务层做处理,短时内不再响应恶意请求啦.虽然不响应了,可还是会消耗资源的,比如我要从数据库(当然也可能是内存数据库)去查询下,你是不是恶意的 ...