Centos7搭设无网络ntp时钟服务器

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

前言

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

可是在银行级别以及政府级别的机房中,外网是禁止的,若是须要访问一些公网的资源则须要申请防火墙的ACL放行特定的IP+端口号,这就让不少的部署工做增长了困难度。bash

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

部署场景

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

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

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

能够部署一台ntp的服务端,而后其余服务器编写shell脚本定时同步便可ide

实现同步拓扑以下

好了,根据拓扑图,首先须要实现搭设的就是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

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

# 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时钟服务同步的相关内容了。

相关文章
相关标签/搜索