當前位置:
首頁 > 最新 > NTP時間伺服器與chrony時間伺服器部署

NTP時間伺服器與chrony時間伺服器部署

1.1 NTP簡介

NTP(Network Time Protocol,網路時間協議)是用來使網路中的各個計算機時間同步的一種協議。它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在區域網內可達0.1ms,在互聯網上絕大多數的地方其精度可以達到1-50ms。

NTP伺服器就是利用NTP協議提供時間同步服務的。

系統版本(這裡已7.2的系統為例)

1[root@test ~]#cat/etc/redhat-release

2CentOS Linux release 7.2.1511 (Core)

1.2 NTP伺服器安裝

[root@test ~]# rpm -qa ntp

ntpdate-4.2.6p5-22.el7.centos.x86_64

如果沒有安裝就yum install ntp -y進行安裝

1.3配置NTP服務

備份配置文件

[root@test ~]#cp/etc/ntp.conf{,.bak}

[root@test~]# ll /etc/ntp.conf*

-rw-r--r--.1root root1778Jan112017/etc/ntp.conf

-rw-r--r--1root root1778Oct2323:10/etc/ntp.conf.bak

精簡化配置文件

[root@test ~]#egrep-v"^$|#"/etc/ntp.conf.bak >/etc/ntp.conf

修改配置文件(紅色字體表示修改的內容)

1# Formoreinformation about thisfile, see themanpages

2# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

3

4driftfile /var/lib/ntp/drift

5

6# Permittimesynchronization with ourtimesource, butdonot

7# permit the source to query or modify the service on this system.

8#restrict default kod nomodify notrap nopeer noquery #控制nto伺服器的許可權,默認為最低,注釋掉

9restrictdefault nomodify

10

11# Permit all access over the loopback interface. This could

12# be tightened as well, but todoso would effect some of

13# the administrative functions.

14restrict127.0.0.1

15restrict -6::116

17# Hosts on local network arelessrestricted.

18#restrict192.168.1.0mask255.255.255.0nomodify notrap

19# 允許內網其他機器同步時間

20restrict172.16.1.0mask255.255.255.0nomodify notrap

21

28

29# 定義使用的上游 ntp伺服器,將原來的注釋

30server time1.aliyun.com

31server ntp1.aliyun.com

32

33#broadcast192.168.1.255autokey # broadcast server

34#broadcastclient # broadcast client

35#broadcast224.0.1.1autokey # multicast server

36#multicastclient224.0.1.1# multicast client

37#manycastserver239.255.254.254# manycast server

38#manycastclient239.255.254.254autokey # manycast client

39

40# 允許上層時間伺服器主動修改本機時間

41restrict time1.aliyun.com nomodify notrap noquery

42restrict ntp1.aliyun.com nomodify notrap noquery

43

44# 外部時間伺服器不可用時,以本地時間作為時間服務

45server127.127.1.0

46fudge127.127.1.0stratum1047

48# Enable public key cryptography.

49#crypto

50

51includefile /etc/ntp/crypto/pw

52

53# Keyfilecontaining the keys and key identifiers used when operating

54# with symmetric key cryptography.

55keys /etc/ntp/keys

56

57# Specify the key identifierswhichare trusted.

58#trustedkey484259

60# Specify the key identifier to use with the ntpdc utility.

61#requestkey862

63# Specify the key identifier to use with the ntpq utility.

64#controlkey865

66# Enable writing of statistics records.

67#statistics clockstats cryptostats loopstats peerstats

1.4啟動NTP伺服器

注意:如果有同步時間的定時任務要將其註銷,否則會衝突

[root@test ~]# systemctl start ntpd

顯示節點列表

[root@test ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

time5.aliyun.co10.137.38.862u286435.0926241215185.623

132.163.96.4.INIT.16u -640.0000.0000.000

[root@m01 ~]# ntpstat

unsynchronised #出現一下情況,需要等待一定時間即可同步

time server re-starting

polling server every 8 s

1.5客戶機時間同步

客戶端推薦使用ntpdate同步時間,如開啟ntp服務不安全

CentOS7默認沒有這個命令,需要安裝:

yum install ntpdate -y

同步內網時間伺服器:

ntpdate 172.16.1.61

說明:客戶機要等幾分鐘再與新啟動的ntp伺服器進行時間同步,否則會提示『』no server suitable for synchronization found錯誤『』

配置chrony時間伺服器:

1.停止ntpd服務,避免佔用123埠

systemctl stop ntpd.service

2.安裝chrony服務

yum -y install chrony

3.備份配置文件

cp /etc/chrony.conf{,.ori}

4.編輯配置文件

vim /etc/chrony.conf

#注釋3-6行,在下面加入新的上游伺服器

serverntp2.aliyun.com

#注釋25-26行,在下面加入允許同步的網段

# Allow NTP client access from local network.

#allow 192.168.0.0/16

allow 172.16.1.0/24

5.對比檢查一下配置文件

diff /etc/chrony.conf{,.ori}

6.啟動chrony服務

systemctl start chronyd


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 linux運維菜鳥之旅 的精彩文章:

HTTPS安全證書訪問連接知識講解

TAG:linux運維菜鳥之旅 |