CentOS7搭建NTP服务器及客户端同步时间

一、服务器配置

1、查看服务器、客户端操作系统版本

[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

2、查看服务器是否安装ntp,系统默认安装ntpdate;

[[email protected] ~]# rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch
ntp-4.2.6p5-28.el7.centos.x86_64

3、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;

yum install ntp ntpdate -y

4、查看ntp服务器状态,两条命令效果一样

[[email protected] ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)

[[email protected] ~]# service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)

6、修改配置文件,使该NTP服务器在不联网的情况下,使用本服务器的时间作为同步时间

vim /etc/ntp.conf

把如下四行代码注释掉

#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 127.127.1.0
fudge  127.127.1.0 stratum 0

配置后:

[[email protected] ~]# 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

# 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

# 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 127.127.1.0
fudge  127.127.1.0 stratum 0

#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

# 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.

7、启动ntp服务

systemctl start ntpd

service ntpd start

8、再次查看服务器状态是否配置成功

[[email protected] ~]# systemctl status ntpd

[[email protected] ~]# service ntpd status

9、查看是否同步

[[email protected] ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 20 64 7 0.000 0.000 0.000

10、设置开机启动

[[email protected] ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

11、设置防火墙,打开udp123端口

[[email protected] ~]# firewall-cmd --permanent --add-port=123/udp
success
[[email protected] ~]# firewall-cmd --reload
success

12、查看防火墙已打开端口

iptables -L -n

二、客户端配置

前5步与服务器一致

6、修改配置文件,将刚刚搭建好的NTP服务器作为客户端上游时间服务器

vim /etc/ntp.conf
#注释掉其他上游时间服务器
#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
#配置上游时间服务器为本地的ntpd Server服务器
server hadoop101.com
fudge hadoop101.com stratum 0

#配置允许上游时间服务器主动修改本机的时间
restrict hadoop101.com nomodify notrap noquery

配置后:

[[email protected] ~]# vi /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

# 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

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

restrict hadoop101.com nomodify notrap noquery

# 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 hadoop101.com
fudge hadoop101.com stratum 0

#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

# 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.

7、与本地ntpd Server同步一下

[[email protected] ~]# ntpdate -u hadoop101.com
15 Aug 11:33:35 ntpdate[8768]: adjust time server 192.168.1.101 offset 0.004621 sec

8、启动ntp服务

systemctl start ntpd

service ntpd start

9、设置开机启动

[[email protected] ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

10、查看状态

[[email protected] ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 hadoop101.com   .LOCL.           1 u   13   64    1    8.148   -2.581   0.000

参考:https://blog.csdn.net/hellboy0621/article/details/81903091

原文地址:https://www.cnblogs.com/MWCloud/p/11357136.html

时间: 2024-11-08 14:59:28

CentOS7搭建NTP服务器及客户端同步时间的相关文章

centos7 搭建ntp服务器

一.安装ntp服务 首先检查服务器上是否已安装ntp服务: [[email protected] /]rpm -qa | grep ntp ntp-4.2.6p5-22.el7.centos.2.x86_64 ntpdate-4.2.6p5-22.el7.centos.2.x86_64 如果有上述内容输出,测说明ntp服务已安装:否则需要安装ntp服务 [[email protected] /]yum install -y ntpd 安装有yum.rpm.源码安装等多种方式,本次安装使用yum安

Linux下安装ntp服务器及客户端同步

服务器端: 1.装包 略.... 2.修改ntp服务配置文件 配置文件位置:/etc/ntp.conf 修改内容: #设置与服务器时间同步的IP网段,以下内容即为192.168.x.x机器可与服务器同步 restrict 192.168.0.0 mask 255.255.0.0 #设置ntp服务器自身同步的服务器IP,127.127.1.0意为与服务器自身进行同步 #如果在最后添加prefer则表示此主机为优先同步 server 127.127.1.0 #这行是时间服务器的层次.设为0则为顶级,

centos7搭建svn服务器及客户端设置

centos7貌似预装了svn服务(有待确认),因此我们直接启动该服务即可 一.svn服务端配置(服务器IP假设为192.168.100.1) 步骤1:创建存放代码库目录 mkdir -p /var/svn 步骤2:启动svn服务 svnserve -d -r /var/svn 步骤3:创建代码库something [[email protected] ~]# cd /var/svn [[email protected] svn]# [[email protected] svn]# [[emai

CentOS6.5系统搭建NTP服务器

在进入到我们的主题之前首先我们可以简单了解一下这几个名词 Atomic Clock: 现在计算时间最准确的是使用 原子震荡周期 所计算的物理时钟(Atomic Clock),因此也被定义为标准时间(International Atomic Time) UTC(coordinated Universal Time): 协和标准时间 就是利用 Atomic Clock 为基准定义出来的正确时间 (世界统一时间,世界标准时间,国际协调时间) 硬件时钟: 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就

在隔离的局域网内部架设基于CentOS7的NTP服务器

局域网内部由于不能跟外部的时间服务器相连,在NTP服务器设置的时候若将NTP服务器本身作为根时间服务器,则需要进行如下设置: 以CentOS7系统为例 # >vim /etc/ntp.conf 将如下行注释掉 # 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

Windows操作系统下搭建Git服务器和客户端。

本文将介绍如何在Windows操作系统下搭建Git服务器和客户端.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配置,简单易用.客户端是采用的TortoiseGit工具,UI操作,省去输入命令的麻烦,对于windows用户来说更易于使用. 所需软件: Git服务器端: BONOBO GIT SERVER,下载最新版:http://bonobogitserver.com/ Git客户端: msysgit,下载最新

CentOS 7.4搭建DNS服务器实现主从同步

CentOS 7.4搭建DNS服务器实现主从同步相关概念:正向解析:将域名解析成IP地址反向解析:将IP地址解析成域名第一步:准备工作systemctl stop firewalld //关闭防火墙setenforce 0 //关闭selinuxyum install bind //安装DNS服务第二步:编辑配置文件1.编辑主配置文件vi /etc/named.conf //主配置文件options {listen-on port 53 { 192.168.80.101; }; //改成自己服务

为嵌入式开发板客户端自己动手在虚拟机上搭建NTP服务器

网络时间协议NTP(Network Time Protocol)是用于互联网中时间同步的标准互联网协议.NTP的用途是把计算机的时间同步到某些时间标准.目前采用的时间标准是世界协调时UTC(Universal Time Coordinated).NTP的主要开发者是美国特拉华大学的David L. Mills教授.NTP对于我们个人来说有什么用呢,简单的讲,当你的计算机时间不准确了,你可以接入到互联网,从网上同步一下时间,看多方便. 对于企业来说,当你有成百上千的计算机,都不能直接连接互联网,时

centos7 搭建svn服务器&客户端的访问&备份迁移

当今用于版本控制的软件程序主要的有svn和git,其它软件咱不熟悉,今天记录下搭建svn服务器和svn客户端使用: 使用环境:虚拟机为centos7系统,svn服务器安装在centos7系统平台上,svn客户端分别在windows7和centos7系统上使用: 1.安装svn服务器: ]# yum install subversion 2.配置svn服务器:建立svn版本库根目录及相关目录即svndata及密码权限命令svnpasswd: ]# mkdir -p /application/{sv