【實驗Linux版本:Red Hat Enterprise Linux Server release 6.4】
1、 安裝ntp
yum -y install ntp
2、 修改ntp.conf的配置
1)vim /etc/ntp.conf
① 配置方法一:只允許100.100.100.0網段的客戶機進行時間同步
#在restrict default kod nomodify notrap nopeer noquery(表示默認拒絕所有IP的時間同步)之後增加一行:
restrict 100.100.100.0 mask 255.255.255.0 nomodify notrap
② 配置方法二:允許任何ip的客戶機都可以進行時間同步
#將restrict default kod nomodify notrap nopeer noquery修改爲如下行:
restrict default nomodify
- 權限的設定主要以restrict這個參數來設定,主要的語法爲:
restrict IP地址 mask 子網掩碼 參數
其中IP可以是IP地址也可以是default,default是指所有的IP
- 參數主要有以下幾個:
ignore :關閉所有的 NTP 聯機服務
nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。
notrust:客戶端除非通過認證,否則該客戶端來源將被視爲不信任子網
noquery:不提供客戶端的時間查詢
2)用server這個參數設定上級時間服務器,語法爲:
server IP地址或域名 [prefer]
- IP地址或域名就是我們指定的上級時間服務器,如果 Server 參數最後加上 prefer,表示我們的 NTP 服務器主要以該部主機時間進行校準。
[參考/etc/ntp.conf文件]
# 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
# 允許任何ip的客戶機都可以進行時間同步
restrict default nomodify
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).
#iserver 0.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org
# IP地址或域名就是我們指定的上級時間服務器,如果 Server 參數最後加上 prefer,表示我們的 NTP 服務器主要以該部主機時間進行校準
server 100.100.100.27 prefer
#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.
server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10
#/etc/init.d/ntpd start #啓動時間服務器
#service ntpd status
#service ntpd start
# 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
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
-------以下內容可以不做------------
3)、解決NTP服務器校準時間時的傳送延遲
使用driftfile參數設置:
driftfile 文件名
在與上級時間服務器聯繫時所花費的時間,記錄在driftfile參數後面的文件內。
注意: driftfile 後面接的文件需要使用完整的路徑文件名,不能是鏈接文件,並且文件的權限需要設定成 ntpd守護進程可以寫入。
/usr/share/zoneinfo 這個目錄下存放的是各個時區對應的設定文件
/etc/localtime 本地系統時間設定文件
/etc/sysconfig/clock 本機時區設定文件
爲了使NTP服務可以在系統引導的時候自動啓動,執行:
chkconfig ntpd on
將同步好的時間寫到CMOS裏邊
vim /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
切記每次修改配置文件後重新啓動服務使配置生效,可使用如下命令來檢查NTP服務是否啓動,啓動的話可以得到一個進程ID號;
pgrep ntpd
使用下面的命令檢查時間服務器同步的狀態
ntpq -p
用ntpstat也可以查看一些同步狀態信息,
安裝完畢客戶端需要過5-10分鐘才能從服務器端更新時間
客戶端設置:
crontab -e
加入一行
*/15 * * * * /usr/sbin/ntpdate 100.100.100.27(服務器端IP)