linux設置自動同步服務器時間

最近遇到一個問題,因爲兩臺服務器時間的問題,常常致使用戶登陸因爲時間差問題而報錯,再三百度,最後整理了一下修改linux定時同步的操做(本方法適用於有本身時間服務器,沒有的只限於借鑑)html

首先確認,咱們的服務是否已經安裝了ntp服務linux

若是沒有安裝,須要先安裝ntp服務centos

安裝完ntp以後,編輯ntp.conf  服務器

vi /etc/ntp.conf

能夠看到文件中內容less

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 127.0.0.1
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
# local clock #fudge 127.127.1.0 stratum 10 # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys

  在文件中 能夠看到修改標出來的ip地址,改成本身的時間服務器地址,也能夠是域名ide

 

修改後保存退出oop

命令行輸入this

crontab -e

  設置定時任務,其這裏設置的是命令行

* * */1 * * /usr/sbin/ntpdate 127.0.0.1 >> /home/logs/ntpd.log

 一樣修改ip地址 
而後建立日誌文件夾用來存放日誌rest

mkdir /home/logs

  ok 大功告成,過幾分鐘在日誌文件夾能夠看到日誌文件,瀏覽會發現,時間差不超過0.01s .通常來講是夠用了的

相關文章
相關標籤/搜索