一:ntp服务器详情
服务器型号
系统版本
IP地址
主机名
ntp服务版本
虚拟机8c-32g-1T
Ubuntu22.04
10.20.30.2
DMZ-NTP-SERVER
4.2.8p15
二:ntp服务端部署配置脚本
#!/bin/bash # 脚本信息 echo "----------------------------------------" echo "NTP 服务器安装脚本" echo "----------------------------------------" # 更新软件包列表 echo "正在更新软件包列表..." sudo apt update # 安装 NTP 服务,避免交互提示 echo "正在安装 NTP 服务..." sudo DEBIAN_FRONTEND=noninteractive apt install -y ntp # 备份原配置文件 echo "正在备份原配置文件 ntp.conf..." sudo cp /etc/ntp.conf /etc/ntp.conf.bak # 配置 NTP 服务器 echo "正在配置 NTP 服务器..." sudo bash -c 'cat > /etc/ntp.conf << EOF # 使用来自 pool.ntp.org 项目的公共服务器 server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst # 允许本地网络中的机器与 NTP 服务器同步 restrict default nomodify notrap nopeer noquery restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 请根据您的内部网络进行适当修改 # 启用日志记录 logfile /var/log/ntp.log EOF' # 确保配置文件没有多余的空格或隐藏字符 sudo sed -i 's/[[:space:]]*$//' /etc/ntp.conf # 删除行尾的空格 sudo sed -i '/^$/d' /etc/ntp.conf # 删除空行 # 启动 NTP 服务 echo "正在启动 NTP 服务..." sudo systemctl start ntp sudo systemctl enable ntp # 显示服务状态 echo "NTP 服务状态:" sudo systemctl status ntp # 提示用户在其他服务器上配置 NTP 客户端 echo "----------------------------------------" echo "要配置其他服务器,请将此 NTP 服务器的 IP 地址添加到 /etc/ntp.conf:" echo "server <NTP_SERVER_IP> iburst" echo "最后,在客户端上重启 NTP 服务。"
三:ntp服务维护相关命令
查看服务状态:systemctl status ntp.service
重启服务:systemctl restart ntp.service 关闭服务:systemctl stop ntp.service 启动服务:systemctl start ntp.service
查看日志:tail -20f /var/log/ntp.log 或者 查看服务的系统日志:journalctl -u ntp
查看或者编辑配置文件:cat /etc/ntp.conf
四:客户端安装配置测试ntp服务
(1)提前保证要配置 ntp 服务的机器和10.20.30.2 ntp 服务端网络且端口互通:
telnet 10.20.30.2 123
(2)在客户端机器上安装 ntp 服务:
sudo apt update sudo apt install -y ntp
(3)配置ntp客户端,编辑 ntp 客户端的配置文件 /etc/ntp.conf,将 ntp 服务端的 IP 地址添加到配置文件中:
sudo vim /etc/ntp.conf并添加以下行:server 10.20.30.2 iburst
(4)完成配置后,重启客户端 ntp 服务使配置生效:
sudo systemctl restart ntp
(5)测试 ntp 时间同步是否生效,使用
ntpq
命令检查 ntp 同步状态。运行以下命令:ntpq -p
此命令会列出已配置的 ntp 服务器及其状态。如果配置正确应该能够看到 ntp 服务器的条目以及它的响应时间。会看到类似于以下的输出(10.20.30.2 202.112.29.82 2 u 1 64 1 0.262 +25.187 1.040 这一列代表配置成功,但是还需要测试时间同步是否成功):
root@DMZ-DNS-SERVER:~# ntpq -premote refid st t when poll reach delay offset jitter ==============================================================================0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0001.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0002.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0003.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.000ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 +0.000 0.00010.20.30.2 202.112.29.82 2 u 1 64 1 0.262 +25.187 1.040ntp1.flashdance 194.58.202.20 2 u 1 64 1 186.576 +5.922 1.366dns2.synet.edu. 202.118.1.48 2 u 1 64 1 33.411 +17.579 0.000electrode.felix 131.188.3.221 2 u 1 64 1 269.660 -17.697 1.204ntp5.flashdance 194.58.202.20 2 u 2 64 1 189.559 +4.068 0.000ntp.wdc2.us.lea 130.133.1.10 2 u 1 64 1 333.790 -17.704 0.000ntp8.flashdance 194.58.202.148 2 u 1 64 1 216.010 +14.262 1.806119.28.183.184 100.122.36.196 2 u 2 64 1 57.018 +18.580 0.852185.125.190.58 79.243.60.50 2 u 4 64 1 279.504 +36.708 0.000time.cloudflare 10.191.8.4 3 u 2 64 1 200.383 +17.699 0.000185.125.190.56 17.253.28.123 2 u 1 64 1 460.684 -39.013 0.000
列名释义:
remote 列显示了当前连接的 NTP 服务器,包括
10.20.30.2
。refid 列显示了该 NTP 服务器的参考时钟(在您的情况下是
202.112.29.82
)。st 列表示服务器的层级(
2
表示这是一个二级 NTP 服务器)。reach 列显示了与该 NTP 服务器的连接状态,值为
1
表示最近的请求已经成功接收。delay、offset 和 jitter 列显示了网络延迟、时间偏移和抖动等信息。
(6)检查时间是否同步,可以通过以下命令查看当前时间:
date
然后,等待一段时间(通常是几分钟),ntp 客户端会自动与ntp服务端进行同步。再次运行
ntpq -p
查看 ntp 同步状态,直到 reach 列显示为377
(即所有请求的回复都成功)。root@DMZ-DNS-SERVER:~# ntpq -premote refid st t when poll reach delay offset jitter ==============================================================================0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0001.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0002.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.0003.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 +0.000 0.000ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 +0.000 0.000 +10.20.30.2 203.107.6.88 3 u 116 128 377 0.287 -14.026 2.050 *dns2.synet.edu. 202.118.1.48 2 u 69 128 377 32.561 -13.713 1.781 -ntp8.flashdance 194.58.202.148 2 u 65 256 377 193.624 -21.543 4.956 +119.28.183.184 100.122.36.196 2 u 231 128 366 55.292 -9.522 3.699 -101.43.66.2 202.112.29.82 2 u 9 128 377 37.396 -12.486 3.318
(7)确保防火墙未阻止 ntp 流量
如果无法同步,请确认防火墙未阻止 ntp 流量。默认情况下,ntp 使用 udp 123 端口。需要在 ntp 服务器上检查防火墙规则:
sudo ufw status
如果 ntp 服务被阻止,则可以通过以下命令允许 ntp 流量:
sudo ufw allow ntp
(8)备注:自建的ntp还是依赖于阿里云的1级ntp服务器,我们这里属于2级ntp服务器
ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com
ubuntu22.04系统如何自建2级ntp服务器
2025/3/1 12:14:11
来源:https://blog.csdn.net/qq_41905051/article/details/145884999
浏览:
次
关键词:ubuntu22.04系统如何自建2级ntp服务器
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com
-
prometheus+node_exporter+grafana监控K8S信息
-
IDEAPyCharm安装ProxyAI(CodeGPT)插件连接DeepSeek-R1教程
-
dify基础之prompts
热文排行
- 《警世贤文》摘抄:处人篇、受恩篇、宽人篇、听劝篇、劝善篇(多读书、多看报、少吃零食多睡觉)
- Vmess协议是什么意思? VLESS与VMess有什么区别?
- Android显示系统(08)- OpenGL ES - 图片拉伸
- `git restore` 和 `git checkout` 用于丢弃工作区的改动, `git switch` 和 `git checkout` 用来切换分支
- nccl 03 记 回顾:从下载,编译到调试 nccl-test
- 【CVE-2024-38077】核弹级Windows RCE漏洞如何自检并修复该漏洞(附批量漏洞检测工具及分析伪代码)
- windows11 ,ubuntu20.04双系统,ubuntu没有wifi的解决方式
- 【HW必备】用友NC-Cloud存在17处漏洞合集
- AD24设计步骤
- ctfshow-web入门-php特性(web132-web136)
最新新闻
- SQL自动发送邮件的方法有哪些?如何配置?
- AI技术:转变未来生活与工作的革命性力量
- Spring模块详解Ⅰ
- HandBrake for Mac v1.9.2 视频压缩及格式转换 汉化版 支持M、Intel芯片
- 在Ubuntu 上实现 JAR 包的自启动
- prometheus+node_exporter+grafana监控K8S信息
- 摄像头原始数据读取——gstreamer(gst_parse_launch)
- 基于Spring Boot的图书个性化推荐系统的设计与实现(LW+源码+讲解)
- 头条百度批量采集软件说明文档
- 使用 Visual Studio Code (VS Code) 开发 Python 图形界面程序
推荐新闻
- SQL自动发送邮件的方法有哪些?如何配置?
- AI技术:转变未来生活与工作的革命性力量
- Spring模块详解Ⅰ
- HandBrake for Mac v1.9.2 视频压缩及格式转换 汉化版 支持M、Intel芯片
- 在Ubuntu 上实现 JAR 包的自启动
- prometheus+node_exporter+grafana监控K8S信息
- 摄像头原始数据读取——gstreamer(gst_parse_launch)
- 基于Spring Boot的图书个性化推荐系统的设计与实现(LW+源码+讲解)
- 头条百度批量采集软件说明文档
- 使用 Visual Studio Code (VS Code) 开发 Python 图形界面程序