在Linux系統(tǒng)中,為了避免主機(jī)時間因?yàn)樵陂L時間運(yùn)行下所導(dǎo)致的時間偏差,進(jìn)行時間同步(synchronize)的工作是非常必要的。Linux系統(tǒng)下,一般使用ntp服務(wù)來同步不同機(jī)器的時間。NTP 是網(wǎng)絡(luò)時間協(xié)議(Network Time PRotocol)的簡稱,干嘛用的呢?就是通過網(wǎng)絡(luò)協(xié)議使計算機(jī)之間的時間同步化。
安裝NTP包
檢查是否安裝了ntp相關(guān)包。如果沒有安裝ntp相關(guān)包,使用rpm或yum安裝,安裝也非常簡單方便。
[root@localhost ~]# rpm -qa | grep ntp
ntpdate-4.2.6p5-1.el6.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
ntp-4.2.6p5-1.el6.x86_64
NTP的配置
A: 配置/etc/ntp.conf
NTP Server的主要配置文件為/etc/ntp.conf ,沒有修改過的ntp.conf文件內(nèi)容如下所示,配置選項都有相關(guān)注釋信息(Linux 版本為Red Hat Enterprise Linux Server release 6.6 )
[root@localhost ~]# more /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
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).
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
#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
# 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
各個選項信息:
#系統(tǒng)時間與BIOS事件的偏差記錄
driftfile /etc/ntp/drift
restrict 控制相關(guān)權(quán)限。
語法為: restrict ip地址 mask 子網(wǎng)掩碼 參數(shù)
其中IP地址也可以是default ,default 就是指所有的IP
參數(shù)有以下幾個:
ignore :關(guān)閉所有的 NTP 聯(lián)機(jī)服務(wù)
nomodify:客戶端不能更改服務(wù)端的時間參數(shù),但是客戶端可以通過服務(wù)端進(jìn)行網(wǎng)絡(luò)校時。
notrust :客戶端除非通過認(rèn)證,否則該客戶端來源將被視為不信任子網(wǎng)
noquery :不提供客戶端的時間查詢:用戶端不能使用ntpq,ntpc等命令來查詢ntp服務(wù)器
notrap :不提供trap遠(yuǎn)端登陸:拒絕為匹配的主機(jī)提供模式 6 控制消息陷阱服務(wù)。陷阱服務(wù)是 ntpdq 控制消息協(xié)議的子系統(tǒng),用于遠(yuǎn)程事件日志記錄程序。
nopeer :用于阻止主機(jī)嘗試與服務(wù)器對等,并允許欺詐性服務(wù)器控制時鐘
kod : 訪問違規(guī)時發(fā)送 KoD 包。
restrict -6 表示IPV6地址的權(quán)限設(shè)置。
1:設(shè)定NTP主機(jī)來源(其中prefer表示優(yōu)先主機(jī)),192.168.7.49是本地的NTP服務(wù)器,所以優(yōu)先指定從該主機(jī)同步時間。
server 192.168.7.49 prefer
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
2:限制你允許的這些服務(wù)器的訪問類型,在這個例子中的服務(wù)器是不容許修改運(yùn)行時配置或查詢您的Linux NTP服務(wù)器
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap
在上例中,掩碼地址擴(kuò)展為255,因此從192.168.0.1-192.168.0.254的服務(wù)器都可以使用我們的NTP服務(wù)器來同步時間
#此時表示限制向從192.168.0.1-192.168.0.254這些IP段的服務(wù)器提供NTP服務(wù)。
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap noquery
#設(shè)置默認(rèn)策略為允許任何主機(jī)進(jìn)行時間同步
restrict default ignore
新聞熱點(diǎn)
疑難解答
圖片精選