Linux时间同步方案:ntp服务配置详解 | linux-tutorial

Linux时间同步方案:ntp服务配置详解 | linux-tutorial

【免费下载链接】linux-tutorial :penguin: Linux教程,主要内容:Linux 命令、Linux 系统运维、软件运维、精选常用Shell脚本 【免费下载链接】linux-tutorial 项目地址: https://gitcode.com/GitHub_Trending/lin/linux-tutorial

一、NTP 简介

网络时间协议(Network Time Protocol,缩写:NTP)是在数据网络中实现计算机时钟同步的网络协议,位于 OSI 模型的应用层。NTP 旨在将参与计算机的协调世界时(UTC)同步到毫秒级误差范围内,是目前仍在使用的最古老互联网协议之一。

NTP 核心要点:

  • 中国本地时间为 UTC+8 时区
  • Linux 系统同时维护系统时间(1970/01/01 起计数)和 BIOS 硬件时间
  • NTP 服务通过 UDP 123 端口通信
  • 时区配置文件位于 /usr/share/zoneinfo/,本地时区由 /etc/localtime 指定
  • NTP 服务器层级结构中,ntpd 服务与 ntpdate 命令不可同时使用
  • 时间同步状态可通过 ntpstatntpq -p 命令查询

二、ntpd 服务部署

2.1 安装与基础配置

安装 NTP 服务(以 CentOS 为例):

yum -y install ntp

配置文件路径/etc/ntp.conf,核心配置参考:

# 权限控制
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

# 国内 NTP 服务器配置
server cn.pool.ntp.org prefer
server ntp1.aliyun.com
server ntp.sjtu.edu.cn

# 时间差异分析文件
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

2.2 防火墙配置

NTP 使用 UDP 123 端口,需开放防火墙规则:

firewalld 配置

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

iptables 配置

iptables -A INPUT -p UDP --dport 123 -j ACCEPT
service iptables save

2.3 服务管理命令

systemctl enable ntpd.service  # 开机自启
systemctl start ntpd.service   # 启动服务
systemctl restart ntpd.service # 重启服务
systemctl status ntpd.service  # 查看状态

2.4 同步状态验证

检查同步状态

ntpstat
# 成功输出示例:
# synchronised to NTP server (5.79.108.34) at stratum 3
#    time correct to within 1129 ms
#    polling server every 64 s

查看服务器连接状态

ntpq -p
# 输出示例:
#      remote           refid      st t when poll reach   delay   offset  jitter
# ==============================================================================
# *ntp1.aliyun.com  10.137.53.7      2 u   33   64  377   25.930   15.908   3.168
#  cn.pool.ntp.org  130.133.1.10     2 u   36   64  367  230.801    5.271   2.791

三、ntpdate 命令行同步

3.1 手动同步方法

ntpdate 是轻量级时间同步工具,适合临时校准:

ntpdate cn.pool.ntp.org
# 输出示例:
# 11 Feb 10:47:12 ntpdate[30423]: step time server 84.16.73.33 offset -49.894774 sec

3.2 定时同步配置

通过 crontab 设置周期性同步(需先停止 ntpd 服务):

systemctl stop ntpd
echo "0 3 * * * /usr/sbin/ntpdate cn.pool.ntp.org" >> /etc/crontab
systemctl restart crond

四、国内优质 NTP 服务器列表

cn.pool.ntp.org    # NTP公共池中国节点
ntp1.aliyun.com    # 阿里云主节点
ntp2.aliyun.com    # 阿里云备用节点
ntp.sjtu.edu.cn    # 上海交通大学
s1a.time.edu.cn    # 北京邮电大学
s1b.time.edu.cn    # 清华大学
s1c.time.edu.cn    # 北京大学
edu.ntp.org.cn     # 中国教育网总节点

五、系统时间维护工具

5.1 时间相关命令

date                          # 查看当前系统时间
hwclock --show                # 查看硬件时钟
hwclock -w                    # 将系统时间同步到硬件时钟
timedatectl set-timezone Asia/Shanghai  # 设置时区为上海

5.2 系统时间配置脚本

项目提供的时间同步脚本:系统时间同步脚本

六、常见问题解决

6.1 服务启动失败

错误现象ntpd 服务启动后自动停止
解决方法:检查系统时间与 NTP 服务器差异是否超过 1000 秒,可先用 ntpdate 手动校准后再启动服务:

ntpdate cn.pool.ntp.org
systemctl start ntpd

6.2 同步精度不足

优化方案

  1. 增加服务器数量(建议配置 3-5 个不同源服务器)
  2. 调整 driftfile 路径权限:chmod 644 /var/lib/ntp/drift
  3. 配置防火墙允许 NTP 响应包通过

参考资料

【免费下载链接】linux-tutorial :penguin: Linux教程,主要内容:Linux 命令、Linux 系统运维、软件运维、精选常用Shell脚本 【免费下载链接】linux-tutorial 项目地址: https://gitcode.com/GitHub_Trending/lin/linux-tutorial

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值