手把手教你搭建Linux时间同步服务器

在这里插入图片描述

1. NTP简介

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。还可介由加密确认的方式来防止恶毒的协议攻击。时间按NTP服务器的等级传播。按照离外部UTC源的远近把所有服务器归入不同的Stratum(层)中。NTP服务器就是利用NTP协议提供时间同步服务的。

2. NTP服务器安装

#一般服务器都会自带ntp服务
[root@nfs01 ~] rpm -qa|grep ntp
ntp-4.2.6p5-10.el6.centos.2.x86_64
ntpdate-4.2.6p5-10.el6.centos.2.x86_64
如果没有,直接利用yum进行安装
[root@nfs01 ~] yum install -y ntp

3. 配置NTP服务

在配置文件vim /etc/ntp.conf中修改参数

[root@nfs01 ~] vim /etc/ntp.conf
# restrict default kod nomodify notrap nopeer noquery
restrict default nomodify
# nomodify客户端可以同步
# 将默认时间同步源注释改用可用源
# 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 ntp1.aliyun.com
server time.nist.gov

4. 启动NTP服务器

#如果计划任务有时间同步,先注释,两种用法会冲突。
#如果没有则进行下一步
1.[root@nfs01 ~] crontab -e
no crontab for root - using an empty one
#启动ntp服务

2.[root@nfs01 ~] /etc/init.d/ntpd start
Starting ntpd:                [  OK  ]

3.[root@nfs01 ~]  ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 120.25.115.20   10.137.53.7      2 u   51   64    3   37.536  1742252   0.498
 time-a-wwv.nist .NIST.           1 u   49   64    3  203.111  1742252  11.005

4.[root@nfs01 ~] ntpstat
unsynchronised
polling server every 64 s

#测试ntp服务
5.[root@nfs01 ~]# ntpdate 172.16.1.31
11 Oct 19:12:04 ntpdate[2072]: the NTP socket is in use, exiting

5. 客户端时间同步

客户机要等10分钟左右再与新启动的ntp服务器进行时间同步,否则会报错no server suitable for synchronization found。

[root@backup ~]# ntpdate 172.16.1.32
21 Sep 15:46:00 ntpdate[2011]: no server suitable for synchronization found

过10分钟左右后,客户端开始与ntp服务端进行时间同步后结果;

[root@backup ~]# ntpdate 172.16.1.31
11 Oct 19:42:35 ntpdate[2349]: step time server 172.16.1.31 offset 1740585.341828 sec

猜你喜欢

转载自blog.csdn.net/weixin_53488443/article/details/120708729