1.介绍ntp服务

  • NTP属于运用层协议(依据UDP传输,运用的端口号为123),用来同步网络中分布式时间服务器和客户端之间的时间,使网络中的设备供应依据一起时间的运用成为可能。
  • 时间服务器和客户端是相对的。供应时间规范的设备为时间服务器,接收时间服务的设备为时间客户端。
  • 设备运转NTP之后,通过沟通NTP报文,既可以作为时间服务器供应时间规范,又可以作为时间客户端接收时间服务。

2. 运行环境

服务器端:192.168.203.101
客户端:192.168.203.102

3. 安装ntp服务(服务器和客户端都安装)

  • yum安装方式
    [root@localhost ~]# yum install -y ntp
  • 源码安装方式
    [root@localhost ~]# wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p15.tar.gz
    [root@localhost opt]# tar -xf ntp-4.2.8p15.tar.gz
    [root@localhost opt]# cd ntp-4.2.8p15/
    [root@localhost ntp-4.2.8p15]# ./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
    [root@localhost bin]# make && make install

4. 设置ntp服务开机启动(以yum安装为例)

设置服务开机自启
[root@localhost ntp]# systemctl enable ntpd
#查看是否开机自启
[root@localhost ntp]# systemctl is-enabled ntpd enabled

  • 另外科普一下chkconfig,该命令是linux旧指令,下边用表格说明来看一下区别
任务旧指令新指令
使某服务自动启动chkconfig –level 3 ntpd onsystemctl enable ntpd.service
使某服务不自动启动chkconfig –level 3 ntpd offsystemctl disable ntpd.service
检查服务状态service ntpd statussystemctl status ntpd.service
显示所有已启动的服务chkconfig –listsystemctl list-units –type=service
启动某服务service ntpd startsystemctl start ntpd.service
停止某服务service ntpd stopsystemctl stop ntpd.service
重启某服务service ntpd restartsystemctl restart ntpd.service

5.设置ntp Server服务端配置文件

[root@localhost ~]# vim /etc/ntp.conf
		#和上层NTP服务器频率误差的记录文件
		driftfile /var/lib/ntp/drift
		#对于默认的client拒绝所有操作,下面的restrict开通指定权限
		restrict default nomodify notrap nopeer noquery
		#允许本机地址的一切操作
		restrict 127.0.0.1
		restrict ::1
		
		#允许192.168.203段机器同步时间,指定可以和NTP通信的IP或者网段
		restrict 192.168.203.0 mask 255.255.255.0 nomodify notrap
		
		#远程的同步时间服务器地址(需要同步外网时间必改)
		#prefer表示优先同步的主机
		server cn.pool.ntp.org prefer
		server ntp1.aliyun.com iburst
		server ntp2.aliyun.com iburst
		server ntp3.aliyun.com iburst
		server ntp4.aliyun.com iburst
		server ntp5.aliyun.com iburst
		
		#远程时间服务器不可用时,以本地时间作为服务时间
		server 127.127.1.0
		#stratum指定层数,越小,离时间源,越近。 层级1为国际时间源服务器...
		fudge 127.127.1.0 stratum 10
		
		#允许上层服务器主动修改本机时间(需要同步外网时间必改)
		restrict cn.pool.ntp.org nomodify notrap noquery
		restrict ntp1.aliyun.com iburst nomodify notrap noquery
		restrict ntp2.aliyun.com iburst nomodify notrap noquery
		restrict ntp3.aliyun.com iburst nomodify notrap noquery
		restrict ntp4.aliyun.com iburst nomodify notrap noquery
		restrict ntp5.aliyun.com iburst nomodify notrap noquery
		
		includefile /etc/ntp/crypto/pw
		
		#指定日志文件
		logfile /var/log/ntp
		#指定日志级别,info、all、event三种可以选择
		logconfig all
		
		#启用公钥加密
		#crypto
		#密钥文件
		keys /etc/ntp/keys

6.设置ntp client客户端配置文件

vim /etc/ntp.conf
        server 192.168.203.101

7. 重启ntp服务(客户端和服务器)

[root@localhost /]# systemctl stop firewalld
[root@localhost /]# setenforce 0
[root@localhost /]# vim /etc/selinux/config
	SELINUX=disabled
 
[root@localhost /]# systemctl restart ntpd

8. 查看ntp服务同步状况

[root@localhost ~]# ntpq -p

ntp Server

在这里插入图片描述

ntp client

在这里插入图片描述

查看时间同步状态,一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。 使用ntpstat查看同步结果

在这里插入图片描述在这里插入图片描述

9.参数使用说明

1 | restrict default nomodify notrap nopeer noquery
1 | restrict 192.168.203.0 mask 255.255.255.0 nomodify

restrict 控制相关权限 语法为: restrict IP地址 mask 子网掩码 参数 其中IP地址也可以是default,default 就是指所有的IP
参数有以下几个:
ignore :关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网 NTP4.2 版本以后使用会出错
noquery:不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap:不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是ntpdq控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
kod :访问违规时发送 KoD 包。
restrict -6 表示IPV6地址的权限设置。

2 | server 127.127.1.0

只有在上级时钟源失效时,NTP才会使用127.127.1.0的本地时钟,将local时间作为ntp服务器时间提供给ntp客户端。NTP把本地主机的时钟也看作外部时钟源来处理,分配的地址是127.127.1.0

3 | fudge 127.127.1.0 stratum 10

设置本地时钟源的层次为10,这样如果NTP服务从本地时钟源获取时间的话,NTP对外宣布的时间层次为11

4 | ntpq -p

在这里插入图片描述

remote: 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid:参考上一层ntp主机地址
st:stratum阶层
when:多少秒前曾经同步过时间
poll:下次更新在多少秒后
reach:已经向上层ntp服务器要求更新的次数
delay:网络延迟
offset:时间补偿
jitter:系统时间与bios时间差

10.使用ntpdate当作客户端

>#测试ntpdate同步是否正常
>ntpdate 192.168.203.101
>#使用定时任务同步
>crontab -e
>	#每天凌晨五点同步
>	0 5 * * * /usr/sbin/ntpdate 192.168.203.101
>不建议使用ntpdate,原因看下方介绍

ntpd与ntpdate的区别
ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。
因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差??或者说 Local Clock 的自然漂移(drift)??记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

11.常用的ntp公共时间服务器

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐