時間同步-使用win7做server,linux做client端__linux

來源:互聯網
上載者:User

  在調試分布式系統時,日誌是個很好的工具,比如使用log4j記錄的日誌。然而如果各機器的時間不一致,則不能方便地判斷各事件的順序。因此時間同步就很有用了,廣為人知的是ntp協議。本文探討如何使用win7作ntp server,linux做ntp client使得win7和linux系統的時間保持一致。
  
  1. 在win7上安裝ntp server
  1) 首先從http://www.meinberg.de/english/sw/ntp.htm#ntp_nt_stable上下載一個最新的ntp伺服器,寫本文時該頁面如下圖,紅色部分為要下載的ntp server。
 
  

  2) 直接安裝(預設會安裝到C:\Program Files\NTP目錄),中間會出現讓你選擇上級伺服器(即本機時間以其為基準定期同步),安裝時先保留預設值。
  安裝完成後,產生開始菜單如下圖所示:
    
  3)開啟C:\Program Files\NTP\etc\ntp.conf檔案,修改配置:(可通過資源管理員瀏覽開啟,從開始菜單"Edit NTP Configuration"開啟時要右鍵以管理員身份開啟,以能儲存修改)
  我的設定檔內容如下:

# NTP Network Time Protocol 

# **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes* 

# PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED 

# Configuration File created by Windows Binary Distribution Installer Rev.: 1.26  mbg

# please check http://www.ntp.org for additional documentation and background information

# Use drift file 

driftfile "D:\develop\NTP\etc\ntp.drift"



# your local system clock, should be used as a backup

# (this is only useful if you need to distribute time no matter how good or bad it is)

server 127.127.1.0

# but it operates at a high stratum level to let the clients know and force them to

# use any other timesource they may have.

fudge 127.127.1.0 stratum 12 



# Use a NTP server from the ntp pool project (see http://www.pool.ntp.org)

# Please note that you need at least four different servers to be at least protected against

# one falseticker. If you only rely on internet time, it is highly recommended to add

# additional servers here. 

# The 'iburst' keyword speeds up initial synchronization, please check the documentation for more details!

 server 0.asia.pool.ntp.org iburst

 server 1.asia.pool.ntp.org iburst

 server 2.asia.pool.ntp.org iburst

 server 0.us.pool.ntp.org iburst

 server 1.us.pool.ntp.org iburst

 server 2.us.pool.ntp.org iburst

# End of generated ntp.conf --- Please edit this to suite your needs


  注意,上面的時間伺服器如果不可用可以從網上找出可用的伺服器ip來替換。

  4) 關閉防火牆。注意一定要關閉win7作業系統內建的防火牆。否則在用戶端更新時會出現“No Server suitable for synchronization found ” 錯誤。
  5) 從開始菜單重啟NTP Service。
  6) 確定ntp service是否工作:使用命令ntpq -p

  2. 在linux系統上配置定期與win7時間同步
  執行ntpdate 192.168.1.32 可將本機時間與win7同步(192.168.1.32為win7的ip)。
  注意:linux上運行了ntpd進程時,ntpdate命令執行會失敗,出現the NTP socket is in use, exiting。執行:ps -aef | grep ntpd找到該進程,然後kill掉即可。
  
  一般情況下我們希望定期進行同步,這可使用linux中的cron實現:
  如我想每隔10分鐘和win7同步時間可以使用crontab -e 編輯crontab工作內容如下:
   */10 * * * * /usr/sbin/ntpdate 192.168.1.32&&/sbin/hwclock-w 
  
  上面的hwclock-w命令是用於更新BIOS時間,從而在系統重啟時間時間能夠儲存。 
  


  
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.