原理
NTP(Network TimeProtocol,網絡時間協議)是用來使計算機時間同步的一種協議。它可以使計算機對其服務器或時鍾源做同步化,它可以提供高精准度的時間校正(LAN上與標准間差小於1毫秒,WAN上幾十毫秒),切可介由加密確認的方式來防止惡意的協議攻擊。
運行模式:C/S模式
運行端口/協議:123/udp
安裝
直接使用 yum
安裝:
yum install ntp ntpdate
配置
配置文件 /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 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 192.168.137.1 mask 255.255.255.0 nomodify #允許更新的IP地址段 restrict ::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). # perfer:表示優先級最高,如果不設置,從上到下,寫的越靠上,優先級越高 # iburst:當一個運程NTP服務器不可用時,向它發送一系列的並發包進行檢測 server cn.pool.ntp.org iburst perfer # 中國國家授時中心ntp服務器 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 # local clock 如果上面的服務器都無法同步時間,就和本地系統時間同步。127.127.1.0在這里是一個IP地址,不是網段。 # 127.127.1.0 為第10層。ntp 和127.127.1.0同步完后,就變成了11層。 ntp是層次階級的。同步上層服務器的stratum 大小不能超過或等於16。 # 在實際搭建中如果設置了local clock會出現即使ntp服務器本身時間不准確也不會和上層ntp服務器同步的問題,故注釋。 # 具體原因待研究 #server 127.127.1.0 #fudge 127.127.1.0 stratum 8 #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 # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
啟動
centos6
啟動:service ntpd start
停止:service ntpd stop
設置開機啟動:chkconfig ntpd on
查看啟動狀態:service ntpd status
查看日志: tailf /var/log/message
查看端口:netstat -ulnp | grep -w 123
centos7
啟動:systemctl start ntpd
停止:systemctl stop ntpd
設置開機啟動:systemctl enable ntpd
查看啟動狀態:systemctl status ntpd
查看日志: tailf /var/log/message
查看端口:netstat -ulnp | grep -w 123
注意: ntpd
有一個自我保護設置:如果本機與上源時間相差太大,ntpd
不運行。所以新設置的時間服務器一定要先ntpdate
從上源取得時間初值,然后再啟動 ntpd
服務。
查看同步狀態
使用命令 ntpq -p
查看同步狀態,ntpstat
列出是否與上游服務器連接。
[root@centos751 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp7.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000 *106.39.20.237 108.196.98.101 2 u 202 64 10 48.325 -4.794 0.997 ntp1.ams1.nl.le 130.133.1.10 2 u 82 64 2 440.047 -111.75 0.000 ntp2.flashdance 192.36.143.130 2 u 7 64 7 268.049 -2.210 45.428 static-5-103-13 .INIT. 16 u - 64 0 0.000 0.000 0.000 [root@centos751 ~]#
- remote:即remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先。
- refid:參考的上一層NTP主機的地址
- st:即stratum階層
- poll:下次更新在幾秒之后
- offset:時間補償的結果
[root@centos751 ~]# ntpstat
synchronised to NTP server (106.39.20.237) at stratum 3 # 可以看到我們當前在3層 time correct to within 350 ms polling server every 64 s [root@centos751 ~]#
客戶端同步時間
使用 ntpdate
同步
[root@centos752 ~]# ntpdate 192.168.223.101 22 Oct 17:19:08 ntpdate[29924]: adjust time server 192.168.223.101 offset -0.001306 sec
這種方法同步一次后如果時間久了,可能時間又會不准確,所以一般是使用 cron
任務定時同步
*/10 * * * * /usr/sbin/ntpdate 192.168.223.101 | logger -t NTP
拓展補充
1、linux系統時間和BIOS時間
有時候linux系統時間和BIOS時間是不一樣的,如果不一樣怎么解決呢?方法如下:
a、首先 hwclock -r
讀出BIOS的時間參數,查看是否准確
b、如果不准確,使用 hwclock -w
將當前系統時間(已同步准確)寫入BIOS中
2、不同機器之間的時間同步
為了避免主機時間因為長期運作下所導致的時間偏差,進行時間同步的工作是非常必要的。 同步時間,可以使用ntpdate命令,也可以使用ntp服務。主要的方法有以下3種。
a、使用 ntpdate
配合 cron
同步公網NTP服務器時間
*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org | logger -t NTP
b、將主機搭建成 ntp
服務器同步上層NTP服務器時間
以上兩種方法有弊端:必須主機可以連接公網,但是有的時候集群當中有部分主機是不能連接公網的,這時候就要用到第三種的方法
c、將內網中能連接公網的主機搭建成 ntp
服務器同步公網上層NTP服務器時間,然后內網中其他主機使用 ntpdate
定時同步此NTP服務器時間或者搭建成 ntp
服務器並將上層NTP服務器設置為次NTP服務器