linux設定時間伺服器

來源:互聯網
上載者:User

周海漢
/文

2010.3.30

 

對多個linux伺服器,時間保持一致是很必要的。根據精確度要求,應該有相應的時間間隔進行時間同步。如果不進行時間同步,時間久了就會差別很大,遇到問題時定位就很困難。因為多台裝置的配合,log之間可能有前因後果,時間是同步事件的先後的重要依據。

 

一般來說,對一個機房內的裝置,可以設定一台時間伺服器,由它定期從一個標準的時間伺服器上擷取時間。其他的伺服器可以通過內網的串連從這台伺服器進行同步。這樣不僅時間會一致,而且照顧到一些沒有公網的裝置。

 

本文測試系統:

[root@test ~]# cat /etc/*release
CentOS release 5.2 (Final)

 

[root@test ~]# rpm -qf /usr/sbin/ntpd
ntp-4.2.2p1-8.el5.centos.1

在安裝時應該確定ntp包已經安裝。

 

啟動伺服器

如果ntpd已經安裝,則可以直接啟動:

[root@test ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

 

同時,也需要檢查一下設定檔,centos預設都配置好了。

[root@test ~]# vi /etc/ntp.conf

server 0.centos.pool.ntp.org

driftfile /var/lib/ntp/drift

keys /etc/ntp/keys

 

檢查一下時間伺服器是否可用:

[root@test ~]# ping 0.centos.pool.ntp.org
PING 0.centos.pool.ntp.org (74.88.39.232) 56(84) bytes of data.
64 bytes from ool-4a5827e8.dyn.optonline.net (74.88.39.232): icmp_seq=1 ttl=54 time=251 ms
如果不可用,則確定一下網路是否能串連到外網。檢查一下網域名稱解析是否配置。

[root@test ~]# cat /etc/resolv.conf
nameserver 8.8.8.8

 

設定ntpd自啟動

[root@test ~]# find  /etc/rc.d/ -name "*ntpd"
/etc/rc.d/rc6.d/K74ntpd
/etc/rc.d/init.d/ntpd
/etc/rc.d/rc3.d/K74ntpd
/etc/rc.d/rc4.d/K74ntpd
/etc/rc.d/rc5.d/K74ntpd
/etc/rc.d/rc2.d/K74ntpd
/etc/rc.d/rc1.d/K74ntpd
/etc/rc.d/rc0.d/K74ntpd
[root@test ~]# /sbin/chkconfig --level 345 ntpd on
[root@test ~]# !find
find  /etc/rc.d/ -name "*ntpd"
/etc/rc.d/rc6.d/K74ntpd
/etc/rc.d/init.d/ntpd
/etc/rc.d/rc3.d/S58ntpd
/etc/rc.d/rc4.d/S58ntpd
/etc/rc.d/rc5.d/S58ntpd
/etc/rc.d/rc2.d/K74ntpd
/etc/rc.d/rc1.d/K74ntpd
/etc/rc.d/rc0.d/K74ntpd
說明在3,4,5三個層級已經可以自啟動。

 

檢查防火牆

[root@test ~]# iptables -L

對比較嚴格的防火牆,應該對ntp連接埠123進行配置:

[root@test ~]# iptables -A INPUT -p udp --dport 123 -j ACCEPT
[root@test ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ntp

 

用戶端配置

用戶端採用ntpdate來更新,配置在crontab中。根據需要決定頻率。在每一台需要同步時間的裝置上設定crontab

[root@test1 ~]# crontab -e

 

00 00 * * * /usr/sbin/ntpdate 192.168.12.31

 

192.168.12.31是test伺服器的內網地址。

crontab 設定的是每天0點同步時間。

為了保證時間伺服器可用,將命令先在命令列下執行一下。

[root@test1 ~]# ntpdate 192.168.12.31
30 Mar 17:45:24 ntpdate[16495]: step time server 192.168.12.31 offset 0.694312 sec
[root@test1 ~]# date
Tue Mar 30 17:45:37 CST 2010

說明同步時間成功。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.