歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux配置 >> NTP配置實踐

NTP配置實踐

日期:2017/2/27 14:57:20   编辑:Linux配置

前言

NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的各個計算機時間同步的一種協議。無論是平時使用的私人計算機還是在工作中搭建的服務器集群,時間的統一 性和准確性是十分重要的。本文以自己公司的NTP配置實踐過程為例,過程本身並不復雜,原理部分請參考擴展閱讀。

時間同步真的很重要

准備工作

檢查NTP是否安裝

如果缺少NTP服務,請從官方下載適合自己系統的版本

#檢查rpm包 
rpm -qa | grep ntp

ntp-4.2.4p8-1.16.1
yast2-ntp-client-2.17.14-1.10.14

手動修改時間

選擇正確的時區,控制誤差在5分鐘以內

#查看當前時間
date

2008年 12月 12日 星期五 14:44:12 CST
Fri Jun 26 14:51:37 CST 2015

#手動修改時間(月/日/年時:分:秒)
date -s "1/1/09 00:01"

2009年 01月 01日 星期四 00:01:00 CST
 
#手動修改分秒
date -s 21:21

NTP配置

NTP服務端配置

服務端時間來源選擇上海交通大學網絡中心NTP服務器地址

#NTP服務端系統版本
cat /etc/SuSE-release 

SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
#編輯ntp配置文件
vi /etc/ntp.conf

server 202.120.2.101 prefer
#server ntp.sjtu.edu.cn


#加入自動啟動
chkconfig ntp on

#啟動ntp服務
service ntp start
service ntp status
service ntp stop
完整的/etc/ntp.conf配置文件如下
################################################################################
## /etc/ntp.conf
##
## Sample NTP configuration file.
## See package 'ntp-doc' for documentation, Mini-HOWTO and FAQ.
## Copyright (c) 1998 S.u.S.E. GmbH Fuerth, Germany.
##
## Author: Michael Andres,  <[email protected]>
##         Michael Skibbe,  <[email protected]>
##
################################################################################

##
## Radio and modem clocks by convention have addresses in the 
## form 127.127.t.u, where t is the clock type and u is a unit 
## number in the range 0-3. 
##
## Most of these clocks require support in the form of a 
## serial port or special bus peripheral. The particular  
## device is normally specified by adding a soft link 
## /dev/device-u to the particular hardware device involved, 
## where u correspond to the unit number above. 
## 
## Generic DCF77 clock on serial port (Conrad DCF77)
## Address:     127.127.8.u
## Serial Port: /dev/refclock-u
##  
## (create soft link /dev/refclock-0 to the particular ttyS?)
##
# server 127.127.8.0 mode 5 prefer
server 202.120.2.101 prefer

##
## Undisciplined Local Clock. This is a fake driver intended for backup
## and when no outside source of synchronized time is available.
##
server 127.127.1.0 
# local clock (LCL)
fudge 127.127.1.0  stratum 10
# LCL is unsynchronized

##
## Add external Servers using
## # rcntp addserver <yourserver>
## 

##
## Miscellaneous stuff
##

driftfile /var/lib/ntp/drift/ntp.drift  
# path for drift file

logfile /var/log/ntp             
# alternate log file
# logconfig =syncstatus + sysevents
# logconfig =all

# statsdir /tmp/                # directory for statistics files
# filegen peerstats  file peerstats  type day enable
# filegen loopstats  file loopstats  type day enable
# filegen clockstats file clockstats type day enable

#
# Authentication stuff
#
keys /etc/ntp.keys               
# path for keys file
trustedkey 1                     
# define trusted keys
requestkey 1                     
# key (7) for accessing server variables
# controlkey 15                 # key (6) for accessing server variables

NTP客戶端配置

如無特殊要求,一般使用crontab計劃任務同步時間

1.crontab計劃任務方式配置

crontab -e

0 0 * * * /usr/sbin/sntp -P no -r  172.16.0.3;hwclock -w

2.以服務進程方式實時同步

#編輯客戶端配置文件
vi /etc/ntp.conf

server 198.16.2.135

#啟動服務
chkconfig ntp on
service ntp restart

#如果相差時間較長,請先手動同步
sntp -P no -r 198.16.2.135
ntpdate –d 198.16.2.135

檢查NTP同步狀態

NTP協議為UDP端口是123,offset時間偏移量以毫秒為單位,注意防火牆的控制策略

#查看服務連接和監聽
ntp:~ # netstat -tlunp | grep ntp  
udp        0      0 198.17.1.224:123        0.0.0.0:*                           28916/ntpd          
udp        0      0 198.16.2.224:123        0.0.0.0:*                           28916/ntpd          
udp        0      0 127.0.0.2:123           0.0.0.0:*                           28916/ntpd          
udp        0      0 127.0.0.1:123           0.0.0.0:*                           28916/ntpd          
udp        0      0 0.0.0.0:123             0.0.0.0:*                           28916/ntpd          
udp        0      0 ::1:123                 :::*                                28916/ntpd          
udp        0      0 fe80::250:56ff:feb9:123 :::*                                28916/ntpd          
udp        0      0 fe80::250:56ff:feb9:123 :::*                                28916/ntpd          
udp        0      0 :::123                  :::*                                28916/ntpd          

#查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系
ntp:~ # ntpq -p  
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*202.120.2.101   202.118.1.81     3 u    5 1024  377  265.748   -2.819  16.138
 LOCAL(0)        .LOCL.          10 l   34   64  377    0.000    0.000   0.001
Copyright © Linux教程網 All Rights Reserved