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

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<br></br>Redirecting to /bin/systemctl status ntpd.service<br></br>● ntpd.service - Network Time Service<br></br>   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)<br></br>   Active: inactive (dead)<br></br>[root@yingyong1 ntp_setup]#

部署Ntp服务器

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

查看当前的时区

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

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

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

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

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

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

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

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

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

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

配置/etc/ntp.conf,配置使用本地时间,不与网络同步。
# 使用本地时间<br></br># Undisciplined Local Clock. This is a fake driver intended for backup<br></br># and when no outside source of synchronized time is available.<br></br>server 127.127.1.0     # local clock<br></br>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<br></br># and when no outside source of synchronized time is available.<br></br>server 127.127.1.0     # local clock<br></br>fudge  127.127.1.0 stratum 10

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

## 停止ntpd时钟服务<br></br>[root@yingyong1 etc]# service ntpd stop<br></br>Redirecting to /bin/systemctl stop ntpd.service<br></br>[root@yingyong1 etc]#<br></br>## 设置不与网络服务同步<br></br>[root@yingyong1 etc]# timedatectl set-ntp no<br></br>[root@yingyong1 etc]#<br></br>## 手动配置当前时间<br></br>[root@yingyong1 etc]# timedatectl set-time "2019-01-04 10:45:10"<br></br>[root@yingyong1 etc]#<br></br>## 确认看看当前时间<br></br>[root@yingyong1 etc]# date<br></br>Fri Jan  4 10:45:15 HKT 2019<br></br>[root@yingyong1 etc]#<br></br>## 开启ntp服务<br></br>[root@yingyong1 etc]# service ntpd start<br></br>Redirecting to /bin/systemctl start ntpd.service<br></br>[root@yingyong1 etc]#<br></br>## 查看ntp服务状态<br></br>[root@yingyong1 etc]# service ntpd status<br></br>Redirecting to /bin/systemctl status ntpd.service<br></br>● ntpd.service - Network Time Service<br></br>   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)<br></br>   Active: active (running) since Fri 2019-01-04 10:46:00 HKT; 3s ago<br></br>  Process: 25620 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)<br></br> Main PID: 25621 (ntpd)<br></br>    Tasks: 1<br></br>   CGroup: /system.slice/ntpd.service<br></br>           └─25621 /usr/sbin/ntpd -u ntp:ntp -g<br></br><br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 2 lo 127.0.0.1 UDP 123<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 3 bond0 134.****.110 UDP 123<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 4 virbr0 192.****.1 UDP 123<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 5 lo ::1 UDP 123<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listen normally on 6 bond0 fe80::6e62:4867:5e...123<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: Listening on routing socket on fd #23 for int...tes<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c016 06 restart<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM<br></br>Jan 04 10:46:00 yingyong1 ntpd[25621]: 0.0.0.0 c011 01 freq_not_set<br></br>Jan 04 10:46:01 yingyong1 ntpd[25621]: 0.0.0.0 c514 04 freq_mode<br></br>Hint: Some lines were ellipsized, use -l to show in full.<br></br>[root@yingyong1 etc]#<br></br>## 查看启动服务后的当前时间<br></br>[root@yingyong1 etc]# date<br></br>Fri Jan  4 10:46:05 HKT 2019<br></br>[root@yingyong1 etc]#<br></br>## 配置开机自启动<br></br>[root@yingyong1 etc]# systemctl enable ntpd<br></br>Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.<br></br>[root@yingyong1 etc]#

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

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

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

配置服务器的时区

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

测试执行一下看看:

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

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

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

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

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

相关文章推荐

添加新评论,含*的栏目为必填