[root@Centosszm ~]# date +%s
1364796931 #軟件時鐘,從1970/01/01開始計算總秒數
#硬件時鐘,BIOS上記錄(硬件)
時間同步協議:Network Time Protocol、Digital Time Synchronization Protocol(DTSS)
我國授時中心服務器的IP地址爲:210.72.145.44
NTP是分層設計,最多15層
所需要的相關軟件:
1.ntp:NTP服務主要軟件
2.tzdata:Time Zone Data,提供各時區對應的顯示格式
相關配置文件與數據庫
1./etc/ntp.conf
2./usr/share/zoneinfo/:由tzdata提供,爲各時區的時間格式對應文件
3./etc/sysconfig/clock:時區設置文件
4./etc/localtime:複製上面的時區文件到這裏就是本地時間配置文件了
相關命令:
1./bin/date
2./sbin/hwclock:硬件時間的修改,date修改軟件時間
3./usr/sbin/ntpd:
4./usr/sbin/ntpdate:時間校對
客戶端修改時區文件:
[root@Centosszm ~]# date
Mon Apr 1 14:37:00 CST 2013
[root@Centosszm ~]# vim /etc/sysconfig/clock
ZONE="America/New_York"
[root@Centosszm ~]# cp /usr/share/zoneinfo/America/New_York /etc/localtime
[root@Centosszm ~]# date
Mon Apr 1 02:39:05 EDT 2013
1.利用restrict來管理權限控制
restrick [ip] mask [netmask_IP] [parameter] #沒有寫parameter爲允許所有
ignore:
nomodify:禁止使用ntpc,ntpq修改服務器時間參數
noquery:禁止使用ntpc,ntpq查詢服務器時間參數,相當於不提供NTP服務
notrap:不提供trap這個遠程事件登錄功能
notrust:拒絕沒有認證的客戶端
可以禁止所有再逐漸添加信任網絡;
2.利用Server設置上層NTP服務器
server [ip|hostname] [prefer] #設置優先級
3.利用driftfile記錄時間差異
driftfile [完整路徑文件名]
文件不能是連接文件
ntpd對這個文件可寫
數值單位爲:ppm(百萬分之一秒)
使用ntpd的owner是ntp
[root@Centosszm ~]# cat /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
4.客戶端需認證
keys [key_file]
ntp-kengen
[root@Centosszm ~]# cat /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
restrict 192.168.179.0 mask 255.255.255.0 nomodify
# 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.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
server 0.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
server 3.rhel.pool.ntp.org
#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
# 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
[root@Centosszm ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]
[root@Centosszm ~]# tail /var/log/messages
[root@Centosszm ~]# netstat -tlunp | grep ntp
udp 0 0 192.168.111.100:123 0.0.0.0:* 3522/ntpd
udp 0 0 172.16.128.211:123 0.0.0.0:* 3522/ntpd
udp 0 0 192.168.179.7:123 0.0.0.0:* 3522/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 3522/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 3522/ntpd
udp 0 0 fe80::20c:29ff:fe24:123 :::* 3522/ntpd
udp 0 0 fe80::20c:29ff:fe24:123 :::* 3522/ntpd
udp 0 0 ::1:123 :::* 3522/ntpd
udp 0 0 :::123 :::* 3522/ntpd
[root@Centosszm ~]# ntpstat
synchronised to NTP server (218.75.4.130) at stratum 3
time correct to within 547 ms
polling server every 64 s
#通常NTP後約15分鐘內纔會和上層NTP服務器順利連接上。等這個結果等了我好久啊....TT
[root@Centosszm ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*Hshh.org 66.220.9.122 2 u 62 64 67 26.741 94.969 6.197
+dns1.synet.edu. 202.118.1.46 2 u 1 64 127 176.216 125.383 142.809
#*使用中上層 上層NTP地址
#+連接成功,候選
st:stratum階層
when:上次更新時間
poll:下次更新時間
reach:已經更新次數
delay:網絡延時時間
offset:時間補償
jitter:Linux與BIOS差異時間,單位爲10的-6次方秒;
注意的地方:
1.NTP服務器要和上層NTP連接,否則無法提供服務;
2.防火牆UDP123端口必須打開,否則也無法提供服務;
安全性設置:
[root@Centosszm ~]# iptables -A INPUT -i eth0 -p udp --dport 123 -s 192.168.179.0/24 -j ACCEPT
[root@Centosszm ~]# /etc/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
客戶端相關設置:
[root@szm ~]# date
Mon Apr 1 15:58:38 CST 2013
[root@szm ~]# date 040116002013 #設置成4月1日的16:00 2013年
Mon Apr 1 16:00:00 CST 2013
[root@szm ~]# hwclock #因爲時區爲東八,差8個小時
Mon 01 Apr 2013 04:00:26 PM CST -0.901812 seconds
[root@szm ~]# hwclock -w;hwclock -r;date
Mon 01 Apr 2013 04:02:32 PM CST -0.047943 seconds #w爲寫,r爲讀
Mon Apr 1 16:02:31 CST 2013
[root@Centosszm ~]# ntpdate
1 Apr 16:06:41 ntpdate[4193]: no servers can be used, exiting
注意:NTP服務器是不可以使用nptdate的,也就是ntpdate與ntpd不能同時啓用;
客戶端校對時間:
[root@szm ~]# ntpdate 192.168.179.7
1 Apr 16:07:52 ntpdate[8695]: step time server 192.168.179.7 offset 17.691761 sec
[root@szm ~]# hwclock -w;date
Mon Apr 1 16:08:46 CST 2013
客戶端自動更新時間到硬件時間;
[root@szm ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
10 5 * * * root (/usr/sbin/ntpdate 192.168.179.7 && /sbin/hwclock -w) &>/etc/null