NTP服务

NTP

NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

在计算机的世界里,时间非常地重要

例如:对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,是按照A这台计算机的时间,还是按照B这台计算机的时间?

NTP就是用来解决这个问题的,NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。

它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。

环境

CentOS Linux release 7.7.1908 (Core)
NTP Server服务器IP:192.168.146.139

NTP Client客户端IP:192.168.146.140

1.搭建NTP服务器

1.1.查看服务器是否安装ntp,系统默认安装ntpdate

[root@Centos ~]# rpm -qa |grep ntp 查看是否安装ntp

1.2、安装ntp,ntpdate已经系统默认安装过了

[root@localhost ~]# yum install -y ntp

1.3、修改ntp配置文件

[root@localhost ~]# vim /etc/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

然后在下面添加这几行:

server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst

1.4、启动ntp服务,并开机自启动

[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd

1.5、查询ntp是否同步

[root@Centos ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+139.199.214.202 100.122.36.4 2 u 156 256 377 39.942 -31.482 5.702

1.6、开启防火墙ntp默认端口udp123

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=123/udp
success
[root@localhost ~]# firewall-cmd --reload
Success

2、NTP客户端配置

安装的NTP跟上面的步骤一样

2.1、修改ntp配置文件,将上面的NTP服务器作为客户端同步NTP时间服务器

 [root@Centos ~]# vim /etc/ntp.conf
#permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

#Permit all access over the loopback interface.  This could
#be tightened as well, but to do so would effect some of
#the administrative functions.
restrict 127.0.0.1
restrict ::1

#Hosts on local network are less restricted.
restrict 192.168.146.139 mask 255.255.255.0 nomodify notrap

#Use public servers from the pool.ntp.org project.
#Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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.146.139

2.2、与NTP server服务器同步一下时间:

[root@Centos ~]# ntpdate -u 192.168.146.139

2.3、查看ntp同步状态

[root@Centos ~]# ntpq -p

remote refid st t when poll reach delay offset jitter
==============================================================================
localhost 139.199.214.202 3 u 53 64 1 1.923 1326262 0.000
[root@Centos ~]# ntpdate -u 192.168.146.139
12 Mar 22:23:38 ntpdate[11997]: step time server 192.168.146.139 offset 132626.249426 sec


版权声明:本文为weixin_46478573原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。