ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

如何在政府机房级别下,使用Centos7搭设无网络ntp时钟服务器

2021-06-18 14:04:57  阅读:171  来源: 互联网

标签:ntp yingyong2 yingyong1 Centos7 sh 搭设 root ntpd


原创仅供学习,转载请注明出处

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

前言

在实际的运维部署工作中,现在很多服务端都直接部署在阿里云、腾讯云等云平台,在这种平台上是肯定没有禁止访问外网的限制,也不需要考虑服务器的NTP时钟应该去哪里同步之类的问题!

但是在银行级别以及政府级别的机房中,外网是禁止的,如果需要访问一些公网的资源则需要申请防火墙的ACL放行特定的IP+端口号,这就让很多的部署工作增加了困难度。

本篇章主要介绍如何在这种情况下部署配置好机房服务器的时钟同步情况。

部署场景

在有些机房部署服务器的时候,服务器是处于无网络区域的。此时,每台服务器的时钟并不准确,各自运行时间。

这样的情况会导致服务端或者数据库获取本地时间的时候出错,导致部分工单时间信息有误。这样的后果是比较严重的。

那么如何去解决这个问题呢?

可以部署一台ntp的服务端,然后其他服务器编写shell脚本定时同步即可

实现同步拓扑如下

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

好了,根据拓扑图,首先需要实现搭设的就是Ntp服务器了。

安装ntp服务

yum install ntp ntpdate -y

不管是作为ntp服务器还是客户端,只要需要时钟同步,都进行安装。

安装完毕之后,可以查看一下服务的状态:

[root@yingyong1 ntp_setup]# 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)
[root@yingyong1 ntp_setup]#

部署Ntp服务器

安装好了ntp服务器之后,那么就开始来配置相关的参数了。

查看当前的时区

使用timedatectl status,可以查看当前服务器运行的时区。从下面返回的结果来看,当前的运行时区就是上海时区(Time zone: Asia/Shanghai (CST, +0800)),这个正确的。

[root@yingyong1 ~]# timedatectl status
Local time: Fri 2019-01-04 09:54:06 CST
Universal time: Fri 2019-01-04 01:54:06 UTC
RTC time: Fri 2019-01-04 01:54:06
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@yingyong1 ~]#

如果时区不是东八区,那么需要设置为东八区的时间(设置北京、上海、香港等时区)

首先查看一下服务器是否有上海时区、香港时区,从结果来看是有的。timedatectl list-timezones | grep Shanghai

[root@yingyong1 ~]# timedatectl list-timezones | grep Shanghai
Asia/Shanghai
[root@yingyong1 ~]# timedatectl list-timezones | grep Hong_Kong
Asia/Hong_Kong
[root@yingyong1 ~]#

设置服务器的时区为香港时区,如下:timedatectl set-timezone Asia/Hong_Kong

[root@yingyong1 ~]# timedatectl set-timezone Asia/Hong_Kong
[root@yingyong1 ~]#
## 查看修改时区后的状态
[root@yingyong1 ~]# timedatectl status
Local time: Fri 2019-01-04 10:07:11 HKT
Universal time: Fri 2019-01-04 02:07:11 UTC
RTC time: Fri 2019-01-04 02:07:11
Time zone: Asia/Hong_Kong (HKT, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

## 输入date,查看当前时间
[root@yingyong1 ~]# date
Fri Jan 4 10:08:03 HKT 2019
[root@yingyong1 ~]#

好了,修改了时区之后,就有两个选择要操作,一种是有网络的情况下,设置时钟与网络时钟同步;另一种则是设置不与网络同步。

因为当前的状态是无网络的,那么先来讲述一下如何配置无网络下以自身作为时钟服务。

完全无网络环境ntp服务端配置自身作为时钟服务

配置/etc/ntp.conf,配置使用本地时间,不与网络同步。

# 使用本地时间
# 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
fudge  127.127.1.0 stratum 10

在配置之前,首先备份配置文件。

[root@yingyong1 etc]# cp ntp.conf ntp.conf.bak

首先将请求网络时钟服务的部分注释(/etc/ntp.conf)

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

配置使用本地时间,不与网络同步。

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=
# 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
fudge 127.127.1.0 stratum 10

本地设置时间,并提供ntpd局域网服务

## 停止ntpd时钟服务
[root@yingyong1 etc]# service ntpd stop
Redirecting to /bin/systemctl stop ntpd.service
[root@yingyong1 etc]#
## 设置不与网络服务同步
[root@yingyong1 etc]# timedatectl set-ntp no
[root@yingyong1 etc]#
## 手动配置当前时间
[root@yingyong1 etc]# timedatectl set-time "2019-01-04 10:45:10"
[root@yingyong1 etc]#
## 确认看看当前时间
[root@yingyong1 etc]# date
Fri Jan 4 10:45:15 HKT 2019
[root@yingyong1 etc]#
## 开启ntp服务
[root@yingyong1 etc]# service ntpd start
Redirecting to /bin/systemctl start ntpd.service
[root@yingyong1 etc]#
## 查看ntp服务状态
[root@yingyong1 etc]# 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: active (running) since Fri 2019-01-04 10:46:00 HKT; 3s ago
Process: 25620 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 25621 (ntpd)
Tasks: 1
CGroup: /system.slice/ntpd.service
└─25621 /usr/sbin/ntpd -u ntp:ntp -g

Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 2 lo 127.0.0.1 UDP 123
Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 3 bond0 134.****.110 UDP 123
Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 4 virbr0 192.****.1 UDP 123
Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 5 lo ::1 UDP 123
Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 6 bond0 fe80::6e62:4867:5e...123
Jan 04 10:46:00 yingyong1 ntpd[25621]: Listening on routing socket on fd #23 for int...tes
Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c016 06 restart
Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c011 01 freq_not_set
Jan 04 10:46:01 yingyong1 ntpd[25621]: 0.0.0.0 c514 04 freq_mode
Hint: Some lines were ellipsized, use -l to show in full.
[root@yingyong1 etc]#
## 查看启动服务后的当前时间
[root@yingyong1 etc]# date
Fri Jan 4 10:46:05 HKT 2019
[root@yingyong1 etc]#
## 配置开机自启动
[root@yingyong1 etc]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@yingyong1 etc]#

好了,到了这一步已经配置搭设好了局域网的ntp时钟服务器了。下一步就是要在客户机配置同步时钟服务。

配置客户机向ntp服务器同步时间

首先每台客户机安装好ntp工具,如下:yum -y install ntp ntpdate

配置服务器的时区

## 查看当前的时间
[root@yingyong2 ~]# date
Fri Jan 4 10:34:40 CST 2019
[root@yingyong2 ~]#
## 查看当前的时区状态
[root@yingyong2 ~]# timedatectl status
Local time: Fri 2019-01-04 10:34:42 CST
Universal time: Fri 2019-01-04 02:34:42 UTC
RTC time: Fri 2019-01-04 02:34:43
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@yingyong2 ~]#
## 查看是否存在上海时区
[root@yingyong2 ~]# timedatectl list-timezones | grep Shanghai
Asia/Shanghai
[root@yingyong2 ~]#
## 修改为香港时区
[root@yingyong2 ~]# timedatectl set-timezone Asia/Hong_Kong
[root@yingyong2 ~]#
## 查看当前时区状态已经是香港时区了,此时可以看到NTP enabled:no(没有与ntp服务同步)
[root@yingyong2 ~]# timedatectl status
Local time: Fri 2019-01-04 10:35:02 HKT
Universal time: Fri 2019-01-04 02:35:02 UTC
RTC time: Fri 2019-01-04 02:35:04
Time zone: Asia/Hong_Kong (HKT, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@yingyong2 ~]# date
Fri Jan 4 10:35:06 HKT 2019
[root@yingyong2 ~]#

在配置好了时区之后,只要写个简单的shell脚本就可以与局域网的ntp服务器同步时间了。

编写ntp客户端时钟同步脚本

echo "`which ntpdate` ntp服务器IP地址;`which hwclock` -w" > ntpUpdate.sh`
[root@yingyong2 sh]# echo "`which ntpdate` 134.78.194.110;`which hwclock` -w" > ntpUpdate.sh
[root@yingyong2 sh]#
[root@yingyong2 sh]# cat ntpUpdate.sh
/usr/sbin/ntpdate 134.78.194.110;/usr/sbin/hwclock -w
[root@yingyong2 sh]#
[root@yingyong2 sh]# chmod +x ntpUpdate.sh
[root@yingyong2 sh]#

测试执行一下看看:

[root@yingyong2 sh]# date
Fri Jan 4 10:43:41 HKT 2019
[root@yingyong2 sh]#
[root@yingyong2 sh]# ./ntpUpdate.sh
4 Jan 11:02:13 ntpdate[15950]: step time server 134.78.194.110 offset 1100.587184 sec
[root@yingyong2 sh]#
[root@yingyong2 sh]# date
Fri Jan 4 11:02:16 HKT 2019
[root@yingyong2 sh]#

好了,那么下面只要写一个crontab定时执行该脚本即可。

编写定时任务

## 创建一个专门存放shell脚本的目录
[root@yingyong2 sh]# mkdir -p /sh
## 将刚才编写同步脚本复制到 /sh 目录
[root@yingyong2 sh]# cp ntpUpdate.sh /sh
[root@yingyong2 sh]# ls /sh/ntpUpdate.sh
/sh/ntpUpdate.sh
## 写入一个定时任务
[root@yingyong2 sh]# echo "1 * * * * /sh/ntpUpdate.sh" >> /var/spool/cron/`whoami`
[root@yingyong2 sh]#
## 查看写入的任务,确保OK
[root@yingyong2 sh]# crontab -l
1 * * * * /sh/ntpUpdate.sh
[root@yingyong2 sh]#
## 测试执行一下定时任务的脚本
[root@yingyong2 sh]# /sh/ntpUpdate.sh
4 Jan 11:11:48 ntpdate[15981]: adjust time server 134.78.194.110 offset -0.002369 sec
[root@yingyong2 sh]# date
Fri Jan 4 11:11:52 HKT 2019
[root@yingyong2 sh]#

到了这里已经完成了无网络局域网ntp时钟服务同步的相关内容了。

 

标签:ntp,yingyong2,yingyong1,Centos7,sh,搭设,root,ntpd
来源: https://blog.51cto.com/u_11239407/2921519

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有