1. NTP簡介
NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的各個計算機時間同步的一種協議。它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在局域網內可達0.1ms,在互聯網上絕大多數的地方其精度可以達到1-50ms。
NTP服務器就是利用NTP協議提供時間同步服務的。
2. NTP服務器安裝
# 系統自帶ntp [root@m01 ~]# rpm -qa ntp ntp-4.2.6p5-5.el6.centos.x86_64 # 如果沒有就安裝yum -y install ntp
3. 配置NTP服務
[root@m01 ~]# vim /etc/ntp.conf # restrict default kod nomodify notrap nopeer noqueryrestrict default nomodify restrict 172.16.1.0 mask 255.255.255.0 nomodify 《==增加這一行 # nomodify客戶端可以同步,但不可以修改時間服務器 # 將默認時間同步源註釋改用可用源 #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 server 202.118.1.81 prefer server 24.56.178.140 server time.nist.gov
4. 啓動NTP服務器
# 如果計劃任務有時間同步,先註釋,兩種用法會衝突。 [root@m01 ~]# crontab -e # time sync by tank at 2015-12-9 #*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1 [root@m01 ~]# /etc/init.d/ntpd start Starting ntpd: [ OK ] [root@m01 ~]# date -s "2015/11/25 10:30" Wed Nov 25 10:30:00 CST 2015 [root@m01 ~]# /etc/init.d/ntpd restart Shutting down ntpd: [ OK ] Starting ntpd: [ OK ] [root@m01 ~]# date Wed Dec 9 02:28:45 CST 2015 [root@m01 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *news.neu.edu.cn 202.118.1.47 2 u 12 64 1 50.238 5.239 0.000 host-24-56-178- .STEP. 16 u - 64 0 0.000 0.000 0.000 utcnist2.colora .ACTS. 1 u 11 64 1 240.119 -1.330 0.000 [root@m01 ~]# ntpstat unsynchronised polling server every 64 s [root@m01 ~]# ntpdate 10.0.0.61 9 Dec 02:31:38 ntpdate[3825]: the NTP socket is in use, exiting
5. 客戶機時間同步
客戶機要等幾分鐘再與新啓動的ntp服務器進行時間同步,否則會提示no server suitable for synchronization found
錯誤。
[root@web02 ~]# /usr/sbin/ntpdate172.16.1.61 4 Dec 11:51:00 ntpdate[1985]: step timeserver 172.16.1.61 offset 65928627.790465 sec # 將命令放入計劃任務即可。 echo"*/2 * * * * /usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1">>/var/spool/cron/root