1. 安裝軟體包:
Yum install ntp
Ntp的配置檔案是/etc/ntp.conf,工作目錄是/etc/ntp/,接着編輯/etc/ntp.conf檔案。
2. 服務端配置:
IP:192.168.1.110
編輯/etc/ntp.conf檔案:
[root@rhel5 ~]# cat /etc/ntp.conf
# 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
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap(允許内網通訊)
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap noquery notrust(禁止其他ip來更新時間)
# Use public servers from the pool.ntp.org project.
server 0.pool.ntp.org(redhat預設情況下是server 0.redhat.pool.ntp.org)
server 1.pool.ntp.org(redhat預設情況下是server 1.redhat.pool.ntp.org)
server 2.pool.ntp.org(redhat預設情況下是server 2.redhat.pool.ntp.org)
#broadcast 192.168.1.255 key 42 # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 key 42 # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 key 42 # 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
fudge 127.127.1.0 stratum 10
# Drift file. Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift (driftfile 選項,則指定了用來儲存系統時鐘頻率偏差的檔案, ntpd程式使用它來自動地補償時鐘的自然漂移, 進而使時鐘即使在切斷了外來時源的情況下,仍能保持相當的準确度)
# 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
Logfile /var/log/ntp.log(定義日志資訊)
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
restrict 0. pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
restrict 1. pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
如果restric 後面不帶參數,表示可以允許全部權限.以上三個影響/etc/ntp/ step-tickers這個檔案,它會随着/etc/ntp.conf檔案的變化而變化。
/etc/sysconfig/clock是用來設定時區的檔案,例如:
[root@rhel5 ntp]# cat /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE="Asia/Chongqing"
UTC=true
ARC=false
/usr/share/zoneinfo這個目錄裡規定了各主要時區的時間設定檔案,例如重慶:/usr/share/zoneinfo/Asia/Chongqing
如果想把同步好的時間寫入BIOS裡,就要設定/etc/sysconfig/ntp檔案,将其中的SYNC_HWCLOCK=no更改為SYNC_HWCLOCK=yes。
啟動ntp服務:
[root@rhel5 ntp]# service ntpd restart
Shutting down ntpd: [ OK ]
ntpd: Synchronizing with time server: [ OK ]
Starting ntpd: [ OK ]
[root@rhel5 ntp]#chkconfig ntpd on
3. 用戶端的設定:
可以通過使用system-config-time來設定,将其中的時間伺服器更改為192.168.1.110即可,或者使用ntpdate -u 192.168.1.110來和時間伺服器同步。切記更改完以後要重新啟動NTP服務,使其生效。或者使用crontab -e來編輯:
*/5 * * * * /usr/sbin/ntpdate -u 192.168.1.110 表示每五分鐘同步一次。重新開機crond服務即可。
可以使用ntpq –p檢視同步狀态