NTP服务简介
- NTP是网络时间协议(Network Time protocol 网络时间协议),它是用来同步网络中各个计算机的时间的协议。NTP服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器于时间服务器的时间保持一致,某些应用对时间实时性要求搞得必须统一时间。
- 在计算机的世界里,时间非常的重要,例如对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常的高,是按照A这台计算机的时间还是B的?NTP就是用来解决这个问题的,NTP是用来使网络中的各个计算机时间同步的一种协议。
- NTP的用途是把计算机的时钟同步到世界协调UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms.
- NTP可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。
- 互联网的时间服务器也有很多,例如 ntpdate ntp.sjtu.edu.cn 上海交通大学的NTP免费提供互联网时间同步。
NTP服务器配置
1、安装NTP软件包
[root@bogon ~]# yum -y install ntp ntpdate.x86_64
2、修改ntp.conf配置文件
- 把如下四行注释掉
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
- 在下面添加两行,不能是127.0.0.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
server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10
server
作用:指定ntp服务器的地址
格式:server [ip or hostname] [perfer]
例子:server 127.127.1.0 <<<将当前主机作为时间服务器
fudge
作用:设置时间服务器的层级
格式:fudge ip [startnum int]
例子:fudge 10.225.5.1 startnum 10
注意:fudge必须和server一块用, 而且是在server的下一行
startnum
0~15
0:表示顶级
10:通常用于给局域网主机提供时间服务
3、启动ntp服务并再次查看状态
[root@bogon ~]# systemctl restart ntpd
[root@bogon ~]# systemctl status ntpd
4、查看是否同步
[root@bogon ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 19 64 377 0.000 0.000 0.000
5、设置开机自启
[root@bogon ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
客户端配置
1、安装NTP软件包
[root@bogon wo]# yum -y install ntp ntpdate.x86_64
2、重启服务并设置开机自启
[root@bogon wo]# systemctl start ntpd
[root@bogon wo]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
3、修改配置文件,将刚刚搭建好的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
修改为
#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
server 192.168.2.10 iburst
restrict 192.168.2.10 nomodify notrap noquery
192.168.2.10为NTP服务器IP
restrict
作用:对ntp做权限控制
格式:restrict [ip] [mask] [par]
par:
ignore:忽略所有类型的NTP连接请求
nomodify:限制客户端不能使用命令ntpc和ntpq来修改服务器端的时间
noquery:不提供NTP网络校时服务
notrap:不接受远程登录请求
notrust:不接受没有经过认证的客户端的请求
【如果没有用任何参数,那么表示不做任何限制】
例子:restrict 10.220.5.0 mask 255.255.255.0 nomodify <<<允许10.220.5.0/24 网段主机进行时间同步
4、跟NTP服务器时间同步一下
[root@bogon wo]# ntpdate -u 192.168.2.10
17 Apr 19:59:59 ntpdate[13730]: adjust time server 192.168.2.10 offset 0.128113 sec
5、重启服务
[root@bogon wo]# systemctl restart ntpd
6、查看状态
[root@bogon wo]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*bogon LOCAL(0) 11 u 1 64 3 0.235 22.965 14.293
7、客户端配置同步时间,每天六点十分与服务器进行同步
[root@bogon ~]# crontab -e
10 06 * * * /usr/sbin/ntpdate -u 192.168.2.10 >> /usr/local/logs/crontab/ntpdate.log
注:192.168.2.10是NTP服务器IP