一、很多分布式軟件需要局域網中各節點時間同步,最粗暴的辦法是在所有節點上都運行ntpdate -u 互聯網服務器地址,這樣人工讓所有節點與外網服務器同步。如果節點重啟,需要在該節點上再次運行該命令。
二、此處要在局域網內搭建一個時間服務器,該服務器與外網授時中心同步時間,局域網其他機器與該服務器保持時間同步。
1.局域網各機器安裝ntp:yum install ntp
2.各機器啟動ntp服務:service ntpd start
3.各機器ntp服務自動啟動:chkconfig ntpd on
4.時間服務器配置。假定局域網所有機器都是120網段,本時間服務器的ip為192.168.120.201,機器名為node01。修改/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 #添加此處。允許內網其他機器同步。 restrict 192.168.120.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 iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst #添加此處 server 授時中心ip或域名 perfer #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
#添加如下一行。允許上層時間服務器主動修改本機時間 restrict 授時中心ip或域名 nomodify notrap noquery #添加如下兩行。外部時間服務器不可用時,以本地時間作為時間服務 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
5.局域網客戶端配置。客戶端機器名為node02、node03等。
# 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.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst #添加以下一行 server 192.168.120.201 #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 #添加如下一行。允許上層時間服務器主動修改本機時間 restrict 192.168.120.201 nomodify notrap noquery # 外部時間服務器不可用時,以本地時間作為時間服務 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
6.配置完成后,有幾個命令需要使用,來檢查配置是否成功,可在時間服務器和客戶端上都運行這些命令來進行檢查。
- service ntpd status(start|stop|restart),查詢本地ntpd服務開啟狀態。修改配置文件后,需運行restart命令重啟服務,要始終保持該服務運行。
- netstat -tlunp | grep ntp 查看相關服務網絡端口開啟情況,該命令判斷本地是否開啟了相關端口用於時間同步,默認本地開放123端口。
[root@node01 etc]# netstat -tlunp | grep ntp
udp 0 0 192.168.120.201:123 0.0.0.0:* 1381/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 1381/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 1381/ntpd
udp 0 0 fe80::20c:29ff:feea:119b:123 :::* 1381/ntpd
udp 0 0 ::1:123 :::* 1381/ntpd
udp 0 0 :::123 :::* 1381/ntpd
- ntpq -p 判斷ntp.conf文件中的互聯網服務器設置是否生效,注意觀察remote列值是否和配置相符。時間服務器地址在此處顯示時可能是倒着寫的,如配置的是223.65.211.42,顯示的可能是42.211.65.223。服務器和客戶端顯示分別是:
[root@node01 etc]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *42.211.65.223.s 10.1.0.0 3 u 61 64 177 35.315 -15.290 9.980 LOCAL(0) .LOCL. 10 l 136 64 374 0.000 0.000 0.000
[root@node02 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *node01 223.65.211.42 4 u 16 128 377 0.167 48.143 30.332 LOCAL(0) .LOCL. 10 l 916 64 0 0.000 0.000 0.000
remote為本機設定的時間服務器地址,refid為本機設定的時間服務器的上層服務器地址。
- ntpstat 命令查看時間同步狀態,用該命令判斷是否最近與設定的時間服務器進行了同步。如,本配置中服務器和客戶端的顯示分別是:
[root@node01 etc]# ntpstat synchronised to NTP server (223.65.211.42) at stratum 4 time correct to within 114 ms polling server every 256 s
[root@node02 ~]# ntpstat synchronised to NTP server (192.168.120.201) at stratum 5 time correct to within 169 ms polling server every 256 s
7.特別注意,本地與互聯網的時間同步在所有設置均完成后,需要等待5-10分鍾才能正式生效,此時才能使用上述命令進行查詢檢測配置是否成功。
8.確保在授時中心有效性。