内网环境搭建NTP服务器

说在前面:ntp和ntpdate区别

①两个服务都是centos自带的(centos7中不自带ntp)。ntp的安装包名是ntp;ntpdate的安装包是ntpdate。他们并非由一个安装包提供。

②ntp守护进程为ntpd,配置文件是/etc/ntp.conf

③ntpdate用于客户端的时间矫正,非NTP服务器可以不启动NTP。

简介:ntp全名 network time protocol 。NTP服务器可以为其他主机提供时间校对服务。

环境准备:两台服务器,一台作为NTP服务器,另一台作为client端向服务器同步时间测试。

NTP服务器:156.0.26.6

client端:156.0.0.27

安装与配置:我们从配置文件的角度来讲解一下ntp的配置

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

 driftfile /var/lib/ntp/drift  #默认即可。driftfile用来指定记录本机与上层NTP server之间的频率误差。单位是百万分之一秒。

 # Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery     
# restrict用来管理权限控制。
# 格式为 restrict [单个ip|网络|default] parameter     parameter:
# ignore:拒绝所有的ntp连接        
# nomodify:客户端不能使用ntpc和ntpq这两个程序来更改服务器的时间参数,但客户端可以通过此主机来进行网络校时。
# noquery:客户端不能使用ntpc和ntpq等命令来查询时间服务器,等于不提供网络校时服务。        # notrap:不提供trap这个网络时间登陆的功能        # notrust:拒绝没有认证的客户端  示例:restrict 156.0.26.7  nomodify 9 
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1  #以下两条默认,放行本机来源
restrict ::1

# 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 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:用来设置上层NTP服务器,说白了就是client向谁请求NTP时间同步。    特别注意,在内网环境中由于无法连接到内网,所以没有办法向例如国家授时服务中心210.72.145.44同步时间    只能将内网中的某台主机设置为server,用以向其他内网服务器提供NTP服务。  server 127.127.1.0 prefer #以本机时间作为时间服务。内网中这个配置一定要加上,否则会导致NTP服务不可用                 # prefer代表这台主机优先级最高。
# broadcast 192.168.1.255 autokey    # broadcast server
# broadcastclient            # broadcast client
# broadcast 224.0.1.1 autokey        # multicast server
# multicastclient 224.0.1.1        # multicast client
# manycastserver 239.255.254.254        # manycast server
# manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
# crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys ##除了restrict来限制客户端连接外,还可以通过秘钥方式来给客户端认证。

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

示例

服务器端需要修改/etc/ntp.conf,添加以下内容

server 127.127.1.0 prefer  #设置本机为NTP服务器
restrict 156.0.26.7            #允许客户端156.0.26.7向本机请求时间同步
restrict 156.0.26.0 mask 255.255.255.0 #允许客户端156.0.26.0网段的所有主机向本机请求时间同步

客户端需要修改/etc/ntp.conf,添加以下内容

 server 156.0.26.6 #指名上层NTP服务器
 restrict 156.0.26.6 #放行156.0.26.6

验证

首先验证服务是否启动成功

启动服务 :service ntpd start

查看服务是否启动:netstat -tunlp |grep ntp ;ntp默认监听于UDP的123端口

其次验证NTP是否正常工作

此处需要使用156.0.26.7这台Client服务器,在此主机上可以使用命令ntpstat或ntpq -p 这两个命令。

ntpstat:这条命令可以查看我们的客户端(此处为156.0.26.7)是否与server(156.0.26.6) 连接成功。

内网环境搭建NTP服务器

上图的大致意思是:本机已经和156.0.26.6这台位于第七层的NTP服务器同步时间,时间精确到36ms以内。每个512s去同步一次时间。

ntpq -p:此命令可以列出当前主机的NTP和上层NTP的状态。

内网环境搭建NTP服务器

remote:上层NTP服务器的IP或者主机名。主要最左边的 :代表目前正在使用中的上层FTP

+:已经连接成功,且可以作为下一个提供时间服务的候选人。

refid:它指的是给远程服务器(156.0.26.6)提供时间同步的服务器,本机上级的上级NTP服务器。

为什么显示为LOCAL(0)?

由于此处为内网环境,无法去外网公用的主机同步时间,因此我们在156.0.26.6上设置的是156.0.26.6自身作为时间服务器同时,使用server 127.127.1.0 prefer指名他的上级NTP为自身。

st:就是stratum层级。 类似于DNS,NTP是层级结构,有顶端的服务器,最多有15层。 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。

when:几秒之前同步过时间

poll:在过多长时间去同步时间。

reach:已经同步时间的次数

delay:网络传输过程中的延迟,单位是10的-6次方秒。

offset:时间修正值,这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别.。单位是10的-3次方。

jitter:linux系统时间(软件时间)与BIOS硬件时间的差异时间,单位是10的-6次方秒。在主机和NTP服务器同步时间欧,可以使用 hwclock -w将系统时间写入BIOS.

若文章对你有帮助,可以点赞或打赏支持我们。发布者:SmallA,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/%e5%86%85%e7%bd%91%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%bantp%e6%9c%8d%e5%8a%a1%e5%99%a8/

(0)
SmallA的头像SmallA认证作者
上一篇 2023年 4月 7日 下午2:25
下一篇 2023年 4月 26日 下午5:03

相关推荐

  • Python进行websocket接口测试

    我们在做接口测试时,除了常见的http接口,还有一种比较多见,就是socket接口,今天讲解下怎么用Python进行websocket接口测试。 现在大多数用的都是websocke…

    技术分享 2023年 3月 2日
  • 通过PAC文件配置自动代理

    文章目录代理自动配置文件(PAC)文件语法参数描述预定义的函数与环境示例1 对关键域名进行代理示例2 对除本地主机以外的所有连接使用代理示例3 和例2一样,但是对防火墙外的本地服务…

    2023年 2月 26日
  • 2.1 使用 systemd

    这个示例将会演示在 Linux 系统下使用 systemd 控制 frps 及配置开机自启。 在 Linux 系统下,使用systemd 可以方便地控制 frp 服务端 frps …

    2023年 3月 2日
  • 4. FRP概念

    文章目录通过 SSH 访问内网机器通过自定义域名访问内网的 Web 服务转发 DNS 查询请求转发 Unix 域套接字对外提供简单的文件访问服务为本地 HTTP 服务启用 HTTP…

    2023年 3月 2日
  • 2.FRP安装

    文章目录安装系统需求下载部署开始使用!使用 systemd 安装 关于如何安装 frp 的说明。 frp 采用 Golang 编写,支持跨平台,仅需下载对应平台的二进制文件即可执行…

    2023年 3月 2日
  • 生成CA证书

    生成服务器CA证书 在配置HTTPS监听时,您可以使用自签名的CA证书,并且使用该CA证书为客户端证书签名。 执行以下命令,在/home目录下新建一个ca文件夹,并在ca文件夹下创…

    2023年 5月 9日
  • 为wordpress新增ip和port日志

    应网安要求,需要对用户日志进行扩增,对用户日志新增端口和port记录 文章目录操作大功告成也可以追加如下字段 操作 找到页面文件目录,在wp-includes/funcitons.…

    2023年 2月 26日
  • 树莓派安装ansible教程(二)

    ansible是一款方便大家对集群计算机进行管理的软件,此软件通过对master节点的设置,通过配置对slave节点的免密服务,完成部署后,即可在master操作一条指令,同时对多…

    2021年 9月 14日
  • pyinstaller打包完很大怎么办?

    Python打包软件有很多,作者这边用的最多的就是以下的这几个 工具名称 windows linux 单文件模式 bbfreeze yes yes py2exe yes yes p…

    2021年 8月 28日
  • 1.FRP概述

    一些概述,便于您快速的了解 frp。 文章目录frp 是什么?为什么使用 frp?下一步? frp 是什么? frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UD…

    2023年 3月 2日
Translate »