时间同步服务器NTP的搭建

一、ntp简介

网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。

二、简单的几个命令

1.查看当前时区
[root@lan-test ~]# timedatectl

      Local time: Thu 2019-11-28 15:33:44 CST
  Universal time: Thu 2019-11-28 07:33:44 UTC
        RTC time: Thu 2019-11-28 07:33:44
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

2.列出所有时区
[root@lan-test ~]# timedatectl list-timezones
3.设置时区
[root@lan-test ~]# timedatectl set-timezone Asia/Shanghai
4.设置时间
[root@lan-test ~]# timedatectl set-time '2019-11-28 15:40:00'
[root@lan-test ~]# date
Thu Nov 28 15:40:02 CST 2019
5.配置文件
/etc/ntp.conf

restrict[IP地址]mask[掩码]parameter

parameter的参数如下

parameter说明
ignore拒绝所有类型的连接
nomodify客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可通过该主机来进行网络校时
noquery客户端不能够使用ntpc、ntpq等命令来查询时间服务器,等于不提供NTP的网络校时
notrap不提供trap这个远程事件登录的功能
notrust拒绝没有认证的客户端

例如:

restrict192.168.1.0mask255.255.255.0留空

表示允许192.168.1.0/24网段的主机同步且不受任何限制

server 0.rhel.pool.ntp.org iburst上层服务器的地址,本ntp服务器的时间是同步上层服务器的,iburst表示当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
6.查询上层服务器的状态
[root@lan-test ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 tock.ntp.infoma .STEP.          16 u   39   64    0    0.000    0.000   0.000
+ntp7.flashdance 192.36.143.130   2 u   11   64    1  220.382   -4.645   3.127
+a.ams.pobot.net 17.253.34.125    2 u    6   64    1  202.727  -12.523   2.083
*stratum2-1.ntp. 89.175.20.7      2 u    7   64    1  120.931    3.127   0.163
字段说明
remote上层ntp服务器主机名
*代表正在使用的上层ntp服务器
+代表连接成功,可提供时间更新的候补服务器
-该NTP服务器被认为不合格
refid上层ntp服务器的ip
st上级NTP服务器的层级,0-15
t连接的方式
l local (such as a GPS clock)
u unicast (this is the common type) (单播,即NTP client向NTP server发送NTP请求,NTP server回复的模式)
m multicast (多播,可跨子网。)
b broadcast (广播,不可跨子网。NTP server定时向广播地址发送NTP包,NTP client通过广播地址获取NTP包,同步本地时钟)
when几秒钟前曾做过时间同步化
poll下一次更新在几秒钟之后
reach八进制数,已经向上层服务器要求更新的次数
delay网络传输过程中的延迟时间
offset本地和服务器之间的时间差别,越接近0,说明和服务器的时间约接近
jitter系统时间与硬件时间的差异

三、搭建ntp服务器

1.装包
[root@lan-test ~]# yum install ntp -y
2.防火墙放行服务
[root@lan-test ~]# firewall-cmd --add-service=ntp --per
[root@lan-test ~]# firewall-cmd --reload
3.编辑配置文件
[root@lan-test ~]# vim /etc/ntp.conf

restrict 192.168.1.0 mask 255.255.255.0	//限制当前网段的主机可以同步时间
server 127.127.1.0						//上层服务器为自己,则需注释掉上层服务器
fudge 127.127.1.0 stratum 8				//虚拟自己在第八层

4.重启服务
[root@lan-test ~]# systemctl restart ntpd
5.客户端查询上层服务器状态
[root@server ~]# ntpdate -q 192.168.1.206

server 192.168.1.206, stratum 9, offset 0.000031, delay 0.02629
28 Nov 15:50:38 ntpdate[4032]: adjust time server 192.168.1.206 offset 0.000031 sec

6.客户端同步上层服务器的时间
[root@server ~]# ntpdate 192.168.1.206

28 Nov 15:51:04 ntpdate[4033]: adjust time server 192.168.1.206 offset 0.000026 sec

7.设置定时同步时间,每天16:18分同步时间
[root@server ~]# crontab -e

18 16 * * * /usr/sbin/ntpdate 192.168.1.206 > /dev/null 2>&1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值