木头虫在家

  • 系统
  • 网络
  • 技术
  • 安全
  • 运维
  • 应用
木头虫在家
蜘蛛网
  1. 首页
  2. 技术
  3. 正文

Linux配置NTP服务器的详细说明

2024年7月24日 141点热度 0人点赞 0条评论

NTP 简介

网络时间协议(英语:Network Time Protocol,缩写:NTP)是在数据网络潜伏时间可变的计算机系统之间通过分组交换进行时钟同步的一个网络协议,位于 OSI 模型的应用层。NTP 意图将所有参与计算机的协调世界时(UTC)时间同步到几毫秒的误差内。

NTP 要点:

  • 地球共有 24 个时区,而以格林威治时间 (GMT) 为标准时间;
  • 中国本地时间为 GMT +8 小时;
  • 最准确的时间为使用原子钟 (Atomic clock) 所计算的,例如 UTC (Coordinated Universal Time) 就是一例;
  • Linux 系统本来就有两种时间,一种是 Linux 以 1970/01/01 开始计数的系统时间,一种则是 BIOS 记载的硬件时间;
  • Linux 可以透过网络校时,最常见的网络校时为使用 NTP 服务器,这个服务启动在 udp port 123;
  • 时区档案主要放置于 /usr/share/zoneinfo/ 目录下,而本地时区则参考 /etc/localtime;
  • NTP 服务器为一种阶层式的服务,所以 NTP 服务器本来就会与上层时间服务器作时间的同步化, 因此 nptd 与 ntpdate 两个指令不可同时使用;
  • NTP 服务器的联机状态可以使用 ntpstat 及 ntpq -p 来查询;
  • NTP 提供的客户端软件为 ntpdate 这个指令;
  • 在 Linux 下想要手动处理时间时,需以 date 设定时间后,以 hwclock -w 来写入 BIOS 所记录的时间。
  • NTP 服务器之间的时间误差不可超过 1000 秒,否则 NTP 服务会自动关闭。

 

在开始安装NTP服务前,建议最好先查看下本机系统的时区信息,并且保证所有关联的设备时区信息保持一致。
[root@localhost ~]# timedatectl status
Local time: 三 2024-07-24 10:25:29 CST
Universal time: 三 2024-07-24 02:25:29 UTC
RTC time: 三 2024-07-24 02:25:29
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

 

ntpd 服务安装

CentOS 安装 NTP 很简单,执行以下命令即可:

yum -y install ntp

ntpd 配置

ntp 的配置文件路径为: /etc/ntp.conf ,参考配置:

# 1. 先处理权限方面的问题,包括放行上层服务器以及开放区网用户来源:
# restrict default kod nomodify notrap nopeer noquery     # 拒绝 IPv4 的用户
# restrict -6 default kod nomodify notrap nopeer noquery  # 拒绝 IPv6 的用户
restrict default nomodify notrap nopeer noquery
restrict 192.168.10.0 mask 255.255.255.0 nomodify # 放行同局域网来源(根据网关和子网掩码决定)
restrict 127.0.0.1   # 默认值,放行本机 IPv4 来源
restrict ::1         # 默认值,放行本机 IPv6 来源
 # restrict IP mask netmask_IP parameter

 

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

# 其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0

# 至于 paramter 则有:

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

# nomodify:表示 Client 端不能更改 Server 端的时间参数,不过,

# Client 端仍然可以透过 Server 端来进行网络校时。

# notrust :该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域

# noquery :不提供 Client 端的时间查询

# notrap :不提供trap这个远程事件登入

# 如果 paramter 完全没有设定,那就表示该 IP (或网域)“没有任何限制”

 

​ # 2. 设定 NTP 主机来源

注释掉默认 NTP 来源

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

设置国内 NTP 来源

server cn.pool.ntp.org prefer # 以这个主机为优先

server ntp1.aliyun.com iburst ​ #

注意:如果更改配置,必须重启 NTP 服务(systemctl restart ntpd)才能生效。

放开防火墙限制

NTP 服务的端口是 123,使用的是 udp 协议,所以 NTP 服务器的防火墙必须对外开放 udp 123 这个端口。

如果防火墙使用 iptables,执行以下命令:

iptables -A INPUT -p UDP -i eth0 -s 192.168.0.0/24 --dport 123 -j ACCEPT

如果防火墙使用 firewalld,执行以下命令:

firewall-cmd --zone=public --add-port=123/udp --permanent

ntpd 服务命令

systemctl enable ntpd.service  # 开启服务(开机自动启动服务)
systemctl disable ntpd.service # 关闭服务(开机不会自动启动服务)
systemctl start ntpd.service   # 启动服务
systemctl stop ntpd.service    # 停止服务
systemctl restart ntpd.service # 重启服务
systemctl reload ntpd.service  # 重新载入配置
systemctl status ntpd.service  # 查看服务状态

查看 ntp 服务状态

验证 NTP 服务正常工作

执行 ntpstat 可以查看 ntp 服务器有无和上层 ntp 连通,,如果成功,可以看到类似以下的内容:

$ ntpstat
synchronised to NTP server (5.79.108.34) at stratum 3
   time correct to within 1129 ms
   polling server every 64 s
查看 ntp 服务器与上层 ntp 的状态
ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp1.ams1.nl.le 130.133.1.10     2 u   36   64  367  230.801    5.271   2.791
 120.25.115.20   10.137.53.7      2 u   33   64  377   25.930   15.908   3.168
 time.cloudflare 10.21.8.251      3 u   31   64  367  251.109   16.976   3.264

三、ntpdate 命令

注意:NTP 服务器为一种阶层式的服务,所以 NTP 服务器本来就会与上层时间服务器作时间的同步化, 因此 nptd 与 ntpdate 两个指令不可同时使用。

手动执行时间同步

ntpdate 命令是 NTP 的客户端软件,它可以用于请求时间同步。

语法:

/usr/sbin/ntpdate <ntp_server>

ntp_server 可以从 国内 NTP 服务器 中选择。

示例:

$ ntpdate cn.pool.ntp.org
11 Feb 10:47:12 ntpdate[30423]: step time server 84.16.73.33 offset -49.894774 sec

自动定时同步时间

如果需要自动定时同步时间,可以利用 Crontab 工具。本质就是用 crontab 定时执行一次手动时间同步命令 ntp。

示例:执行如下命令,就可以在每天凌晨 3 点同步系统时间:

echo "0 3 * * * /usr/sbin/ntpdate cn.pool.ntp.org" >> /etc/crontab 
# 修改 crond 服务配置
systemctl restart crond # 重启 crond 服务以生效

四、国内 NTP 服务器

以下 NTP 服务器搜集自网络:

cn.pool.ntp.org  # 最常用的国内NTP服务器
cn.ntp.org.cn    # 中国
edu.ntp.org.cn   # 中国教育网
ntp1.aliyun.com  # 阿里云

Views: 7
标签: linux ntp
最后更新:2024年7月24日

admin

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >
分类
  • 值的推
  • 域名主机
  • 安全
  • 应用
  • 技术
  • 未分类
  • 系统
  • 网络
  • 运维
最新 热点 随机
最新 热点 随机
ucloud香港云主机 Windows 11访问局域网共享文件时出现报错 “出现了扩展错误”解决办法 4s店修理工秘密,车开到报废,这几种东西不必换 联想K4e-ITL重装WIN系统蓝屏解决方案 上学出路在哪里 windows远程桌面中如何发送CTRL+ALT+DEL命令 linux查看远程文件保存到本地,复制一个文件到新文件未尾命令 电脑分区盘符不见了怎么找回 Windows系统通过命令行工具启用禁用网卡 攻防演练之企业网络安全体系建设历程
Windows 11访问局域网共享文件时出现报错 “出现了扩展错误”解决办法ucloud香港云主机
Junos的Commit 定时提交命令 金叉就搞 win10任务计划里定时任务尚未运行(0x41303) BT宝塔https ssl证书Let's Encrypt不会自动续签 win7任务栏右下角图标空白解决办法(表现为占用了图标位置,却不能显示出来,也点击不了) 一份freebsd内核优化参数表 Windows 网卡驱动黄色叹号 【Windows 仍在设置此设备的类配置。 (代码 56)】 由于无法验证发行者,所以WINDOWS已阻止该软件 juniper 日常操作命令 Chrome浏览器“Adobe Flash Player 因过期而遭到阻止”的解决方法

COPYRIGHT © 2025 blog.qmun.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备11036795号