Linux服务器ntp时间同步离线安装配置


写在前面:在大多数实际生产环境集群模式中,需要进行配置集群中所有服务器时间同步,避免因时间不同步导致集群异常问题;但是大多数公司生产环境都为内网环境不能连接互联网进行在线安装ntp服务,本文基于内网环境进行ntp服务安装配置,做到内网集群时间自动同步。

1.环境准备

准备三台(或三台以上)物理机或虚拟机,选择其中一台作为NTP服务端,其他作为NTP客户端。

IP主机名角色
192.168.86.201esnode1NTP服务端
192.168.86.202esnode2NTP客户端
192.168.86.203esnode3NTP客户端

2.下载安装包

确认操作系统版本,可执行以下命令确认自己系统的版本信息(我的Linux系统为CentOS7.4 x86_64版本)

cat /etc/redhat-release
uname -a
cat /proc/version

确认系统版本截图
确认系统是否已安装NTP服务

rpm -qa | grep ntp

在这里插入图片描述
注:我的系统通过在线安装已经安装过,这里为了演示离线安装我进行卸载。

如果系统自带或已安装版本不符合个人需求,可进行卸载,命令如下:

rpm -e --nodeps ntpdate-4.2.6p5-29.el7.centos.2.x86_64
rpm -e --nodeps ntp-4.2.6p5-29.el7.centos.2.x86_64

在这里插入图片描述

离线安装NTP需要下载NTP相关rpm安装包
可以通过https://pkgs.org/download/ntp查找所需对应操作系统的安装包
需要下载ntp与ntpdate两个安装包

ntp下载截图
也可以直接通过以下链接进行下载:

http://mirror.centos.org/centos/7/updates/x86_64/Packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
http://mirror.centos.org/centos/7/updates/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

3.安装NTP服务

下载完成所需安装包,接下来就进行NTP服务的安装
安装ntpdate

rpm -ivh ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm

在这里插入图片描述
安装ntp

rpm -ivh ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm

在这里插入图片描述
温馨提示:其他节点参考此步骤进行安装。

4.配置NTP

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

# 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
# 允许192.168.86.0网段内所有机器从esnode1同步时间
restrict 192.168.86.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
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

# 允许上层时间服务器主动修改本机时间
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
restrict 3.cn.pool.ntp.org nomodify notrap noquery

# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 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

# 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.
SYNC_HWCLOCK=yes
disable monitor

NTP客户端配置

# 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

# 从esnode1中同步时间
server 192.168.86.201

# 允许esnode1修改本地时间
restrict 192.168.86.201 nomodify notrap noquery

# 如果esnode1不可用,用本地的时间服务
server 127.127.1.0
fudge 127.127.1.0 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

# 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.
SYNC_HWCLOCK=yes
disable monitor

5.验证时间同步

启动ntp服务器需要在各节点先手动同步一下时间
如果能连接互联网且在ntp服务端配置了上层服务端为中国国家授时中心服务器地址,应先手动同步一下国家授时中心时间,命令如下:

ntpdate cn.pool.ntp.org

其他客户端节点都进行手动同步ntp服务端时间,命令如下:

ntpdate 192.168.86.201

启动ntp

systemctl start ntpd.service

查看启动运行状态

systemctl status ntpd.service

在这里插入图片描述

ntpstat

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
每个节点开启开机自启动

systemctl enable ntpdate.service
或
chkconfig ntpd on

同时执性date命令

[root@esnode1 ~]# date
Thu Sep 10 11:00:55 CST 2020
[root@esnode2 ~]# date
Thu Sep 10 11:00:55 CST 2020
[root@esnode3 ~]# date
Thu Sep 10 11:00:55 CST 2020

结语:如若文章有何不妥之处欢迎指正,谢谢!

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值