自动化安装 NTP服务器

1 概述

linux服务器在提供服务时,要和其他机器进行请求的交互,实际生产环境中,可能因为时间不同步,导致了服务异常。本文将接受NTP服务器的相关配置和 概念,通过脚本实现一键安装NTP服务器和客户端

将服务器的本地机器的时间设置为NTP服务器,读者可以自己将server调整为网络上的时间服务器

如果实验中,使用vmware来安装虚拟机,建议vmware安装vmware tools,然后设置vmware选项,开启时间同步,使得虚拟机的时间和物理机的时间同步 ,防止虚拟机时间自动变更为错误的时间

注意,CentOS7开始改用chrony同步时间,原来ntp同步方式在CentOS7也可以使用,但要安装ntp服务。

2 服务器的配置

用ntpd时间同步的话,前提是客户端和服务器时间基本一致,才能通过服务ntpd同步,为了防止时间同步服务异常,所以可以先手动同步该服务,然后再启用ntpd服务进行自动同步.

服务器端要安装包 ntp包

命令如下

yum -y install ntp

服务器端需要修改配置文件命令如下

vim  /etc/ntp.conf


driftfile /var/lib/ntp/drift

restrict 192.168.32.0 mask 255.255.255.0 nomodify 

#允许哪些机器同步

restrict 172.18.0.0  mask 255.255.0.0  nomodify

restrict 127.0.0.1

restrict -6 ::1

server 127.127.1.0  

#将该服务器的本地时间作为ntp server的时间,如果其他客户端同步,将同步这台服务器的时间,这里如果要同步公网上的服务器,可以写上公网的服务器,

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys


重启ntpd:

# service ntpd restart

等五六分钟,让ntpd完成自身的时间同步

设置开机启动

chkconfig ntpd on

验证

ntpq  -p 


这里st为 stratum,范围是0--15,如果为16就是错误的

关于restrict格式如下:

restrict [ip] mask [mask_number] [parameter]

其中parameter如下:

ignore:居然所有类型的ntp连接

nomodify:不允许客户端修改服务器的时间参数,但是允许客户端透过这部主机进行时间校验。

noquery:不允许客户端进行时间校验。

notrap:不提供trap时间登录

notrust:拒绝没有认证的客户端

3 客户端同步时间

客户端需要停用ntp服务,否则无法运行ntpdata  服务器地址这个命令 来同步时间,同步的时候会提示ntp被使用中

停用服务命令如下

service ntpd stop

同步服务器的时间,如本例子中服务器的ip是192.168.32.61

ntpdate 192.168.32.61

设置定时同步

使用cron或修改crontab文件定期向NTP服务器更新时间

语句如下

crontab -e

* 10 * * *  /usr/sbin/ntpdate 192.168.32.61;/sbin/hwclock -w   //每天十点执行同步命令

4  自动化安装脚本


#!/bin/bash

#

#******************************************************************************

#Author:              Sunny

#Date:                2017-09-08

#FileName:            install_ntp.sh

#version:              1.0

#Your change info:

#Description:          For install ntp by  auto

#Copyright(C):        2017  All rihts reserved

#*****************************************************************************

is_bakfile(){

[ -e /root/bakfile ] || mkdir /root/bakfile

}

min_time () {

time=`date +%Y%m%d%H%M`

}

is_bakfile

min_time

[ -e /etc/ntp.conf ] &&mv /etc/ntp.conf /root/bakfile/ntp.conf."$time".bak

read -p "Please input your choice,server or client: " choice

if ! $(date | grep CST &>/dev/null);then

[ -e /etc/localtime ]&& mv /etc/localtime /root/bakfile/localtime."$time".bak

ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

fi

case $choice in

server)

echo "#################################################################################################"

echo "if your server is virtual host,suggest you will use its own time to  synchronization host time."

echo "you should also install vmware tools in the server host in order  use phisical  host  time to syn your virtual time,you need to start time ntp option in vmware"

echo "If your want to user common time from internet  to syn server host time,you  should make sure your host can access to the common time server from internet.You just replace server ip which line in line "server 127.127.0.1" "

echo "start to config ntp server"

echo "###################################################################################################"

echo

echo

rpm -q ntp &>/dev/null || yum -y install ntp &>/dev/null

cat >>/etc/ntp.conf<<eof

driftfile /var/lib/ntp/drift

restrict 192.168.32.0 mask 255.255.255.0 nomodify

restrict 172.18.0.0  mask 255.255.0.0  nomodify

restrict 127.0.0.1

restrict -6 ::1

server 127.127.1.0

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

eof

service ntpd restart&>/dev/null || echo "Something wrong when restart ntp service"

netstat -ntlup | grep ntpd &>/dev/null && echo "ntpd is running " || echo "Something wrong happened when start ntp,please check"

chkconfig ntpd on

echo "now it use cmd "ntpq -p",if you see st is what you set in  stratum and less than 16,ntp server is now now ,but it need few minutes to work normal."

ntpq  -p

echo "hint: to syn client host time after 3 minites"

;;

client)

netstat -ntlup | grep ntpd &>/dev/null && service ntpd stop&>/dev/null

read -p "Please input which server to syn(default:192.168.32.61): " ip

if [ -z "${ip:-}" ];then

ip=192.168.32.61

fi

ntpdate $ip

#set auto syn schedule

grep ntpdate /etc/crontab &>/dev/null && echo "already set auto ntp schdule,please check" || echo "* * * * * root /usr/sbin/ntpdate 192.168.32.61;/sbin/hwclock -w">>/etc/crontab

;;

*)

echo "Your choice is wrong,please check"

;;

esac

unset ip

unset time

exit


5 附录

5.1 EDT 和 CST区别和设置

要检查各个服务器时间格式,EDT或者是CST,在中国我们将服务器的时间格式调为CST

EDT:指美国东部夏令时间,波士顿、纽约市、华盛顿哥伦比亚特区,都在这个时区内,跟北京时间有12小时的时差,晚12小时。

CST:可以指下面两种:

1. 美国中部标准时间(西六区,-6:00),中国是东八区(+8:00),北京时间比美国中部标准时间早14个小时。3:45 PMCST是北京时间凌晨1:45。

2. 中澳大利亚标准时间(+10:30),中国是东八区(+8:00),北京时间比中澳大利亚标准时间晚2个半小时。3:45 PMCST是北京时间下午上午5:45。

将系统的时间格式调整为CST的命令如下两条

[root@test ~]# mv /etc/localtime /etc/localtime.bak

[root@test ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

/usr/share/zoneinfo/Asia/Shanghai  为源文件    /etc/localtime 新创建的链接

如果要把CST 时间调整为 EDT,使用如下语句

[root@localhost America]# mv /etc/localtime /etc/localtime.bak

[root@localhost America]#ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

5.2 参考文献

本文的编写参考了以下的文章,读者如果要获取更多详细信息,建议参考如下内容

http://blog.csdn.net/love_life2011/article/details/8067652

http://michael2017.blog.51cto.com/151632/1440488

http://www.cnblogs.com/13188196we/archive/2012/10/20/ntp.html

http://www.linuxidc.com/Linux/2012-08/67223.htm

5.3 相关授时服务器ip

127.127.1.0    本机的ip

210.72.145.44  (国家授时中心服务器IP地址)

ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)

s1a.time.edu.cn 北京邮电大学

s1b.time.edu.cn 清华大学

s1c.time.edu.cn 北京大学

s1d.time.edu.cn 东南大学

s1e.time.edu.cn 清华大学

s2a.time.edu.cn 清华大学

s2b.time.edu.cn 清华大学

s2c.time.edu.cn 北京邮电大学

s2d.time.edu.cn 西南地区网络中心

s2e.time.edu.cn 西北地区网络中心

s2f.time.edu.cn 东北地区网络中心

s2g.time.edu.cn 华东南地区网络中心

s2h.time.edu.cn 四川大学网络管理中心

s2j.time.edu.cn 大连理工大学网络中心

s2k.time.edu.cn CERNET桂林主节点

s2m.time.edu.cn 北京大学

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容