如何在Linux上配置NTP服务器并防火墙实现时钟同步 (linux ntp配置 防火墙)

时间同步在计算机网络领域是十分重要的。同时,安全也是一个永恒的话题,因此,在配置NTP服务时,同样需要注意防火墙设置以保障信息安全。本文将介绍如何在Linux上配置NTP服务器,并防火墙实现时钟同步。

一、 安装NTP服务

Ubuntu、Debian、CentOS等主流Linux发行版都默认安装了NTP(网络时间协议)。可以通过以下命令安装和开启NTP服务:

在Ubuntu/Debian上

“`

sudo apt-get install ntp

“`

在CentOS上

“`

sudo yum install ntp

“`

服务安装后可以用以下命令确认NTP状态:

“`

sudo systemctl status ntpd

“`

二、配置NTP服务器

在开启NTP服务之前,我们需要先配置NTP服务器。我们将以Ubuntu 18.04为例。

1. 打开ntp.conf文件

“`

sudo vim /etc/ntp.conf

“`

2. 删掉注释部分“pool.ntp.org iburst”,改为NTP服务器的地址,如:

“`

server 3.cn.pool.ntp.org

“`

3. 若是内网服务器,需要在ntp.conf文件中加入下面两行代码:

“`

restrict default noquery nomodify notrap

restrict 127.0.0.1

“`

restrict限制哪些IP可以向该NTP服务器发送请求。如restrict 127.0.0.1表示只有本机(即127.0.0.1)可以请求NTP服务器。

4. 重启NTP服务:

在Ubuntu/Debian上

“`

sudo systemctl restart ntp

“`

在CentOS上

“`

sudo service ntpd restart

“`

三、防火墙设置

当NTP服务开启后,需要设置防火墙以保障信息安全。

在Ubuntu/Debian上

1. 查看已安装的防火墙

“`

sudo apt-get install ufw

sudo ufw status

“`

若是已开启,则结果应该如下:

“`

Status: active

To Action From

— —— —-

OpenSSH ALLOW Anywhere

“`

2. 开启NTP服务端口

“`

sudo ufw allow from any to any port ntp # UDP 也就是 123 端口号

sudo ufw reload

“`

注:allow from any to any表示开启来自任何IP地址的请求,只要请求端口号为ntp。

3. 确认状态

“`

sudo ufw status

“`

结果如下

“`

Status: active

To Action From

— —— —-

OpenSSH ALLOW Anywhere

123/udp ALLOW Anywhere

OpenSSH (v6) ALLOW Anywhere (v6)

123/udp (v6) ALLOW Anywhere (v6)

“`

则表示成功开放NTP相关端口。

在CentOS上

CentOS 默认使用的是firewalld

1. 开放 123/UDP 端口

“`

sudo firewall-cmd –add-service=ntp –permanent

sudo firewall-cmd –reload

“`

2. 确认状态

“`

sudo firewall-cmd –list-all

“`

结果如下

“`

public (active)

target: default

icmp-block-inversion: no

interfaces: eth0

sources:

services: ssh dhcpv6-client ntp

ports: 111/tcp 2023/tcp 9418/tcp 111/udp 123/udp

protocols:

masquerade: no

forward-ports:

sourceports:

icmp-blocks:

rich rules:

“`

四、验证

开启NTP服务和防火墙相关设置后,我们需要测试是否成功实现了时钟同步。我们将以Ubuntu 18.04为例。

使用ntpdate命令可以检查本机是否在NTP服务器上拉取时间。

1. 安装ntpdate

“`

sudo apt-get install ntpdate

“`

2. 在终端中使用ntpdate命令获取时间

“`

sudo ntpdate 3.cn.pool.ntp.org

“`

若显示以下信息,则表示成功获取UTC时间:

“`

server 183.230.40.26, stratum 2, offset 4.912023, delay 0.04730

date 2023-12-18 12:45:06 UTC

“`

至此,我们已经成功在Linux上配置了NTP服务器,并防火墙实现了时钟同步。

相关问题拓展阅读:

NTP服务器未联网,怎么配置

二、配置ntp服务器端

restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap noquery

restrict 192.168.166.0 mask 255.255.255.0 nomodify

restrict 127.0.0.1

server 210.72.145.44 prefer

server 127.127.1.0

fudge 127.127.1.0 stratum 8

driftfile /var/lib/ntp/drift

keys /etc/ntp/keys

说明:关于权限设定部分

权限的设定主要以 restrict 这个参数来设定,主要的语法为:

restrict

IP地址

mask

子网掩码

参数

其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP

参数有以下几个:

ignore :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询

注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!

三、查看NTP服务的运行状况

#watch ntpq -p

参数说明:

remote: 它指的就是本地机器所连接的远程

NTP服务器

refid: 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器

st: 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端. 所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.

t: 这个…..我也不知道啥意思^_^

when: 我个人把它理解为一个

计时器

用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步

poll: 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小

reach: 这是一个

八进制

值,用来测试能否和服务器连接.每成功连接一次它的值就会增加

delay: 从本地机发送同步要求到服务器的round trip time

offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近

jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的

绝对值

越小我们和服务器的时间就越精确

四、客户端配置

ntpdate 192.168.166.100

LINUX做为客户端自动同步时间

如果想定时进行时间校准,可以使用crond服务来定时执行。

编辑 /etc/crontab 文件

加入下面一行:

30 8 * * * root /usr/in/ntpdate 192.168.166.100; /in/hwclock -w #192.168.0.1是NTP服务器的IP地址

然后重启crond服务

service crond restart

这样,每天 8:30 Linux 系统就会自动的进行网络时间校准。

WINDOWS 需要打开windows time服务和RPC的二个服务

如果在打开windows time 服务,时报 错误1058,进行下面操作

1.运行 cmd 进入

命令行

,然后键入

w32tm /register 进行注册

正确的响应为:W32Time 成功注册。

2.如果上一步正确,用 net start “windows time” 或 net start w32time 启动服务。

五、报错说明

当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:

错误1.Server dropped: Strata too high

在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。

在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。

这是因为NTP server还没有和其自身或者它的server同步上。

以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

server 127.127.1.0 fudge

127.127.1.0 stratum 8

在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

# watch ntpq -p

出现画面:

Every 2.0s: ntpq -p Thu Jul 10 02:28:

remote refid st t when poll reach delay offset jitter

==============================================================================

192.168.30.22 LOCAL(0) 8 u.. 0.001

LOCAL(0) LOCAL(0) 10 l.000 0.000 0.001

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。

错误2.Server dropped: no data

从客户端执行netdate –d时有错误信息如下:

tranit(192.168.30.22)

tranit(192.168.30.22)

tranit(192.168.30.22)

tranit(192.168.30.22)

tranit(192.168.30.22)

192.168.30.22: Server dropped: no data

server 192.168.30.22, port 123

…..

28 Jul 17:42:24 ntpdate: no server suitable for synchronization found

出现这个问题的原因可能有2:

1.检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:

# ntpq -c version

下面是来自ntp官方网站的说明:

The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant “Don’t trust this host/subnet for time”.

In 4.2 (and later) notrust means “Ignore all NTP packets that are not cryptographically authenticated.” This forces remote time servers to authenticate themselves to your (client) ntpd

解决:

把notrust去掉。

2.检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。

可以用命令

#iptables INPUT -p udp -m udp –dport 123 -j ACCEPT

如果觉得麻烦就直接把防火墙停掉

#service iptables stop

来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

修改配置文件/etc/ntp.conf,把里面默认的server设置都注释掉,添加本地环回地址为server,如下:

#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 127.127.1.0

启动系统ntpd服务后使用ntpstat查看状态。如果显示“synchronised to local net at stratum 6”,表示启动成功。

关于linux ntp配置 防火墙的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » 如何在Linux上配置NTP服务器并防火墙实现时钟同步 (linux ntp配置 防火墙)