NTP服务器搭建与配置

NTP服务器建与配置
hostnameipOS
server192.168.220.138centos7
client192.168.220.139centos7

1、原理

NTP(Network Time Protocol,网络时间协议)是用来使计算机时间同步化的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒).

2、NTP服务器层次

由于NTP时间服务器采用类似分级架构(stratum)来处理时间的同步化,所以它使用的是类似一般Server/Client的主从架构。

当我们配置一台NTP主机,这台NTP所向上要求同步化的那台主机位stratum-1是,那么我们的NTP就是stratum-2。依次类推,最多可达15个阶层

3、NTP端口

[root@server ~]# vim /etc/services 
123 ntp             123/tcp
124 ntp             123/udp                         # Network Time Protocol

4、NTP服务端客户端安装

[root@server ~]# yum install ntp -y
[root@server ~]# systemctl start ntpd
[root@server ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

[root@server ~]# netstat -antup | grep 123
udp        0      0 192.168.220.138:123     0.0.0.0:*                           107447/ntpd         
udp        0      0 127.0.0.1:123           0.0.0.0:*                           107447/ntpd 
[root@client ~]# yum install ntpdate -y

5、restrict进行权限控制

[root@server ~]# vim /etc/ntp.conf 
 10 # Permit all access over the loopback interface.  This could
 11 # be tightened as well, but to do so would effect some of
 12 # the administrative functions.
 13 restrict 127.0.0.1
 14 restrict ::1

restrict进行权限控制

restrict [你的IP] mask [netmask_IP] [parameter]

parameter有如下参数

  1. ignore:拒绝所有类型的NTP连接。
  2. nomodify:客户端不能使用ntpq这来修改服务器的时间参数,但仍可以网络校时。
  3. noquery:客户端不能使用ntpq来查询时间服务器,等于不提供NTP的网络校时。
  4. notrap:不提供trap这个远程时间登陆的功能。
  5. 拒绝没有认证的客户端。

如果没有在parameter的地方加上任何参数的话,这表示“该IP或网段不受任何限制”的意思。

例:restrict 192.168.220.0 mask 255.255.255.0 ignore 限制192.168.220.0/24网段对时

6、利用server设置上层NTP服务器

[root@server ~]# vim /etc/ntp.conf 
 19 # Use public servers from the pool.ntp.org project.
 20 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
 21 server 0.centos.pool.ntp.org iburst
 22 server 1.centos.pool.ntp.org iburst
 23 server 2.centos.pool.ntp.org iburst
 24 server 3.centos.pool.ntp.org iburst
 
设置方式 server [IP or hostname] [prefer]
默认优先级从上到下

7、NTP常用命令

  1. ntpstat查看ntp服务器是否顺利更新时间
[root@server ~]# ntpstat 
synchronised to NTP server (202.108.6.95) at stratum 3 
   time correct to within 1036 ms
   polling server every 64 s
  1. ntpq -p 可以列出当前我们的NTP与相关的上层NTP的状态
[root@server ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*xk-6-95-a8.bta. 10.69.2.34       2 u   39   64   77    5.093   51.284   3.991
 a.ams.pobot.net 17.253.52.253    2 u   36   64   35  298.308   50.842  22.296
+b.sin.pobot.net 17.253.82.125    2 u  104   64   76  180.526   15.973   5.702
+119.28.183.184  100.122.36.196   2 u   34   64   77   51.837   66.186  10.971
  • remote:NTP主机的IP或主机名,左边“*”代表目前正在作用当中的上层NTP。“+”表示已经连接成功,可以作为下一个提供时间更新的候选者。
  • refid:参考的上一层NTP主机的地址。
  • st:上层stratum阶层。
  • when :几秒钟前曾经做过时间同步化更新的操作。
  • poll:下一次更新在几秒钟之后。
  • reach:已经向上层NTP服务器要求更新的次数。
  • delay:网络传输过程中延迟的时间,单位为10-6秒。
  • offset:时间补偿的结果,单位为10-3秒。
  • jitter:Linux系统时间与BIOS硬件时间的差异时间,单位为10-6秒。
  1. ntpdate与服务器对时
[root@client ~]# ntpdate server
21 Jul 21:36:40 ntpdate[12712]: adjust time server 192.168.220.138 offset -0.063698 sec

8、软件时钟与硬件时钟

  • 硬件时钟:Linux自己的系统时间,从1970年1月1日开始记录的时间参数
[root@server ~]# date
2019年 07月 21日 星期日 21:40:29 CST
  • 软件时钟:计算机系统在BIOS记录的实际时间,这也是硬件所记录的
hwclock [-rw]
-r:读出目前BIOS时间。
-w:将目前Linux系统时间写入BIOS中。
[root@server ~]# hwclock -r
2019年07月21日 星期日 21时41分00秒  -0.694246 秒

9、timedatectl

[root@server ~]# timedatectl 						##查看当前设置
      Local time: 日 2019-07-21 21:48:57 CST
  Universal time: 日 2019-07-21 13:48:57 UTC
        RTC time: 日 2019-07-21 13:48:56
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@server ~]# timedatectl list-timezones | more		#列出时区
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
[root@server ~]# timedatectl set-timezone [时区]		#设置时区

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