1. 程式人生 > 其它 >Linux知識點[001]:NTP服務搭建

Linux知識點[001]:NTP服務搭建

環境描述

  • 共使用三臺虛擬機器進行測試。
  • 主要修改配置檔案:/etc/ntp.conf。
  • NTP服務開放的埠為:123
  • 基礎環境:
    • 關閉了firewalld。
    • 關閉了SElinux。

安裝NTP服務

在NTP Server和NTP Server上都需要執行。

  • 確認是否已安裝ntp

    rpm -qa ntp*
    ntp-4.2.6p5-29.el7.centos.2.x86_64
    ntpdate-4.2.6p5-29.el7.centos.2.x86_64
    
  • 如果未安裝,使用以下命令安裝

    yum -y insatll ntp
    
  • 註釋/etc/ntp.conf的內容

    server 0.centos.pool.ntp.org iburst
    server 1.centos.pool.ntp.org iburst
    server 2.centos.pool.ntp.org iburst
    server 3.centos.pool.ntp.org iburst
    

NTP Server

  • 修改配置檔案/etc/ntp.conf,在檔案中新增以下內容。

    #將本機作為NTP伺服器的服務端。
    server  127.127.1.0 iburst
    
  • 修改完成後,開啟NTP服務並開機自啟。

    [root@node01 ~]# systemctl --now enable ntpd
    
  • 檢視NTP服務狀態。

    [root@node01 ~]# systemctl status ntpd
    
  • 查詢網路中的NTP伺服器,同時顯示客戶端和每個伺服器的關係。

    [root@node01 ~]# ntpq -p
    remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *LOCAL(0)        .LOCL.      5 l   48   64  377    0.000    0.000   0.000
    
  • 查詢在本地計算機上執行的NTP守護程式(ntpd)的同步狀態。

    [root@node01 ~]# ntpstat
    synchronised to local net (127.127.1.0) at stratum 6
       time correct to within 12 ms
       polling server every 64 s
    
  • 檢視123埠是否開放。

    [root@node01 ~]# lsof -i:123
    
  • 檢視同步狀態,NTP synchronized: yes此項為yes為同步正常。

    [root@node01 ~]# timedatectl
          Local time: Wed 2021-06-30 12:57:52 CST
      Universal time: Wed 2021-06-30 04:57:52 UTC
            RTC time: Wed 2021-06-30 04:57:51
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: yes
    NTP synchronized: yes
     RTC in local TZ: no
          DST active: n/a
    

NTP Client

  • 修改配置檔案/etc/ntp.conf,在檔案中新增以下內容。

    #將10.0.0.11作為NTP伺服器的Server端。
    server 10.0.0.11 iburst
    
  • 修改完成後,開啟NTP服務並開機自啟。

    [root@node02 ~]# systemctl --now enable ntpd
    
  • 檢視NTP服務狀態。

    [root@node02 ~]# systemctl status ntpd
    
  • 查詢網路中的NTP伺服器,同時顯示客戶端和每個伺服器的關係。

    [root@node02 ~]# ntpq -p
    remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *10.0.0.11       LOCAL(0)    6 u   25   64    7    0.810    0.054   0.162
    
  • 查詢在本地計算機上執行的NTP守護程式(ntpd)的同步狀態。

    [root@node02 ~]# ntpstat
    synchronised to NTP server (10.0.0.11) at stratum 7
       time correct to within 953 ms
       polling server every 64 s
    
  • 檢視123埠是否開放。

    [root@node01 ~]# lsof -i:123
    
  • 檢視同步狀態,NTP synchronized: yes此項為yes為同步正常。

    [root@node02 ~]# timedatectl
          Local time: Wed 2021-06-30 12:57:38 CST
      Universal time: Wed 2021-06-30 04:57:38 UTC
            RTC time: Wed 2021-06-30 04:57:37
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: yes
    NTP synchronized: yes
     RTC in local TZ: no
          DST active: n/a
    

注意

  • 啟動NTP服務之後,使用ntpq -p檢視正常,使用lsof -i:123埠正常開放,使用timedatectl檢視同步NTP synchronized: no,使用ntpstat出現unsynchronised polling server every 8 s,這需要等待約2分鐘左右再次檢視同步的狀態。