CentOS 7 服务器对时两种方式

1,050 阅读1分钟

修改时区

将当前机器时区调整为上海

方式1 覆盖文件
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 

方式2 链接文件
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

对时

  1. 运行以下命令更新硬件时钟(RTC)

    hwclock -w
    
  2. 运行以下命令查看时区

    timedatectl status
    

    查询结果如下所示,时区已修改为Shanghai

      Local time: Thu 2021-09-30 16:52:14 CST
      Universal time: Thu 2021-09-30 08:52:14 UTC
            RTC time: Thu 2021-09-30 08:52:14
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: yes
    NTP synchronized: no
     RTC in local TZ: no
          DST active: n/a
    

一、ntpdate + 定时任务 方式

网络时间协议(英語:Network Time Protocol,缩写:NTP

  1. 更新yum 相关包
yum update -y
  1. 利用 ntpdate 程序来完成对时
yum install -y ntpdate

根据网络网络情况,找一台 NTP 服务器 阿里云NTP服务器

这里选择 公网 ntp1.aliyun.com 作为 NTP 服务器

  1. 构建定时任务
crontab -e #进入编辑页面
*/10 * * * * /usr/sbin/ntpdate ntp1.aliyun.com | logger -t NTP

二、chrony

  1. 启用Chrony服务

    systemctl start chronyd.service
    systemctl enable chronyd.service
    
  2. 运行以下命令查看本机时间同步状态,用于验证服务是否已启动。

    chronyc tracking
    
  3. 运行以下命令查看时间同步服务器列表。

    chronyc -n sources -v
    
  4. 配置Chrony服务,可增删 NTP 服务器

    vim /etc/chrony.conf