1. 程式人生 > >NTP服務器搭建和客戶端配置

NTP服務器搭建和客戶端配置

position 服務器 relative middle border

1 搭建NTP服務器

準備搭建環境
主機IPOS備註
NTP Server192.168.5.180CentOS 6
NTP Client192.168.5.181CentOS 6

1.1 安裝NTP服務程序

  1. [[email protected] ~]# rpm -qa | grep ntp #查詢是否安裝ntp服務程序
  2. [[email protected] ~]# yum install -y ntp #安裝ntp服務程序

1.2 配置NTP服務程序

紅色為添加的配置,其它都是默認配置
  1. [[email protected] ~]# cp /etc/ntp.conf{,.
    bak} #備份ntp配置文件
  2. [[email protected] ~]# vim /etc/ntp.conf #編輯ntp配置文件
  3. [[email protected] ~]# grep -Ev ‘^$|^#‘ /etc/ntp.conf
  4. driftfile /var/lib/ntp/drift
  5. restrict default kod nomodify notrap nopeer noquery
  6. restrict -6 default kod nomodify notrap nopeer noquery
  7. restrict 127.0.0.1
  8. restrict -6 ::1
  9. restrict
    192.168.5.0 mask 255.255.255.0 nomodify notrap #允許該網段內的主機可以進行校時
  10. restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap
  11. server time.nist.gov prefer #上層NTP服務器地址,prefer表示優先
  12. server 0.centos.pool.ntp.org iburst
  13. server 1.centos.pool.ntp.org iburst
  14. server 2.centos.pool.ntp.org iburst
  15. server 3.centos.pool.ntp.org iburst
  16. includefile
    /etc/ntp/crypto/pw
  17. keys /etc/ntp/keys

1.3 啟動NTP服務程序

# chkconfig ntpd on# /etc/init.d/ntpd start
# /etc/init.d/ntpd stop# /etc/init.d/ntpd restart
查看NTP啟動的端口
  1. [[email protected] ~]# netstat -tlunp | grep ntp
  2. udp 0 0 192.168.5.180:123 0.0.0.0:* 28331/ntpd
  3. udp 0 0 127.0.0.1:123 0.0.0.0:* 28331/ntpd
  4. udp 0 0 0.0.0.0:123 0.0.0.0:* 28331/ntpd
  5. udp 0 0 fe80::20c:29ff:fe2b:9990:123 :::* 28331/ntpd
  6. udp 0 0 ::1:123 :::* 28331/ntpd
  7. udp 0 0 :::123 :::* 28331/ntpd

1.4 查看NTP服務狀態

現在ntp服務器已經啟動了,不過與上層服務器連接則還需要一些時間,通常啟動NTP後在15分鐘內才會和上層NTP服務器順利連接上。如何確定NTP服務器順利地更新了自己的時間呢?可以使用以下幾個命令查看
  1. [[email protected] ~]# ntpstat
  2. synchronised to NTP server (94.237.64.20) at stratum 3
  3. time correct to within 362 ms
  4. polling server every 64 s
這個命令列出NTP服務器是否已經與上層NTP服務器連接,由上述的輸出結果可以知道,時間已經校正約362ms,且每隔64秒會主動去更新
  1. [[email protected] ~]# ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. time-a.nist.gov .INIT. 16 u - 128 0 0.000 0.000 0.000
  5. *makaki.miuku.ne 218.186.3.36 2 u 16 64 377 269.215 44.744 69.262
  6. +ntp1.ams1.nl.le 130.133.1.10 2 u 21 64 377 271.981 -32.490 66.809
  7. +ntp.wdc1.us.lea 130.133.1.10 2 u 22 64 337 308.368 -6.923 90.300
  8. 61-216--104. 62.161.56.158 3 u 38 64 21 80.450 -33.733 23.397
註意:,
ntpq -p命令可以列出當前我們的NTP服務器與上層NTP服務器的連接狀態,以上幾個字段的意義如下:
remote: NTP主機的IP或主機名;註意左邊的符號:
*:表示正在使用的NTP服務器
+:表示已經連接成功,但是作為備用提供時間更新的NTP服務器;
refid: 上層NTP服務器地址;
st: 就是stratum階層
when:幾秒鐘前做過時間同步更新操作;pool:下次更新在幾秒鐘之後;reach:已經向上層NTP服務器要求更新的次數;delay: 網絡傳輸過程中延遲的時間,單位為10-6offset:時間補償結果,單位為10-3秒;jitter:系統時間與BIOS硬件時間的差異時間,單位為10-6秒;

2 NTP客戶端配置

Client:192.168.5.181
[[email protected] tools]# ntpdate 192.168.5.18015 Jun 14:36:19 ntpdate[24881]: adjust time server 192.168.5.180 offset -0.002062 sec[[email protected] tools]# hwclock -w #將系統時間寫入BIOS硬件時間[[email protected] tools]# hwclock Thu 15 Jun 2017 03:46:26 PM CST -0.860434 seconds[[email protected] tools]# dateThu Jun 15 15:46:38 CST 2017[[email protected] tools]# crontab -e #配置計劃任務,系統第隔3分鐘自動與NTP服務器192.168.5.180進行校時
*/3 * * * * (/usr/sbin/ntpdate 192.168.5.180 && /sbin/hwclock -w) &> /dev/null

NTP服務器搭建和客戶端配置