Linux 搭建ntp服务

NTP协议

NTP(Network Time Protocol)是一种基于UDP(User Datagram Protocol)的应用层协议。 被设计用于在计算机网络中同步时间,默认使用123端口号。

例如:

在一个局域网中,需要保证若干台服务器之间的时间一致,这个时候就可以通过将一台主机搭建为时间服务器,其它局域网内的主机可以和本机的时间进行同步。ntp就是实现NTP协议的一个工具,ntp工具的存在时间已经很长了,很稳定。所以使用ntp来搭建时间服务器是一个很好的选择。

搭建NTP服务器

1、安装工具

需要先安装ntp工具,可以直接通过Linux发行版上的软件包管理工具进行安装,也可以下载源码包进行离线安装;

ntp工具有一个特点就是属于客户端和服务端一体的工具,即通过不同的配置,可以配置为服务端,也可以配置为客户端;

ntp官网:https://www.ntp.org/

# centos
yum install ntp

# ubuntu
apt install ntp

2、修改配置

ntp的配置文件如果是通过软件管理工具安装的,一般是:/etc/ntp.conf(/etc/ntp/ntp.conf) 这个配置文件

搭建NTP服务器就需要把NTP配置为服务端,并且时间以本机时间为准,这个时候需要修改配置文件中的一下内容实现;

(1)通过 service 指令将本地时钟配置为NTP服务器的时间源,并设置优先同步本地时间

server 127.127.1.0

fudge 127.127.1.0 stratum 10

如果是在局域网里面没法访问外网,可以把这些默认的时间源给注释掉,这里使用pool指令指定时间 源地址。pool和server有以下区别:

  • server:指定一个NTP服务器来同步时间,如果是自己搭建的单个时间服务器,一般使用server来指定。

  • pool:允许指定一个NTP服务器的池。这些池中的服务器是动态的,如果一个服务器变得不可用或不稳定,NTP客户端可以轻松地切换到同一个池中的另一个服务器。一般公共的时间服务器都是使用pool来指定

说明:

  • 127.0.0.1是我们熟知的本地换回地址,而在 NTP 协议 中,IP 地址 127.127.1.0 是特殊的,它表示本地时钟 (Local Clock)。

(2)通过 restrict 指令设置时间同步规则,可以同时设置多条规则

# 设置来自IPv4地址和IPv6地址的默认访问规则
restrict -4 default kod notrap nomodify nopeer
restrict -6 default kod notrap nomodify nopeer

# 来自本地的请求,没加任何限制,可以查看和修改
restrict 127.0.0.1
restrict ::1

# 设置来自 192.168.0.0网段请求的访问规则
restrict 192.168.0.0 mask 255.255.252.0 notrap nomodify

说明:

  • 如果有一个请求来自IPv4地址,如果属于192.168.0.0网段,就会匹配到 restrict 192.168.0.0 mask 255.255.255.0 notrap nomodify noquery 这条规则,如果这个IPv4地址不属于这个网段,就会匹配到 restrict -4 default kod notrap nomodify nopeer noquery 这条规则。

说明:

这些参数的目的就是为了提高NTP服务器的安全性,使得对外部的请求进行了限制(不能查询、不能修改)

  • 这里的 -4 和 -6分别表示适用于 IPv4地址和IPv6地址

  • default 用于设置默认的访问控制规则,如果一个请求没有被其它规则匹配,就会使用这个默认的规则

  • kod表示如果NTP客户端发送的请求过于频繁,则发送“拒绝服务

  • notrap用于禁止trap设置。这可以防止某些NTP控制消息操作

  • nomodify用于不允许从这个地址修改NTP服务器

  • nopeer表示从该地址的机器不允许创建持久对等关系。

  • noquery表示不允许来自该地址的机器查询ntp服务器的状态(设置了noquery选项后,来自该地址或地址范围的NTP客户端使用ntpq -p命令将无法查询NTP服务器的状态)

  • limited参数用于对于频繁的请求进行速率限制(不会影响正常同步时间的客户端,只有那些异常频繁地查询服务器的客户端会受到影响)

3、重启服务

配置文件修改完成后,重启服务生效,NTP协议的功能主要是通过ntpd这个守护进程来实现,ntp.service是systemd用来管理ntpd守护进程的方式

# centos这种,上面默认的service是ntpd.service

systemctl restart ntp.service

4、检查是否生效

NTP协议使用的是UDP的123端口,可以先检查端口是否处于正常监听的状态:

ss -nul | grep 123

全部配置完成后,通过 ntpq -p 来检查时间服务器是否生效,可以通过 -n 参数来显示数字地址而不是主机名

ntpq -np

说明:

  • remote:这里表示的是对等方的地址,127.127.1.0表示一个本地时钟驱动

  • refid:用于标识对等方的时间源,LOCL.表示本地时钟。

  • st:用于表示级别,

  • t:类型,这里的l表示本地时钟驱动

  • when:上次接收到这个对等方信息以来的秒数

  • pool:表示每64秒轮询一次对等方的时间

  • reach:是要给八进制数字,只要不为0,表示在最近的8次轮询中,至少有一次成功的通信

  • dalay、offset、jitter:表示延迟、时间偏移和偏移量的变化

ntp.conf全部配置文件:

ehigh@ubuntu:~$ cat /etc/ntp.conf 
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
# pool 0.ubuntu.pool.ntp.org iburst
# pool 1.ubuntu.pool.ntp.org iburst
# pool 2.ubuntu.pool.ntp.org iburst
# pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
# pool ntp.ubuntu.com

server 127.127.1.0

fudge 127.127.1.0 stratum 10

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
#restrict -4 default kod notrap nomodify nopeer noquery limited
#restrict -6 default kod notrap nomodify nopeer noquery limited

restrict -4 default kod notrap nomodify nopeer
restrict -6 default kod notrap nomodify nopeer

restrict 192.168.0.0 mask 255.255.252.0 notrap nomodify

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
#restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

使用NTP服务器

1、安装工具

安装方法同上

2、修改配置

将ntp配置为客户端,同步指定ntp服务端的时间。只需要在配置文件里面通过 server 来指定ntp服务端的IP地址即可。

server 192.168.13.255

说明:

加速NTP的初始同步过程,实现快速同步,可以使用 iburst 关键字

server 192.168.13.255 iburst

因为 当NTP服务首次启动或当NTP服务认为自己已经失去与服务器的同步时,如果配置了iburst,NTP会发送一个连续的查询包序列来尝试快速同步。具体来说,而不是每隔约64秒发送一次查询(默认行为),它会在短时间内连续发送约8个查询。

3、重启服务

systemctl restart ntp.service 

4、同步检查

ntpq -np

说明:

这里的offset值是“-11.348”表示本地时钟与时间源之间的时间差异,单位是毫秒

使用ntp时间源的配置:

ehigh@ubuntu:~$ cat /etc/ntp.conf 
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
# pool 0.ubuntu.pool.ntp.org iburst
# pool 1.ubuntu.pool.ntp.org iburst
# pool 2.ubuntu.pool.ntp.org iburst
# pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
# pool ntp.ubuntu.com

server 192.168.13.255

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

posted on 2022-12-13 16:42  背对背依靠  阅读(1154)  评论(0编辑  收藏  举报