运维Linux架构师

搭建ntp服务

2019-11-01  本文已影响0人  唯爱熊

ntp简介

ntp全称Network Time Protocol(NTP)是用来使计算机[时间同步化的一种协议,它可以使对其(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。

ntp场景设置以及地址规划

系统环境准备

yum方式安装,此处统一使用aliyun的base和epel源
[root@ntp-server ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
查看内核版本
[root@ntp-server ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
关闭防火墙
[root@ntp-server ~]# getenforce
Disabled
[root@ntp-server ~]# systemctl stop firewalld.service

额外说明

ntp和ntpdate区别:

1.Centos 7 之前两个服务都是centos自带的(centos7中不自带ntp)。ntp的安装包名是ntp,ntpdate的安装包是ntpdate。他们并非由一个安装包提供。
2.ntp守护进程为ntpd,配置文件是/etc/ntp.conf
3.ntpdate用于客户端的时间矫正,非NTP服务器可以不启动NTP。

服务端安装

1安装ntp,ntpdate

[root@ntp-server ~]# yum -y install ntp ntpdate

2配置ntp

[root@ntp-server ~]# vim /etc/ntp.conf
做如下修改:
1.注释如下四条:
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
2.添加如下内容:
server ntp1.aliyun.com     #定义使用的上游 ntp服务器
restrict ntp1.aliyun.com nomodify notrap noquery #允许上层时间服务器主动修改本机时间
server 127.127.1.0 prefer  #设置本机为NTP服务器,在外部时间服务器不可用的前提下。
restrict 172.16.1.62           #允许客户端172.16.1.61向本机请求时间同步
restrict 172.16.1.0 mask 255.255.255.0 #允许客户端172.16.1.0网段的所有主机向本机请求时间同步。

3.启动ntp服务

[root@ntp-server ~]# systemctl start ntpd
ntp服务的默认端口使用的事udp 123
查看是否启动成功:
[root@m01 ~]# netstat -lntup|grep 123
udp        0      0 172.16.1.61:123         0.0.0.0:*                           7898/ntpd           
udp        0      0 10.0.0.61:123           0.0.0.0:*                           7898/ntpd           
udp        0      0 127.0.0.1:123           0.0.0.0:*                           7898/ntpd           
udp        0      0 0.0.0.0:123             0.0.0.0:*                           7898/ntpd           
udp6       0      0 fe80::20c:29ff:fec7:123 :::*                                7898/ntpd           
udp6       0      0 fe80::20c:29ff:fec7:123 :::*                                7898/ntpd           
udp6       0      0 ::1:123                 :::*                                7898/ntpd           
udp6       0      0 :::123                  :::*   

4.配置文件解释

 # 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  #默认即可。driftfile用来指定记录本机与上层NTP server之间的频率误差。单位是百万分之一秒。

 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 nomodify notrap nopeer noquery
#restrict用来管理权限控制。格式为 restrict [单个ip|网络|default] parameter
parameter:
ignore:拒绝所有的ntp连接
nomodify:客户端不能使用ntpc和ntpq这两个程序来更改服务器的时间参数,单客户端可以通过此主机来进行网络校时。
noquery:客户端不能使用ntpc和ntpq等命令来查询时间服务器,等于不提供网络校时服务。
notrap:不提供trap这个网络时间登陆的功能
notrust:拒绝没有认证的客户端
示例:restrict 172.16.1.62 nomodify
 9

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

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

12 # the administrative functions.

13 restrict 127.0.0.1  #以下两条默认,放行本机来源

14 restrict ::1

15

16 # Hosts on local network are less restricted.

17 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

18

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

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

21 server 0.centos.pool.ntp.org iburst #以下四条为默认,注释掉即可

22 server 1.centos.pool.ntp.org iburst

23 server 2.centos.pool.ntp.org iburst

24 server 3.centos.pool.ntp.org iburst

25 server:用来设置上层NTP服务器,说白了就是client向谁请求NTP时间同步。
特别注意,在内网环境中由于无法连接到内网,所以没有办法向例如国家授时服务中心210.72.145.44同步时间只能讲内网中的某台主机设置为server,用以向其他内网服务器提供NTP服务。
server 127.127.1.0 prefer #以本机时间作为时间服务。内网中这个配置一定要加上,否则会导致NTP服务不可用
#prefer代表这台主机优先级最高。

26 #broadcast 192.168.1.255 autokey    # broadcast server

27 #broadcastclient            # broadcast client

28 #broadcast 224.0.1.1 autokey        # multicast server

29 #multicastclient 224.0.1.1        # multicast client

30 #manycastserver 239.255.254.254        # manycast server

31 #manycastclient 239.255.254.254 autokey # manycast client

32

33 # Enable public key cryptography.

34 #crypto

35

36 includefile /etc/ntp/crypto/pw

37

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

39 # with symmetric key cryptography.

40 keys /etc/ntp/keys ##除了restrict来限制客户端连接外,还可以通过秘钥方式来给客户端认证。

41

42 # Specify the key identifiers which are trusted.

43 #trustedkey 4 8 42

44

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

46 #requestkey 8

47

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

49 #controlkey 8

50

51 # Enable writing of statistics records.

52 #statistics clockstats cryptostats loopstats peerstats

53

54 # Disable the monitoring facility to prevent amplification attacks using ntpdc

55 # monlist command when default restrict does not include the noquery flag. See

56 # CVE-2013-5211 for more details.

57 # Note: Monitoring will not be disabled with the limited restriction flag.

58 disable monitor

客户端安装

1.安装ntp,ntpdate

[root@ntp-client ~]# yum -y install ntp ntpdate

2.修改配置文件

[root@ntp-client ~]# vim /etc/ntp.conf
添加如下的内容:
server 172.16.1.61    #指名上层NTP服务器
restrict 172.16.1.61       #放行172.16.1.61

客户端测试

[root@ntp-client ~]# date -s 2017/12/1
Fri Dec  1 00:00:00 CST 2017
[root@ntp-client ~]# ntpdate 172.16.1.61
 2 Nov 00:21:05 ntpdate[7979]: step time server 172.16.1.61 offset 60567641.756538 sec
上一篇 下一篇

猜你喜欢

热点阅读