首页 > 系统相关 >Linux系统时间同步方法

Linux系统时间同步方法

时间:2023-04-14 09:23:18浏览次数:37  
标签:00 同步 root 08 master01 Linux k8s 方法 ntpd

使用NTP服务时间同步

1、安装ntp。

[root@k8s-master01 ~]# yum -y install ntp

 2、启动ntp服务。

[root@k8s-master01 ~]# systemctl start ntpd
[root@k8s-master01 ~]# 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@k8s-master01 ~]# 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

4、修改ntp.conf文件。

# 添加如下内容
[root@k8s-master01 ~]# vim /etc/ntp.conf
[root@k8s-master01 ~]# grep server /etc/ntp.conf 
server ntp.aliyun.com iburst

5、重启服务。

[root@k8s-master01 ~]# systemctl restart ntpd

 6、检查同步状态。

[root@k8s-master01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*stratum2-1.ntp. 194.190.168.1    2 u    4   64    1  170.653    6.791  40.306
+ntp1.as200552.n 195.66.241.3     2 u    4   64    1  236.322    9.481  32.132
+time.cloudflare 10.190.9.35      3 u    3   64    1  220.805  -17.283  20.695
-time.cloudflare 10.210.8.4       3 u    2   64    1  242.932   -4.425  43.233
-203.107.6.88    100.107.25.114   2 u    1   64    1   41.403  -17.157  11.194

使用ntpdate同步

1、使用ntpd命令同步。

[root@k8s-master01 ~]# ntpdate ntp.aliyun.com
14 Apr 08:51:28 ntpdate[1301]: adjust time server 203.107.6.88 offset 0.000744 sec

2、查看时间。

[root@k8s-master01 ~]# date
Fri Apr 14 08:51:53 CST 2023

使用chrony服务同步时间

1、安装chrony服务。

[root@k8s-master01 ~]# yum -y install chrony
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                            | 3.6 kB     00:00     
docker-ce-stable                                | 3.5 kB     00:00     
extras                                          | 2.9 kB     00:00     
kubernetes                                      | 1.4 kB     00:00     
updates                                         | 2.9 kB     00:00     
wlnmp                                           | 2.9 kB     00:00     
(1/8): docker-ce-stable/7/x86_64/updateinfo       |   55 B   00:00     
(2/8): base/7/x86_64/group_gz                     | 153 kB   00:01     
(3/8): docker-ce-stable/7/x86_64/primary_db       | 102 kB   00:01     
(4/8): kubernetes/primary                         | 129 kB   00:00     
(5/8): extras/7/x86_64/primary_db                 | 249 kB   00:01     
(6/8): wlnmp/7/x86_64/primary_db                  | 310 kB   00:00     
(7/8): base/7/x86_64/primary_db                   | 6.1 MB   00:25     
(8/8): updates/7/x86_64/primary_db                |  20 MB   01:17     
kubernetes                                                     953/953
Package chrony-3.4-1.el7.x86_64 already installed and latest version
Nothing to do

2、启动chrony服务。

[root@k8s-master01 ~]# systemctl enable --now chronyd

3、查看chrony服务状态。

[root@k8s-master01 ~]# 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 2023-04-14 08:55:42 CST; 1min 34s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1363 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1359 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1361 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─1361 /usr/sbin/chronyd

Apr 14 08:55:42 k8s-master01 systemd[1]: Starting NTP client/server...
Apr 14 08:55:42 k8s-master01 chronyd[1361]: chronyd version 3.4 star...
Apr 14 08:55:42 k8s-master01 chronyd[1361]: Frequency -93.957 +/- 22...
Apr 14 08:55:42 k8s-master01 systemd[1]: Started NTP client/server.
Apr 14 08:55:50 k8s-master01 chronyd[1361]: Selected source 2606:470...
Apr 14 08:55:53 k8s-master01 chronyd[1361]: Source 193.182.111.142 r...
Hint: Some lines were ellipsized, use -l to show in full.

4、给chrony服务配置文件添加内容。

[root@k8s-master01 ~]# vim /etc/chrony.conf
# 添加内容如下
server ntp1.aliyun.com iburst

5、重启服务。

[root@k8s-master01 ~]# systemctl restart chronyd

6、检查时间同步状态。

[root@k8s-master01 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? tick.ntp.infomaniak.ch        0   6     0     -     +0ns[   +0ns] +/-    0ns
^+ vsrv02141.customer.xenwa>     3   6    17    37    +11ms[+1532us] +/-  140ms
^+ 2402:4e00:1800:f800:0:95>     2   6    17    38    +20ms[  +11ms] +/-  133ms
^* time.cloudflare.com           3   6    17    37  +2046us[-7170us] +/-  129ms

手动修改时间

1、修改时间方法1

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

2、修改时间方法2

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

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

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

[root@k8s-master01 ~]# hwclock -w

4、按格式输出时间

[root@k8s-master01 ~]# date "+%Y-%m-%d %H:%M:%S"
2012-05-23 01:04:10

5、设置时区

[root@k8s-master01 ~]# timedatectl list-timezones | grep Shanghai
Asia/Shanghai
[root@k8s-master01 ~]# timedatectl set-timezone Asia/Shanghai

标签:00,同步,root,08,master01,Linux,k8s,方法,ntpd
From: https://www.cnblogs.com/Bookcict/p/17316411.html

相关文章

  • 两种list求差集的方法
    第一种:两个UserInfo实体List,一个全部用户集合List<UserInfo>allUser,一个部分用户集合List<UserInfo>commentUser,根据UserInfo中的UserID求差集,从allUser中得到剩下的一部分用户,通过stream流和lamda表达式实现publicList<UserInfo>getNotComment(List<UserInfo>allU......
  • Linux中使用ntpdate同步失败报错:14 Apr 08:42:12 ntpdate[1255]: the NTP socket is i
    报错信息: 报错原因:1、可能是因为同步的域名信息没有解析到。2、可能是因为服务的问题导致没有同步成功。 解决方法:1、先关闭ntpd服务。[root@k8s-master01~]#servicentpdstopRedirectingto/bin/systemctlstopntpd.service 2、重新同步。[root@k8s-maste......
  • linux设备树-linux内核设备树移植(二)
    ----------------------------------------------------------------------------------------------------------------------------内核版本:linux5.2.8根文件系统:busybox1.25.0u-boot:2016.05-------------------------------------------------------------------------------......
  • Linux copy from pipe command All In One
    LinuxcopyfrompipecommandAllInOneshutdown#Linux关机$shutdown--help$manshutdownRaspberryPi#RaspberryPi???$manshutdown|???macOS#macOS✅$manshutdown|pbcopy#手动复制粘贴到shutdown.md✅$manshutdown#自动导出❌$......
  • 防止网段改变的方法
    如果我们同学出现在学校用的好好的,回家后突然发现你的网段变了,比如你192.168.91.128 此时现在在学校是91,回到家后不是91了,此时怎么办呢,你此时就按照下面步骤的操作,把默认租用时间、最长租用时间调到最大值就行1.打开虚拟网络编辑器窗口,然后点击DHCP设置按钮  2.把默认租用......
  • Linux Shell概述
     Linux系统的Shell作为操作系统的外壳,为用户提供使用操作系统的接口。它是命令语言、命令解释程序及程序设计语言的统称。Shell的分类:BourneShellCShellKornShellBourne-AgainShell Shell的基本功能:命令的解释执行环境变量的设置输入输出的重定向shell程序语言的设计Lin......
  • Linux环境tomcat安装步骤详细教程
    下载tomcat包:https://tomcat.apache.org/tomcat-10.1-doc/index.html将下载的安装包放至linux环境的/usr/local/tomcat目录下:执行解压命令tar-xvfapache-tomcat-9.0.73.tar.gz将解压出来的目录改名为tomcat9,然后进入tomcat9/bin目录,执行./startup.sh命令启动tomcat:执行启动命令......
  • Linux运维进阶之路
    前言首先在我看来,不论你以后是做运维亦或者是做后端开发,云计算等。只要和后端搭边,Linux都是必会的基础知识。所以说Linux是伴随我们工作中一个特别重要的知识。 不过很多同学在初学Linux的时候,始终不得其法,发现越学越摸不着头脑。到最后不了了之。   在我看来,这些同学......
  • Linux运维进阶之路
    前言首先在我看来,不论你以后是做运维亦或者是做后端开发,云计算等。只要和后端搭边,Linux都是必会的基础知识。所以说Linux是伴随我们工作中一个特别重要的知识。 不过很多同学在初学Linux的时候,始终不得其法,发现越学越摸不着头脑。到最后不了了之。   在我看来,这些同学......
  • Linux运维进阶之路
    前言首先在我看来,不论你以后是做运维亦或者是做后端开发,云计算等。只要和后端搭边,Linux都是必会的基础知识。所以说Linux是伴随我们工作中一个特别重要的知识。 不过很多同学在初学Linux的时候,始终不得其法,发现越学越摸不着头脑。到最后不了了之。   在我看来,这些同学......