Linux 时间同步 -NTP 服务器

简介: 本文主要介绍如何配置 Linux 服务器集群的时间同步,技术实现上使用 NTP 服务器和客户端同步时间。
  • 基本概念

NTP 是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

在计算机的世界里,时间非常地重要

例如:对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,是按照 A 这台计算机的时间,还是按照 B 这台计算机的时间?

NTP 就是用来解决这个问题的,NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。

它的用途是把计算机的时钟同步到世界协调时 UTC,其精度在局域网内可达 0.1ms,在互联网上绝大多数的地方其精度可以达到 1-50ms。

它可以使计算机对其服务器或时钟源(如石英钟,GPS 等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。

  • 部署环境:

系统:centos-release-7-3.1611.el7.centos.x86_64
image.png
NTP Server 服务器 IP:192.168.111.129

NTP Client 客户端 IP:192.168.111.89

搭建 NTP 服务器

查看服务器是否安装 ntp

系统默认已安装 ntp/ntpdate;

rpm -qa |grep ntp
  • 安装 ntp 命令
    如未安装 ntp,执行如下命令安装 ntp

    yum install -y ntp
    
  • 修改 ntp 配置文件

#打开配置文件
vim /etc/ntp.conf
#注释如下信息
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#server 192.168.111.129
#Fudge 192.168.111.129 stratum 10
#注释掉无法访问的时间服务器
#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

image.png

重新启动 ntp 服务

systemctl restart ntpd

查询 ntp 服务服务状态

systemctl status ntpd

image.png

配置 ntp 服务开机自启动

systemctl enable ntpd

查询 ntp 是否同步

ntpq -p

image.png

开启防火墙 ntp 默认端口 udp 123

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

公网下,使用命令行同步网络时间

/usr/sbin/ntpdate -u ntp.api.bz

NTP 客户端配置

安装 ntp/ntpdate 跟上面的步骤一样

修改 ntp 配置文件
将上面的 NTP 服务器作为客户端同步 NTP 时间服务器

vim /etc/ntp.conf
#配置允许NTP Server时间服务器主动修改本机的时间
restrict 192.168.111.129 nomodify notrap noquery
#注释掉其他时间服务器
#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服务器
server 192.168.111.129


NTP 客户端配置

安装 ntp/ntpdate 跟上面的步骤一样

修改 ntp 配置文件
将上面的 NTP 服务器作为客户端同步 NTP 时间服务器

vim /etc/ntp.conf
#配置允许NTP Server时间服务器主动修改本机的时间
restrict 192.168.111.129 nomodify notrap noquery
#注释掉其他时间服务器
#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服务器
server 192.168.111.129

image.png

同步时间
与 NTP server 服务器同步一下时间:

ntpdate -u 192.168.111.129

能看到已经成功同步

image.png

重新启动 ntp 服务

systemctl restart ntpd
目录
相关文章
|
6天前
|
网络安全
构建 NTP 时间服务器
构建 NTP 时间服务器
21 2
|
6天前
|
存储 数据挖掘 Linux
服务器数据恢复—Linux操作系统网站服务器数据恢复案例
服务器数据恢复环境: 一台linux操作系统服务器上跑了几十个网站,服务器上只有一块SATA硬盘。 服务器故障: 服务器突然宕机,尝试再次启动失败。将硬盘拆下检测,发现存在坏扇区
|
13天前
|
存储 安全 Linux
离线Linux服务器环境搭建
【9月更文挑战第3天】在离线环境下搭建Linux服务器需按以下步骤进行:首先确定服务器用途及需求,准备安装介质与所需软件包;接着安装Linux系统并配置网络;然后设置系统基础参数,如主机名与时区;安装必要软件并配置服务;最后进行安全设置,包括关闭非必要服务、配置防火墙、强化用户认证及定期备份数据。整个过程需确保软件包的完整性和兼容性。
|
15天前
|
存储 监控 Linux
监控Linux服务器
详细介绍了如何监控Linux服务器,包括监控CPU、内存、磁盘存储和带宽的使用情况,以及使用各种系统监控工具如vmstat、iostat、sar、top和dstat来分析系统性能,并推荐了一些开源监控系统。
24 0
监控Linux服务器
|
15天前
|
监控 安全 网络协议
快速配置Linux云服务器
快速配置Linux云服务器
|
20天前
|
缓存 Linux 调度
Linux服务器如何查看CPU占用率、内存占用、带宽占用
Linux服务器如何查看CPU占用率、内存占用、带宽占用
58 0
|
21天前
|
Linux 网络安全 网络架构
如何处理在学校Linux连接不上服务器
如何处理在学校Linux连接不上服务器
33 0
|
Linux
linux 中NTP时间同步
There are tow pen source software related to the NTP in linux, The first is ntpdate, the second is ntpd.
983 0