linux-centos 设置系统时间

CentOS 系统提供了多种方式来设置和管理时间,包括手动设置时间和使用网络时间协议 (NTP) 自动同步时间。以下是几种常见的方法:

手动设置时间

  1. 使用date命令临时设置时间 :

    如果你只需要临时设置时间,可以使用 date 命令:

    bash 复制代码
    date -s "2023-09-13 14:09:00"

    这里的日期格式是 YYYY-MM-DD HH:MM:SS。

  2. 使用hwclock命令设置硬件时钟 :

    如果你想将当前系统时间写入硬件时钟(即 BIOS 或 CMOS 时钟),可以使用 hwclock 命令:

    bash 复制代码
    hwclock --set --date="2023-09-13 14:09:00"

注意:手动设置时间仅适用于特殊情况或测试环境,因为这样做可能会导致时间的不连续性和不一致性,尤其是在分布式系统中。

使用 NTP 自动同步时间

  1. 安装 NTP 客户端 :

    在 CentOS 上,你可以使用 chrony 或 systemd-timesyncd 作为 NTP 客户端。chrony 是一个功能更强大的 NTP 客户端,而 systemd-timesyncd 是轻量级的,通常用于桌面和嵌入式系统。

    • 安装 chrony:

      bash 复制代码
      sudo yum install chrony
    • 安装 systemd-timesyncd(大多数情况下已经默认安装):

      bash 复制代码
      sudo yum install systemd-timesyncd
  2. 配置 NTP 客户端:

    • 配置 chrony :

      编辑 /etc/chrony/chrony.conf 文件,在里面添加 NTP 服务器:

      plaintext 复制代码
      server 0.centos.pool.ntp.org iburst
      server 1.centos.pool.ntp.org iburst
    • 配置 systemd-timesyncd :

      编辑 /etc/systemd/timesyncd.conf 文件,添加或修改 NTP 服务器:

      plaintext 复制代码
      [Time]
      NTP=0.centos.pool.ntp.org 1.centos.pool.ntp.org
  3. 启动并设置为开机启动 :

    根据你选择的客户端,使用相应的命令:

    bash 复制代码
    sudo systemctl start chronyd
    sudo systemctl enable chronyd

    或者

    bash 复制代码
    sudo systemctl start systemd-timesyncd
    sudo systemctl enable systemd-timesyncd

使用timedatectl工具

timedatectl 是一个用于查询和更改系统日期和时间的工具,通常与 systemd-timesyncd 一起使用。你可以使用它来手动设置时间或检查当前的日期和时间设置:

bash 复制代码
sudo timedatectl set-time "2023-09-13 14:09:00"
sudo timedatectl status
相关推荐
少司府2 小时前
嵌入式外设篇(二)GPIO·二:GPIO输出与LED控制
linux·stm32·单片机·嵌入式硬件·mcu·51单片机·系统
Rabitebla3 小时前
【Linux操作系统】 文件篇(一):文件描述符与 Linux 文件管理机制
linux·网络·c++·笔记·学习
发疯的技术鼠5 小时前
03-优先级与调度
linux
无足鸟ICT6 小时前
【RHCA+】sosreport(收集诊断文档)
linux·运维·服务器
GeW6 小时前
世界目光聚焦中国:底层技术、Linux、数据库的破局逻辑
linux·数据库
IT大白鼠7 小时前
当 AI 开始自动化渗透:FBI ARTEX 警报的防守对策
运维·人工智能·自动化
Decay__7 小时前
SQLServer 运维自动化平台 —— PowerShell + SQL Agent 作业:部署、监控、巡检、告警、工单闭环一体化
运维·数据库·sql·mysql·架构
A-刘晨阳8 小时前
K8S 二进制集群搭建(一主两从)v1.37.2 版本
运维·云原生·容器·kubernetes·calico·nerdctl
tonydf8 小时前
没完了,Nginx又报新漏洞!
运维·nginx
星恒随风8 小时前
MySQL 从零入门(十):MVCC、Undo Log 与 Read View 详解——InnoDB 如何实现事务隔离?
linux·数据库·笔记·sql·mysql