0


Linux系统之时间同步方法

Linux系统之时间同步方法

一、使用NTP服务时间同步

1.安装ntp

[root@node ~]# yum -y install ntp

2.启动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.

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

4.修改ntp.conf文件

[root@node ~]# vim /etc/ntp.conf
[root@node ~]# grep server /etc/ntp.conf 
#Use public servers from the pool.ntp.org project.#server0.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
#broadcast192.168.1.255 autokey    # broadcast server#broadcast224.0.1.1 autokey        # multicast server#manycastserver239.255.254.254        # manycast server

5.重启服务

[root@node ~]# systemctl restart ntpd

6.检查同步状态

[root@node ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================203.107.6.88100.107.25.1142 u    264130.975149.4990.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

2.查看时间

[root@node ~]# date
Thu Aug  400: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

2.处理方法

cna02:~ # service ntpd stop
Redirecting to /bin/systemctl stop ntpd.service

3.重新同步

cna02:~ # ntpdate ntp1.aliyun.com
 3 Aug 23:22:36 ntpdate[28980]: step time server 120.25.115.20 offset -28911.609110 sec

4.查看时间是否正确

cna02:~ # date
Wed Aug  323: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!

2.启动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.

3.查看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-0400: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 0400:12:27 node systemd[1]: Starting NTP client/server...
Aug 0400:12:27 node chronyd[21134]: chronyd version 3.4starting(+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 0400:12:27 node systemd[1]: Started NTP client/server.
Aug 0400:12:35 node chronyd[21134]: Selected source 193.182.111.141
Aug 0400:12:35 node chronyd[21134]: Source 78.46.102.180 replaced with 94.237.64.20

4.修改配置文件

[root@node ~]# grep server /etc/chrony.conf 
#Use public servers from the pool.ntp.org project.#server0.centos.pool.ntp.org iburst#server1.centos.pool.ntp.org iburst#server3.centos.pool.ntp.org iburst
server ntp1.aliyun.com iburst

5.重启服务

[root@node ~]# systemctl restart chronyd

6.检查时间同步状态

[root@node ~]# chronyc sources
210 Number of sources =1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================^*120.25.115.20267126+751us[+1549us]+/-19ms

五、手动修改时间

1.修改时间方法1

[root@node ~]# date -s "2012-05-23 01:01:01"
Wed May 2301:01:01 CST 2012[root@node ~]# date
Wed May 2301:01:05 CST 2012

2.修改时间方法2

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

[root@tianyi ~]# date 090621282021.28
Mon Sep  621:28:28 CST 2021

3.硬件时间向系统时间同步

[root@node ~]# hwclock -w

4.按格式输出时间

[root@node ~]# date "+%Y-%m-%d %H:%M:%S"2012-05-2301:04:10

5.设置时区

[root@tianyi backups]# timedatectl list-timezones |grep Shanghai
Asia/Shanghai
[root@tianyi backups]# timedatectl set-timezone Asia/Shanghai
标签: linux 服务器 网络

本文转载自: https://blog.csdn.net/jks212454/article/details/126151111
版权归原作者 江湖有缘 所有, 如有侵权,请联系我们删除。

“Linux系统之时间同步方法”的评论:

还没有评论