NTP配置实践

简介:

前言

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。不管是平时使用的私人计算机还是在工作中搭建的服务器集群。时间的统一性和准确性是十分重要的。

本文以自己公司的NTP配置实践过程为例,过程本身并不复杂,原理部分请參考扩展阅读。

时间同步真的非常重要


更新历史

2015年06月26日 - 初稿

阅读原文 - http://wsgzao.github.io/post/ntp/

扩展阅读


准备工作

检查NTP是否安装

假设缺少NTP服务,请从官方下载适合自己系统的版本号

#检查rpm包 
rpm -qa | grep ntp

ntp-4.2.4p8-1.16.1
yast2-ntp-client-2.17.14-1.10.14

手动改动时间

选择正确的时区,控制误差在5分钟以内

#查看当前时间
date

20081212日 星期五 14:44:12 CST
Fri Jun 26 14:51:37 CST 2015

#手动改动时间(月/日/年时:分:秒)
date -s "1/1/09 00:01"

20090101日 星期四 00:01:00 CST

#手动改动分秒
date -s 21:21

NTP配置

NTP服务端配置

服务端时间来源选择上海交通大学网络中心NTP服务器地址

#NTP服务端系统版本号
cat /etc/SuSE-release 

SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
#编辑ntp配置文件
vi /etc/ntp.conf

server 202.120.2.101 prefer
#server ntp.sjtu.edu.cn


#增加自己主动启动
chkconfig ntp on

#启动ntp服务
service ntp start
service ntp status
service ntp stop

完整的/etc/ntp.conf配置文件例如以下

################################################################################
## /etc/ntp.conf
##
## Sample NTP configuration file.
## See package 'ntp-doc' for documentation, Mini-HOWTO and FAQ.
## Copyright (c) 1998 S.u.S.E. GmbH Fuerth, Germany.
##
## Author: Michael Andres,  <ma@suse.de>
##         Michael Skibbe,  <mskibbe@suse.de>
##
################################################################################

##
## Radio and modem clocks by convention have addresses in the 
## form 127.127.t.u, where t is the clock type and u is a unit 
## number in the range 0-3. 
##
## Most of these clocks require support in the form of a 
## serial port or special bus peripheral. The particular  
## device is normally specified by adding a soft link 
## /dev/device-u to the particular hardware device involved, 
## where u correspond to the unit number above. 
## 
## Generic DCF77 clock on serial port (Conrad DCF77)
## Address:     127.127.8.u
## Serial Port: /dev/refclock-u
##  
## (create soft link /dev/refclock-0 to the particular ttyS?)
##
# server 127.127.8.0 mode 5 prefer
server 202.120.2.101 prefer

##
## Undisciplined Local Clock. This is a fake driver intended for backup
## and when no outside source of synchronized time is available.
##
server 127.127.1.0 
# local clock (LCL)
fudge 127.127.1.0  stratum 10
# LCL is unsynchronized

##
## Add external Servers using
## # rcntp addserver <yourserver>
## 

##
## Miscellaneous stuff
##

driftfile /var/lib/ntp/drift/ntp.drift  
# path for drift file

logfile /var/log/ntp             
# alternate log file
# logconfig =syncstatus + sysevents
# logconfig =all

# statsdir /tmp/                # directory for statistics files
# filegen peerstats  file peerstats  type day enable
# filegen loopstats  file loopstats  type day enable
# filegen clockstats file clockstats type day enable

#
# Authentication stuff
#
keys /etc/ntp.keys               
# path for keys file
trustedkey 1                     
# define trusted keys
requestkey 1                     
# key (7) for accessing server variables
# controlkey 15                 # key (6) for accessing server variables

NTPclient配置

如无特殊要求,一般使用crontab计划任务同步时间

1.crontab计划任务方式配置

crontab -e

0 0 * * * /usr/sbin/sntp -P no -r  172.16.0.3;hwclock -w

2.以服务进程方式实时同步

#编辑client配置文件
vi /etc/ntp.conf

server 198.16.2.135

#启动服务
chkconfig ntp on
service ntp restart

#假设相差时间较长,请先手动同步
sntp -P no -r 198.16.2.135
ntpdate –d 198.16.2.135

检查NTP同步状态

NTP协议为UDP端口是123,offset时间偏移量以毫秒为单位,注意防火墙的控制策略

#查看服务连接和监听
ntp:~ # netstat -tlunp | grep ntp  
udp        0      0 198.17.1.224:123        0.0.0.0:*                           28916/ntpd          
udp        0      0 198.16.2.224:123        0.0.0.0:*                           28916/ntpd          
udp        0      0 127.0.0.2:123           0.0.0.0:*                           28916/ntpd          
udp        0      0 127.0.0.1:123           0.0.0.0:*                           28916/ntpd          
udp        0      0 0.0.0.0:123             0.0.0.0:*                           28916/ntpd          
udp        0      0 ::1:123                 :::*                                28916/ntpd          
udp        0      0 fe80::250:56ff:feb9:123 :::*                                28916/ntpd          
udp        0      0 fe80::250:56ff:feb9:123 :::*                                28916/ntpd          
udp        0      0 :::123                  :::*                                28916/ntpd          

#查看网络中的NTP服务器。同一时候显示client和每一个服务器的关系
ntp:~ # ntpq -p  
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*202.120.2.101   202.118.1.81     3 u    5 1024  377  265.748   -2.819  16.138

LOCAL(0) .LOCL. 10 l 34 64 377 0.000 0.000 0.001





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5241198.html,如需转载请自行联系原作者

相关文章
|
2月前
|
自然语言处理 开发者
拿下30个第1名的腾讯混元翻译模型,开源!
腾讯混元又带来一个在国际机器翻译比赛拿下30个第1名的翻译模型Hunyuan-MT-7B,它总参数量仅7B,支持33个语种、5种民汉语言/方言互译,是一个能力全面的轻量级翻译模型。
418 0
|
缓存 前端开发 应用服务中间件
【Nginx】图片显示过慢,文件下载不完全,竟然是Nginx的锅!!
最近,一名读者跟我说他通过浏览器访问自己的服务器时,图片显示的非常慢,以至于在浏览器中都无法完全加载出来,下载文件时,更是恼火,文件根本就无法完全下载下来。而且奇怪的是这位读者所在的网络是没啥问题的。于是,我便开始帮他排查各种问题。。。
1317 0
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
5756 0
|
消息中间件 数据安全/隐私保护
RabbitMQ 清除全部队列及消息
安装RabbitMQ后可访问:http://{rabbitmq安装IP}:15672使用(默认的是帐号guest,密码guest。此账号只能在安装RabbitMQ的机器上登录,无法远程访问登录。) 远程访问登录,可以使用自己创建的帐号,给与对应的管理员权限即可。
1492 0
|
算法 分布式数据库 分布式计算
|
6月前
|
自然语言处理 前端开发 IDE
用通义灵码全新智能体+MCP实现从设计稿到前端代码,个人免费用
通义灵码全新升级,发布国内首个支持“自主决策+工具链闭环”的编程智能体,面向个人免费!新增功能包括智能体模式、混合推理模型Qwen3支持、全面集成MCP中文社区(涵盖2400+服务)及长期记忆能力。用户可通过IDE插件使用,兼容主流开发环境如JetBrains、VS Code和Visual Studio。教程展示如何将MasterGo设计稿转化为前端代码,简化开发流程。探索链接:[通义灵码官网](https://lingma.aliyun.com/)。
|
监控 安全 Linux
CentOS7下安装配置ntp服务的方法教程
通过以上步骤,您不仅能在CentOS 7系统中成功部署NTP服务,还能确保其配置合理、运行稳定,为系统时间的精确性提供保障。欲了解更多高级配置或遇到特定问题,提供了丰富的服务器管理和优化资源,可作为进一步学习和求助的平台。
2041 1
|
10月前
|
程序员 API 开发者
实战阿里qwen2.5-coder 32B,如何配置Cline的Ollama API接口。
阿里Qwen2.5大模型开源免费,适合编程应用。在Ollama平台下载时,推荐选择带有“cline”字样的Qwen2.5-Coder版本,仅需额外下载适配文件,无需重复下载模型文件。Ollama环境永久免费,配置简单,效果出色,适合开发者使用。
5205 77
|
关系型数据库 MySQL Linux
Docker安装Mysql5.7,解决无法访问DockerHub问题
当 Docker Hub 无法访问时,可以通过配置国内镜像加速来解决应用安装失败和镜像拉取超时的问题。本文介绍了如何在 CentOS 上一键配置国内镜像加速,并成功拉取 MySQL 5.7 镜像。
2074 3
Docker安装Mysql5.7,解决无法访问DockerHub问题