温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ntp时间服务器怎么安装部署

发布时间:2022-03-17 10:54:17 来源:亿速云 阅读:217 作者:iii 栏目:web开发

这篇文章主要讲解了“ntp时间服务器怎么安装部署”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ntp时间服务器怎么安装部署”吧!

ntp时间服务器

ntp简介

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

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

50ms。

NTP服务器就是利用NTP协议提供时间同步服务的。

环境准备

[root@ntpserver ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

主机规划

NTP服务端:ntpserver IP:10.0.0.61
NTP客户端:ntpclient IP:10.0.0.7

服务端安装部署

安装ntp和ntpdate

yum install ntp ntpdate -y

启动ntp

systemctl start ntpd.service
systemctl enable ntpd.service

查看是否成功

netstat -lntup|grep ntpd

配置ntp配置文件

[root@ntpserver ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
# 允许内网其他机器同步时间
restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
#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
# 定义使用的上游 ntp服务器,将原来的注释
server time1.aliyun.com
server ntp1.aliyun.com
# 允许上层时间服务器主动修改本机时间
restrict time1.aliyun.com nomodify notrap noquery
restrict ntp1.aliyun.com nomodify notrap noquery
# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

重启ntp服务

systemctl restart ntpd.service

==注意==:如果有同步时间的定时任务要将其注销。否则会冲突

查看ntp服务器与上层ntp的状态

[root@ntpserver ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
203.107.6.88 10.165.84.13 2 u 66 64 7 27.784 10.499 1.787
*120.25.115.20 10.137.53.7 2 u 3 64 17 33.749 9.611 1.618
LOCAL(0) .LOCL. 10 l 10 64 17 0.000 0.000 0.000
#参数含义
remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid:参考上一层ntp主机地址
st:stratum阶层
when:多少秒前曾经同步过时间
poll:下次更新在多少秒后
reach:已经向上层ntp服务器要求更新的次数
delay:网络延迟
offset:时间补偿
jitter:系统时间与bios时间差

客户端部署

安装ntp和ntpdate

yum install ntp ntpdate -y

配置ntp服务配置文件

[root@web01 ~]# cat /etc/ntp.conf
...
...
restrict 127.0.0.1
restrict ::1
#新增此行,进行权限配置
restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
# Hosts on local network are less restricted.
#restrict 192.168.1.0 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 172.16.1.61 prefer
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
....
....

手动同步一次时间(服务端主机IP,这里需要先关闭NTP服务哦)

[root@ntpclient ~]# ntpdate 172.16.1.61
25 Mar 14:38:04 ntpdate[2937]: step time server 172.16.1.51 offset 139055.717574 sec

注意:客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示以下这个错误。

no server suitable for synchronization found
在这里,你可以设置定时任务,定时向服务端同步时间,亦可以启动ntp服务,使其自动向服务端同步时间。

启动ntp

systemctl start ntpd.service
systemctl enable ntpd.service

观察时间同步状况

[root@ntpcilent ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
172.16.1.61 203.107.6.88 3 u - 64 1 2.485 400.150 0.000

查看时间同步结果

[root@ntpcilent ~]# ntpstat
unsynchronised
time server re-starting
polling server every 8 s
#同步失败,同步也需要时间嘛,需等待5-10分钟再次查询
[root@ntpcilent ~]# ntpstat
synchronised to NTP server (172.16.1.61) at stratum 3
time correct to within 28 ms
polling server every 64 s
#OK!时间同步完成,date一下是不是和服务器主机时间一致呢

扩展一:系统时间与硬件时间同步

如果主从服务时间超过1000秒则不再进行同步了,这时候要手动同步,即:/usr/sbin/ntpdate命令,如果怕服务器

时差会经常变动比较大可以再Linux中添加计划任务,例如:

10 5 * * * root /usr/sbin/ntpdate 192.168.31.223 && /sbin/hwclock -w

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfifig/ntpd文件,
在/etc/sysconfifig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

hwclock -r 读出BIOS的时间参数
hwclock -w 将当前系统时间写入BIOS中

扩展二:不同机器之间的时间同步

为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。

同步时间,可以使用ntpdate命令,也可以使用ntp服务。

方法一:使用ntpdate比较简单,格式如下

[root@client ~]# ntpdate 172.16.1.623 May 19:50:44 ntpdate[7507]: step time server 172.16.1.61 offset 1.239826 sec

但这样的同步,只是强制性的将系统时间设置为ntp 服务器时间。只是治标不治本。所以,一般配合cron命令,来进

行定期同步设置。比如,在crontab 中添加:

[root@client ~]# crontab -e
0 10 * * * /usr/sbin/ntpdate 172.16.1.61

方法二:使用ntp服务进行同步

要注意的是,ntpd 有一个自我保护设置:如果本机与上源时间相差太大,ntpd 不运行。所以新设置的时间服务器一

定要先ntpdate 从上源取得时间初值。然后启动ntpd 服务。

ntp.ntpdate的区别
下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:
使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时
间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务准)。
第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错

感谢各位的阅读,以上就是“ntp时间服务器怎么安装部署”的内容了,经过本文的学习后,相信大家对ntp时间服务器怎么安装部署这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ntp
AI