前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos7搭设无网络ntp时钟服务器

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

作者头像
Devops海洋的渔夫
发布2019-06-02 13:47:53
1.8K0
发布2019-06-02 13:47:53
举报
文章被收录于专栏:Devops专栏Devops专栏

部署场景

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

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

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

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

实现同步拓扑如下

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

安装ntp服务

yum install ntp ntpdate -y

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

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

代码语言:javascript
复制
[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)),这个正确的。

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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,配置使用本地时间,不与网络同步。

代码语言:javascript
复制
# 使用本地时间
# 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

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

代码语言:javascript
复制
[root@yingyong1 etc]# cp ntp.conf ntp.conf.bak

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

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

代码语言:javascript
复制
# 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局域网服务

代码语言:javascript
复制
## 停止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 配置服务器的时区

代码语言:javascript
复制
## 查看当前的时间
[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客户端时钟同步脚本

代码语言:javascript
复制
echo "`which ntpdate` ntp服务器IP地址;`which hwclock` -w" > ntpUpdate.sh`

代码语言:javascript
复制
[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]# 

测试执行一下看看:

代码语言:javascript
复制
[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定时执行该脚本即可。

编写定时任务

代码语言:javascript
复制
## 创建一个专门存放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时钟服务同步的相关内容了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 部署场景
  • 实现同步拓扑如下
  • 安装ntp服务
  • 部署Ntp服务器
    • 查看当前的时区
      • 如果时区不是东八区,那么需要设置为东八区的时间(设置北京、上海、中国香港等时区)
        • 完全无网络环境ntp服务端配置自身作为时钟服务
          • 本地设置时间,并提供ntpd局域网服务
          • 配置客户机向ntp服务器同步时间
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档