1.1 NTP简介

image.png

spacer.gifNTPNetwork Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms

NTP服务器就是利用NTP协议提供时间同步服务的。

系统版本(这里已7.2的系统为例)

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

2 CentOS 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--. 1 root root 1778 Jan 11 2017 /etc/ntp.conf

-rw-r--r-- 1 root root 1778 Oct 23 23:10 /etc/ntp.conf.bak

精简化配置文件

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

修改配置文件(红色字体表示修改的内容)

1 # For more information about this file, see the man pages

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

3

4 driftfile /var/lib/ntp/drift

5

6 # Permit time synchronization with our time source, but do not

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

8 #restrict default kod nomodify notrap nopeer noquery    #控制nto服务器的权限,默认为最低,注释掉

9 restrict default nomodify

10

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

12 # be tightened as well, but to do so would effect some of

13 # the administrative functions.

14 restrict 127.0.0.1

15 restrict -6 ::116

17 # Hosts on local network are less restricted.

18 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

19 # 允许内网其他机器同步时间

20 restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap

21

22 # Use public servers from the pool.ntp.org project.

23 # Please consider joining the pool (http://www.pool.ntp.org/join.html).24 #server 0.centos.pool.ntp.org iburst

25 #server 1.centos.pool.ntp.org iburst

26 #server 2.centos.pool.ntp.org iburst

27 #server 3.centos.pool.ntp.org iburst

28

29 # 定义使用的上游 ntp服务器,将原来的注释

30 server time1.aliyun.com

31 server ntp1.aliyun.com

32

33 #broadcast 192.168.1.255 autokey # broadcast server

34 #broadcastclient # broadcast client

35 #broadcast 224.0.1.1 autokey # multicast server

36 #multicastclient 224.0.1.1 # multicast client

37 #manycastserver 239.255.254.254 # manycast server

38 #manycastclient 239.255.254.254 autokey # manycast client

39

40 # 允许上层时间服务器主动修改本机时间

41 restrict time1.aliyun.com nomodify notrap noquery

42 restrict ntp1.aliyun.com nomodify notrap noquery

43

44 # 外部时间服务器不可用时,以本地时间作为时间服务

45 server 127.127.1.0

46 fudge 127.127.1.0 stratum 1047

48 # Enable public key cryptography.

49 #crypto

50

51 includefile /etc/ntp/crypto/pw

52

53 # Key file containing the keys and key identifiers used when operating

54 # with symmetric key cryptography.

55 keys /etc/ntp/keys

56

57 # Specify the key identifiers which are trusted.

58 #trustedkey 4 8 4259

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

61 #requestkey 862

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

64 #controlkey 865

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.co 10.137.38.86 2 u 28 64 3 5.092 6241215 185.623

132.163.96.4 .INIT. 16 u - 64 0 0.000 0.000 0.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行,在下面加入新的上游服务器

2# Please consider joining the pool (http://www.pool.ntp.org/join.html).

3#server 0.centos.pool.ntp.org iburst

4#server 1.centos.pool.ntp.org iburst

5#server 2.centos.pool.ntp.org iburst

6#server 3.centos.pool.ntp.org iburst

server ntp2.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             

spacer.gif

小伙伴们可以关注我的微信公众号:linux运维菜鸟之旅

qrcode_for_gh_5ecc48d3d14a_258.jpg

关注“中国电信天津网厅”公众号,首次绑定可免费领2G流量,为你的学习提供流量! 

qrcode_for_gh_d20dccd1c74b_258.jpg