Centos 7部署NTP时间服务器

NTP(Network Time Protocol)网络时间协议基于UDP,用于网络时间同步的协议,使网络中的计算机时钟同步到UTC,再配合各个时区的偏移调整就能实现精准同步对时功能。

实验环境:

OS:Linux Centos 7.4 x86_64

1.查看当前服务器时区&列出时区并设置时区(如已是正确时区,请略过):

# timedatectl# timedatectl list-timezones# timedatectl set-timezone Asia/Shanghai

2.时间时区概念理解:

GMT、UTC、CST、DST

UTC:

整个地球分为二十四时区,每个时区都有自己的本地时间,在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC:Universal Time Coordinated)。

GMT:

格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线(UTC与GMT时间基本相同)。

CST:

中国标准时间 (China Standard Time)

GMT + 8 = UTC + 8 = CST

DST:

夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用(中国不使用)。

2.使用 NTP 公共时间服务器池(NTP Public Pool Time Servers)http://www.pool.ntp.org 同步你的服务器时间。

查看是否安装:

# rpm -q ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64

如已安装请略过此步,否则请执行以下命令安装:

# yum install ntpdate ntp -y

修改NTP配置:

注:绿色为原有我注释掉的内容,红色为新增代替之前注释内容,其他默认。

# vim /etc/ntp.conf# For more information about this file, see the man pages# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

#新增:日志目录.
logfile /var/log/ntpd.log

# Permit time synchronization with our time source, but do not# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could# be tightened as well, but to do so would effect some of# the administrative functions.
restrict 127.0.0.1
restrict ::1
#这一行的含义是授权172.16.128.0网段上的所有机器可以从这台机器上查询和同步时间.
restrict 172.16.128.0 mask 255.255.255.0 nomodify notrap

# Hosts on local network are less restricted.#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap# Use public servers from the pool.ntp.org project.# Please consider joining the pool (http://www.pool.ntp.org/join.html).#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 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst

#新增:当外部时间不可用时,使用本地时间.
server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10

#broadcast 192.168.1.255 autokey # broadcast server#broadcastclient # broadcast client#broadcast 224.0.1.1 autokey # multicast server#multicastclient 224.0.1.1 # multicast client#manycastserver 239.255.254.254 # manycast server#manycastclient 239.255.254.254 autokey # manycast client#新增:允许上层时间服务器主动修改本机时间.
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery

# Enable public key cryptography.#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.#trustedkey 4 8 42# Specify the key identifier to use with the ntpdc utility.#requestkey 8# Specify the key identifier to use with the ntpq utility.#controlkey 8# Enable writing of statistics records.#statistics clockstats cryptostats loopstats peerstats# Disable the monitoring facility to prevent amplification attacks using ntpdc# monlist command when default restrict does not include the noquery flag. See# CVE-2013-5211 for more details.# Note: Monitoring will not be disabled with the limited restriction flag.disable monitor

3.设置系统开机自启动:

# systemctl enable ntpd# systemctl enable ntpdate# systemctl is-enabled ntpd

在ntpd服务启动时,先使用ntpdate命令同步时间:

# ntpdate -u 1.cn.pool.ntp.org

原因之一:当服务端与客户端之间的时间误差过大时,此时修改时间可能对系统或应用带来不可预知问题,NTP会停止时间同步。若检查NTP启动后时间未同步时,应考虑可能是时间误差过大所致,此时需要先手动进行时间同步。

启动NTP服务器:

# systemctl start ntpdate# systemctl start ntpd

4.加入防火墙:

# firewall-cmd --permanent --add-service=ntp# firewall-cmd --reload

5.查看ntp连接状态如果没有问题,将正确时间写入硬件:

# ss -tlunp | grep ntp# ntpq -p# hwclock -w

5.1.系统时间与硬件时间

硬件时间:

RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。

系统时间:

一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

6.客户端:

以服务进程方式实时同步(需安装NTP):

# vim /etc/ntp.conf
server 172.16.128.171

重要:修改任意节点服务器的NTP配置文件都需要重起ntpd服务:

# systemctl restart ntpd

以crontab任务计划同步时间(需安装ntpdate,每天24点更新同步时间):

# crontab -e
0 0 * * * /usr/sbin/sntp -P no -r 172.16.128.171;hwclock -w

已部署完毕。这样集群会自动定期进行服务的同步,如此以来集群的时间就保持一致了。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/219844.html<

(0)
运维的头像运维
上一篇2025-04-14 07:23
下一篇 2025-04-14 07:24

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注