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時鐘服務同步的相關內容了。

相關文章
相關標籤/搜索