ntp时间同步

ntp时间同步服务

ntp是网络时间协议,它是用来同步网络中各个计算机的时间的协议
时间是一个很重要的单位概念,很多服务部署,因为时间的不同步,都会导致出错,增加排错难度。如:

  • 定时任务的执行
  • 应用数据的同步
  • 必须保证服务器之间的时间一致,非常重要

timedatectl

  • 修改Linux的时间、日期
  • date 改时间日期
  • hwclock 改硬件时间
 status                   Show current time settings 查看当前状态
  set-time TIME            Set system time  设置当前的时间 
  set-timezone ZONE        Set system time zone 设置当前的时区
  list-timezones           Show known time zones 查看系统支持哪些时区

查看当前时间

[root@zz ~]# timedatectl
      Local time: Mon 2022-03-21 16:43:38 CST
  Universal time: Mon 2022-03-21 08:43:38 UTC
        RTC time: Mon 2022-03-21 08:43:38
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

[root@zz ~]# timedatectl status #查看当前状态
      Local time: Mon 2022-03-21 16:46:12 CST
  Universal time: Mon 2022-03-21 08:46:12 UTC
        RTC time: Mon 2022-03-21 08:46:11
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

用法

 查看时间
[root@zz ~]# timedatectl set-time '2000-10-28 19:00'

 查看系统支持多少时区
[root@zz ~]# timedatectl list-timezones|wc -l
425
 找出某个时区,并设置其为当前时区
[root@zz ~]# timedatectl list-timezones|grep -i 'shanghai'
Asia/Shanghai
[root@zz ~]# timedatectl set-timezone Asia/Shanghai
[root@zz ~]# 
[root@zz ~]# timedatectl
      Local time: Mon 2022-03-21 17:03:08 CST
  Universal time: Mon 2022-03-21 09:03:08 UTC
        RTC time: Mon 2022-03-21 09:03:08
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

ntp时间同步

  • ntpdate 强制性更新整个系统的时间,不友好的强制同步
  • 搭建ntp服务,自动的,友好的更新校正系统时间

ntpdate

找到时间服务器地址,强制更新
[root@zz ~]# ntpdate -u ntp.aliyun.com
21 Mar 17:12:06 ntpdate[7398]: adjust time server 203.107.6.88 offset -0.000448 sec

搭建ntpd服务

1.安装ntp软件
[root@zz ~]# ntpdate -u ntp.aliyun.com

2.查看软件信息
[root@zz ~]# ls /usr/lib/systemd/system/ | grep ntp

3.找到ntp软件的配置文件
 rpm -ql ntp |grep conf

4.修改ntp配置文件
vim /etc/ntp.conf

  4 driftfile /var/lib/ntp/drift
  5 
  6 #添加ntp的运行日志
  7 logfile /var/log/my_ntp.log
  8 
  9 #记录程序运行进程号,可以用于写脚本,读取这个文件,就找到了程序的进程ID
 10 pidfile /var/run/ntpd.pid
 11 
 12 # Permit time synchronization with our time source, but do not
 13 # permit the source to query or modify the service on this system.
 14 restrict default nomodify notrap nopeer noquery
 15 
 16 # Permit all access over the loopback interface.  This could
 17 # be tightened as well, but to do so would effect some of
 18 # the administrative functions.
 19 restrict 127.0.0.1
 20 restrict ::1
 21 
 22 # Hosts on local network are less restricted.
 23 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
 24 
 25 # Use public servers from the pool.ntp.org project.
 26 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
 27 #server 0.centos.pool.ntp.org iburst
 28 #server 1.centos.pool.ntp.org iburst
 29 #server 2.centos.pool.ntp.org iburst
 30 #server 3.centos.pool.ntp.org iburst
 31 
 32 server ntp.aliyun.com iburst prefer
 33 server cn.pool.ntp.org iburst
添加6、7、9、10、32、33行

5.修改机器的时间为错误时间
[root@zz ~]# timedatectl set-time '2000-11-23 19:00'
[root@zz ~]# timedatectl status
      Local time: Thu 2000-11-23 19:00:02 CST
  Universal time: Thu 2000-11-23 11:00:02 UTC
        RTC time: Thu 2000-11-23 11:00:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
6.启动ntpd服务,等待时间是否同步
[root@zz ~]# systemctl start  ntpd
[root@zz ~]# 
[root@zz ~]# systemctl status  ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-03-21 17:40:34 CST; 21 years 3 months left
 Main PID: 7765 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─7765 /usr/sbin/ntpd -u ntp:ntp -g

Mar 21 17:40:34 zz systemd[1]: Starting Network Time Service...
Mar 21 17:40:34 zz systemd[1]: Started Network Time Service.
Mar 21 17:40:34 zz ntpd[7765]: proto: precision = 0.040 usec
Mar 21 17:40:34 zz ntpd[7765]: 0.0.0.0 c01d 0d kern kernel time sync enabled

7.查看ntp是否与上游服务器同步
[root@zz ~]# ntpstat
synchronised to unspecified (203.107.6.88) at stratum 3
   time correct to within 957 ms
   polling server every 64 s

8.查看时间同步状态
[root@zz ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 203.107.6.88    10.137.55.181    2 u   12   64   57   14.129   -0.910 4405024
 ntp1.flashdance 192.36.143.152   2 u    5   64   43  272.912  6728785 5827299

* 这样一个可当做上游时间服务器的机器就配完了
posted @ 2022-03-21 19:46  Fyy^  阅读(833)  评论(0编辑  收藏  举报