天天看點

ubuntu下多機時間同步設定

測試平台:兩個機器,一個交換機

同步方法:使用ntp工具來同步時間

NTP簡介:NTP 是通過網絡來同步時間的一種 TCP/IP 協定。通常用戶端向伺服器請求目前的時間,并根據結果來設定其時鐘。

安裝方法:sudo apt-get install ntp(本地時間伺服器安裝,其他裝置可以不裝),sudo apt-get install ntpdate(都要安裝)。

使用方法:兩個機器,IP分别為192.168.1.112,192.168.1.113.使用IP為192.168.1.112的裝置擷取網絡時間(ntpdate -u cn.pool.ntp.org),并設定這個裝置作為本地的時間伺服器(配置ntp.conf檔案,下面會說到)。IP為192.168.1.113的裝置同步到本地的時間伺服器(ntpdate -u 192.168.1.112),這樣兩台機器的裝置即可同步。

配置檔案說明:

192.168.1.112的ntp配置檔案(/etc/ntp.conf)

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.

#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats

filegen loopstats file loopstats type day enable

filegen peerstats file peerstats type day enable

filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board

# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for

# more information.

pool 0.ubuntu.pool.ntp.org iburst

pool 1.ubuntu.pool.ntp.org iburst

pool 2.ubuntu.pool.ntp.org iburst

pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.

pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for

# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>

# might also be helpful.

#

# Note that "restrict" applies to both servers and clients, so a configuration

# that might be intended to block requests from certain clients could also end

# up blocking replies from your own upstream servers.

restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap#本地同網段的裝置可同步

# By default, exchange time with everybody, but don't allow configuration.

restrict -4 default kod notrap nomodify nopeer noquery limited

restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.

restrict 127.0.0.1

restrict ::1

# Needed for adding pool entries

restrict source notrap nomodify noquery

# 中國這邊最活躍的時間伺服器 : http://www.pool.ntp.org/zone/cn

server 210.72.145.44 perfer   # 中國國家受時中心

server 202.112.10.36             # 1.cn.pool.ntp.org

server 59.124.196.83             # 0.asia.pool.ntp.org

# allow update time by the upper server

# 允許上層時間伺服器主動修改本機時間

restrict 210.72.145.44 nomodify notrap noquery

restrict 202.112.10.36 nomodify notrap noquery

restrict 59.124.196.83 nomodify notrap noquery

# 外部時間伺服器不可用時,以本地時間作為時間服務

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

# Clients from this (example!) subnet have unlimited access, but only if

# cryptographically authenticated.

#restrict 192.168.123.0 mask 255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.

# (Again, the address is an example only.)

#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the

# next lines.  Please do this only if you trust everybody on the network!

#disable auth

#broadcastclient

#Changes recquired to use pps synchonisation as explained in documentation:

#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS

#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)

#fudge 127.127.22.1 flag3 1            # enable PPS API

192.168.1.113的配置檔案使用預設的即可,通過ntpdate -u 192.168.1.112即可同步本地時間伺服器的時間。

注意:

  如果重新開機ntp守護程序後,闆卡有可能無法立即更新時間,提示錯誤“no server suitable for synchronization found”,這是因為每次重新開機NTP伺服器之後大約要3-5分鐘用戶端才能與伺服器建立正常的通訊連接配接,等待一會兒再更新就可以了。

  有可能防火牆會阻礙更新時間,這時可以加上“-u”參數。

繼續閱讀