1.1 NTP简介
NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。
NTP服务器就是利用NTP协议提供时间同步服务的。
系统版本
[root@iso-all ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
1.2 NTP服务器安装
[root@iso-all ~]# yum install -y ntp ntpdate
[root@iso-all ~]# rpm -qa ntp
ntp-4.2.6p5-29.el7.centos.2.x86_64
1.3 配置NTP服务
备份配置文件
[root@iso-all ~]# cp /etc/ntp.conf{,.bak} [root@iso-all ~]# ll /etc/ntp.conf* -rw-r--r-- 1 root root 2000 11月 28 2019 /etc/ntp.conf -rw-r--r-- 1 root root 2000 9月 27 17:09 /etc/ntp.conf.bak
精简化配置文件
[root@iso-all ~]# egrep -v "^$|#" /etc/ntp.conf.bak >/etc/ntp.conf
修改配置文件
driftfile /var/lib/ntp/drift # restrict default nomodify notrap nopeer noquery #restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器 restrict 127.0.0.1 #(这里的查询是服务器本身状态的查询)。 restrict ::1 server ntp1.aliyun.com iburst #这个域名都是互联网上的ntp服务器,也还有许多其他可用的ntp服务器,能连上 #server 127.127.1.0 iburst local clock #这句也是手动添加的,可以将局域网中的指定ip作为局域网内的ntp服务器。 includefile /etc/ntp/crypto/pw #该文件包含在/etc/ntp.conf中。 它指定用于的密码#decrypt包含私钥和身份参数的文件。 密码 仅在文件已加密时才需要#。 keys /etc/ntp/keys disable monitor #
1.4 启动NTP服务器
注意:如果有同步时间的定时任务要将其注销,否则会冲突
[root@iso-all ~]# systemctl start ntpd
显示节点列表
[root@iso-all ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 120.25.115.20 .STEP. 16 u 72 64 0 0.000 0.000 0.000
1.5 客户机时间同步
客户端安装NTP服务
[root@10e0e0e17 ~]#yum install -y ntp ntpdate
修改配置文件
driftfile /var/lib/ntp/drift # restrict default nomodify notrap nopeer noquery #restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器 restrict 127.0.0.1 #(这里的查询是服务器本身状态的查询)。 restrict ::1 server 10.0.0.15 iburst #这个域名都是互联网上的ntp服务器,也还有许多其他可用的ntp服务器,能连上 #server 127.127.1.0 iburst local clock #这句也是手动添加的,可以将局域网中的指定ip作为局域网内的ntp服务器。 includefile /etc/ntp/crypto/pw #该文件包含在/etc/ntp.conf中。 它指定用于的密码#decrypt包含私钥和身份参数的文件。 密码 仅在文件已加密时才需要#。 keys /etc/ntp/keys disable monitor
1.6 启动NTP客户端
[root@10e0e0e17 ~]# systemctl start ntpd [root@10e0e0e17 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 10.0.0.15 120.25.115.20 3 u 55 64 0 0.000 0.000 0.000