Redian新闻
>
Linux 系统时间老是不准怎么办?

Linux 系统时间老是不准怎么办?

公众号新闻

来自:入门小站

一、使用 NTP 服务时间同步

  1. 安装 ntp
[root@node ~]# yum -y install ntp
  1. 启动 ntp 服务
[root@node ~]# systemctl start  ntpd
[root@node ~]# systemctl enable  ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  1. 查看 ntp 服务
[root@node ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-08-04 08:00:47 CST; 7h left
 Main PID: 20746 (ntpd)
    Tasks: 1
   Memory: 768.0K
   CGroup: /system.slice/ntpd.service
           └─20746 /usr/sbin/ntpd -u ntp:ntp -g

Aug 04 08:00:47 node ntpd[20746]: Listen normally on 6 lo ::1 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listen normally on 7 ens33 fe80::8a83:7297:7cb8:9934 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listening on routing socket on fd #24 for interface updates
Aug 04 08:00:47 node systemd[1]: Started Network Time Service.
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c016 06 restart
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c011 01 freq_not_set
Aug 04 08:00:56 node ntpd[20746]: 0.0.0.0 c61c 0c clock_step -28798.353463 s
Aug 04 00:00:57 node ntpd[20746]: 0.0.0.0 c614 04 freq_mode
Aug 04 00:00:58 node ntpd[20746]: 0.0.0.0 c618 08 no_sys_peer
  1. 修改 ntp.conf 文件

[root@node ~]# vim /etc/ntp.conf
[root@node ~]# grep server /etc/ntp.conf 
# Use public servers from the pool.ntp.org project.
#server 0.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcast 224.0.1.1 autokey  # multicast server
#manycastserver 239.255.254.254  # manycast server
  1. 重启服务
[root@node ~]# systemctl restart ntpd
  1. 检查同步状态

[root@node ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 203.107.6.88    100.107.25.114   2 u    2   64    1   30.975  149.499   0.393

二、使用 ntpdate 同步

  1. 使用 ntpd 命令同步
[root@node ~]# ntpdate ntp.aliyun.com
 4 Aug 00:07:17 ntpdate[20924]: adjust time server 203.107.6.88 offset -0.001543 sec
  1. 查看时间
[root@node ~]# date
Thu Aug  4 00:07:46 CST 2022

三、处理 ntpdate 同步失败报错处理方法

  1. 报错信息
cna02:~ # ntpdate ntp1.aliyun.com
 4 Aug 07:23:41 ntpdate[18694]: the NTP socket is in use, exiting
  1. 处理方法
cna02:~ # service ntpd stop
Redirecting to /bin/systemctl stop ntpd.service
  1. 重新同步
cna02:~ # ntpdate ntp1.aliyun.com
 3 Aug 23:22:36 ntpdate[28980]: step time server 120.25.115.20 offset -28911.609110 sec
  1. 查看时间是否正确
cna02:~ # date
Wed Aug  3 23:22:41 CST 2022

四、使用 chrony 服务时间同步

  1. 安装 chrony
[root@node ~]# yum -y install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.2-2.el7 will be updated
---> Package chrony.x86_64 0:3.4-1.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================
 Package                                  Arch                                     Version                                        Repository                              Size
===============================================================================================================================================================================
Updating:
 chrony                                   x86_64                                   3.4-1.el7                                      base                                   251 k

Transaction Summary
===============================================================================================================================================================================
Upgrade  1 Package

Total download size: 251 k
Downloading packages:
No Presto metadata available for base
chrony-3.4-1.el7.x86_64.rpm                                                                                                                             | 251 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : chrony-3.4-1.el7.x86_64                                                                                                                                     1/2 
  Cleanup    : chrony-3.2-2.el7.x86_64                                                                                                                                     2/2 
  Verifying  : chrony-3.4-1.el7.x86_64                                                                                                                                     1/2 
  Verifying  : chrony-3.2-2.el7.x86_64                                                                                                                                     2/2 

Updated:
  chrony.x86_64 0:3.4-1.el7                                                                                                                                                    

Complete!
  1. 启动 chrony 服务
[root@node ~]# systemctl enable --now chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
  1. 查看 chrony 状态
[root@node ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2022-08-04 00:12:27 CST; 29s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 21136 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 21132 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 21134 (chronyd)
    Tasks: 1
   Memory: 364.0K
   CGroup: /system.slice/chronyd.service
           └─21134 /usr/sbin/chronyd

Aug 04 00:12:27 node systemd[1]: Starting NTP client/server...
Aug 04 00:12:27 node chronyd[21134]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 04 00:12:27 node systemd[1]: Started NTP client/server.
Aug 04 00:12:35 node chronyd[21134]: Selected source 193.182.111.141
Aug 04 00:12:35 node chronyd[21134]: Source 78.46.102.180 replaced with 94.237.64.20
  1. 修改配置文件
[root@node ~]# grep server /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com iburst
  1. 重启服务
[root@node ~]# systemctl restart chronyd
  1. 检查时间同步状态
[root@node ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 120.25.115.20                 2   6    71    26   +751us[+1549us] +/-   19ms

五、手动修改时间

  1. 修改时间方法 1
[root@node ~]# date -s "2012-05-23 01:01:01"
Wed May 23 01:01:01 CST 2012
[root@node ~]# date
Wed May 23 01:01:05 CST 2012
  1. 修改时间方法 2

修改时间顺序为月日时分年. 秒

[root@tianyi ~]# date 090621282021.28
Mon Sep  6 21:28:28 CST 2021
  1. 硬件时间向系统时间同步
[root@node ~]# hwclock -w
  1. 按格式输出时间
[root@node ~]# date "+%Y-%m-%d %H:%M:%S"
2012-05-23 01:04:10
  1. 设置时区
[root@tianyi backups]# timedatectl list-timezones |grep Shanghai
Asia/Shanghai
[root@tianyi backups]# timedatectl set-timezone Asia/Shanghai

END

官方站点:www.linuxprobe.com

Linux命令大全:www.linuxcool.com

刘遄老师QQ:5604215

Linux技术交流群:3861509

(新群,火热加群中……)

想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
570个常用的Linux命令,1349页Linux命令速查手册(附PDF)7 个超轻量级 Linux 发行版 | Linux 中国Agustín Hernández:中美洲建筑背景下的未来主义巨构8 个比 Linux 系统本身还要古老的 Linux 概念禁套套(TT,Tiktok)算个啥?被禁的APP大把在路上超越 Bash: 9 个不太知名的 Linux Shell 及其功能 | Linux 中国Reminders:一个漂亮的开源 Linux 应用,可帮助你完成工作 | Linux 中国大家最喜欢的 Linux 终端字体 | Linux 中国Arch Linux 的最佳 GUI 包管理器 | Linux 中国如何在 Linux 系统中使用 Ventoy 创建多重引导的 U 盘 | Linux 中国测评适用于 Linux 中 Wayland 的最佳屏幕录制软件 | Linux 中国如何入门 Linux Shell 脚本编写 | Linux 中国Kali Linux 简介:你需要了解的一切 | Linux 中国使用 ChatGPT AI 从英文文本生成 Linux 命令 | Linux 中国Alpine Linux 3.18 发布 | Linux 中国如何在 Linux 中合并 PDF 文件 | Linux 中国修行从否定自己开始花木兰 杜甫与卖炭翁 茅屋塌了(写于富士康抓壮丁期间)570个Linux超全命令大全,带你实现Linux命令自由终端基础:在 Linux 中删除文件和文件夹 | Linux 中国8 款最佳的 Linux 远程桌面工具 | Linux 中国微软发布自己的 Linux 发行版,“ Linux 是种癌症 ” 影响犹在如何在 Rocky Linux 9 / AlmaLinux 9 上安装 KVM | Linux 中国TUXEDO Stellaris 16(Gen5)是目前所能找到的终极 Linux 笔记本电脑 | Linux 中国Voyager Linux 23.04:具有华丽外观且无 Snap 的 Ubuntu 体验 | Linux 中国risiOS:一个易于使用的基于 Fedora 的 Linux 发行版 | Linux 中国11 个不可变 Linux 发行版,适合那些想要拥抱未来的人们 | Linux 中国Linux 有多重要?这么说吧,只要是干 IT 相关的,学 Linux 是绕不过去的 “坎儿”漫游大华府(10)华盛顿故居在 Linux 上用 Kdenlive 编辑视频 | Linux 中国Ubuntu 18.04 即将停止更新,你该怎么办? | Linux 中国微软发布自己的 Linux 发行版,“Linux是种癌症”影响犹在《美的让人醉》&《我把吉祥送给你》优化 Linux 服务器的 9 个小技巧,让 Linux 服务器效率起来微软发布自己的Linux发行版,“Linux是种癌症”影响犹在
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。