一 ntp相关知识

什么是时间同步服务器

Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。

NTP提供准确时间,首先要有准确的时间来源,这一时间应该是国际标准时间UTC。 NTP获得UTC的时间来源可以是原子钟、天文台、卫星,也可以从Internet上获取。这样就有了准确而可靠的时间源。时间按NTP服务器的等级传播。按照离外部UTC 源的远近将所有服务器归入不同的Stratum(层)中。Stratum-1在顶层,有外部UTC接入,而Stratum-2则从Stratum-1获取时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构相互连接,而Stratum-1的时间服务器是整个系统的基础。

时间同步在真实环境中使用较多,不同机器时间不一致,向同一台数据库服务器写数据会出问题。这仅仅只是一个应用。

在学习时间同步服务器之前,我们先了解下时间相关的命令:

#查看时间
[root@serv01 ~]# date
Wed Aug 7 17:47:44 CST 2013
[root@serv01 ~]# date -s "" #查看时区
[root@larrywen /]# cat /etc/sysconfig/clock
# The time zone of the system is defined bythe contents of /etc/localtime.
# This file is only for evaluation bysystem-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Chongqing" #可以重新设置时区
[root@larrywen /]# system-config-date #查看不同地区的时区信息
[root@larrywen /]# ls /usr/share/zoneinfo/
Africa Australia Cuba Etc GMT0 Iceland Japan MST Poland right Universal Zulu
America Brazil EET Europe GMT-0 Indian Kwajalein MST7MDT Portugal ROC US
Antarctica Canada Egypt Factory GMT+0 Iran Libya Navajo posix ROK UTC
Arctic CET Eire GB Greenwich iso3166.tab MET NZ posixrules Singapore WET
Asia Chile EST GB-Eire Hongkong Israel Mexico NZ-CHAT PRC Turkey W-SU
Atlantic CST6CDT EST5EDT GMT HST Jamaica Mideast Pacific PST8PDT UCT zone.tab #设置时区不重要,重要的是不同机器之间的时间同步起来
[root@serv01 ~]# cat /etc/sysconfig/clock
ZONE="Asia/Chongqing"
[root@serv01 ~]# date
Wed Aug 7 09:52:31 CST 2013 #修改时区
[root@serv01 ~]# date
Wed Aug 7 09:52:31 CST 2013
#修改成日本时区
[root@serv01 ~]# vim /etc/sysconfig/clock
[root@serv01 ~]# cat /etc/sysconfig/clock
ZONE="Asia/Tokyo"
#拷贝内容
[root@serv01 ~]# cp/usr/share/zoneinfo/Asia/Tokyo /etc/localtime
cp: overwrite `/etc/localtime'? Y
#查看时间,发现立即改变,不需要重启
[root@serv01 ~]# date
Wed Aug 7 10:55:44 JST 2013 #使用工具修改时间
#安装工具
[root@serv01 ~]# yum install/usr/bin/system-config-date -y
[root@serv01 ~]# yum installsystem-config-date -y
#安装system-config-date的时候会把ntp服务器安装上 #支持X Window的形式登录
[root@larrywen 0807]# ssh 192.168.1.11 -X
root@192.168.1.11's password:
Last login: Wed Aug 7 18:19:30 2013 from 192.168.1.1
/usr/bin/xauth: creating new authority file /root/.Xauthority
#修改时间,改成重庆时区
[root@serv01 ~]# system-config-date
Gtk-Message: Failed to load module"pk-gtk-module": libpk-gtk-module.so: cannot open shared object file:No such file or directory
[root@serv01 ~]# cat /etc/localtime
[root@serv01 ~]# date
Wed Aug 7 10:03:22 CST 2013

硬件时钟和软件时钟

Linux将时钟分为系统时钟(SystemClock)和硬件(Real Time Clock,简称RTC)时钟两种。系统时间是指当前Linux Kernel中的时钟,也就是软件时钟。而硬件时钟则是主板上由电池供电的那个主板硬件时钟,这个时钟可以在BIOS的“Standard BIOS Feture”项中进行设置。

#同步硬件时钟和软件时钟
[root@serv01 ~]# clock --help
hwclock - query and set the hardware clock(RTC) Usage: hwclock [function] [options...] Functions:
-s | --hctosys set the system time from the hardware clock
-w | --systohc set the hardware clock to the currentsystem time #ntp:Network TimeProtocal

二 ntp同步时间

#第一步,安装ntp
[root@serv01 ~]# yum install ntp* -y #查询安装的RPM包
[root@serv01 ~]# rpm -qa|grep ntp
ntp-4.2.4p8-2.el6.x86_64
ntpdate-4.2.4p8-2.el6.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
[root@serv01 ~]# chkconfig|grep ntp
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@serv01 ~]# ls /etc/ntp.conf
/etc/ntp.conf
[root@serv01 ~]# rpm -ql ntp #第二步,修改配置文件
[root@serv01 ~]# vim /etc/ntp.conf
[root@serv01 ~]# man 5 ntp.conf [root@serv01 ~]# vim /etc/ntp.conf
[root@serv01 ~]# cat /etc/ntp.conf
#配置如下
restrict 192.168.1.0 mask 255.255.255.0nomodify notrap
#注释上一级时间同步服务器
#server 0.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org
#取消注释
server 127.127.1.0 # local clock
fudge 127.127.1.0stratum 10 #第三步,启动服务
[root@serv01 ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]
#重启后生效
[root@serv01 ~]# chkconfig ntpd on
[root@serv01 ~]# chkconfig|grep ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@serv01 ~]# #serv02的配置
[root@serv02 ~]# vim /etc/ntp.conf
[root@serv02 ~]# cat /etc/ntp.conf
server 192.168.1.11
[root@serv02 ~]# vim /etc/ntp/step-tickers
[root@serv02 ~]# cat /etc/ntp/step-tickers
# List of servers used for initialsynchronization.
192.168.1.11
[root@serv02 ~]# date
Wed Aug 7 18:30:18 CST 2013
[root@serv02 ~]# /etc/init.d/ntpdate start
ntpdate: Synchronizing with time server: [ OK ]
[root@serv02 ~]# date
Wed Aug 7 10:29:39 CST 2013

三 ssh双向等效性验证时间同步服务器

#serv01
[root@serv01 ~]# ssh-keygen
[root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.12
[root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11 #serv02
[root@serv02 ~]# ssh-keygen
[root@serv02 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11 #可以查看同步的时间
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;
Wed Aug 7 10:34:50 CST 2013
Wed Aug 7 10:34:50 CST 2013 #修改时间
[root@serv01 ~]# date -s"10:38:00"
Wed Aug 7 10:38:00 CST 2013 #查看serv02的时间
[root@serv02 ~]# date
Wed Aug 7 10:35:42 CST 2013
#关闭服务
[root@serv02 ~]# /etc/init.d/ntpdate stop
#开启服务
[root@serv02 ~]# /etc/init.d/ntpdate start
ntpdate: Synchronizing with timeserver: [ OK ]
#可以看到已经同步了
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date
Wed Aug 7 10:38:57 CST 2013
Wed Aug 7 10:38:57 CST 2013 [root@serv02 ~]# ntpq -p 192.168.1.11
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 33 64 377 0.000 0.000 0.000 #使用命令同步,并将同步命令做成死循环
[root@serv02 ~]# ntpdate 192.168.1.11
7Aug 23:02:44 ntpdate[1342]: adjust time server 192.168.1.11 offset 0.000041 sec
#可以看到已经同步了
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date
Wed Aug 7 23:02:53 CST 2013
Wed Aug 7 23:02:53 CST 2013
[root@serv01 ~]# date -s "10:50"
Wed Aug 7 10:50:00 CST 2013 [root@serv02 ~]# ntpdate 192.168.1.11
7Aug 10:51:10 ntpdate[1372]: step time server 192.168.1.11 offset -43989.248450sec
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date
Wed Aug 7 10:51:15 CST 2013
Wed Aug 7 10:51:15 CST 2013 #写成死循环
[root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done
7Aug 10:53:27 ntpdate[1380]: adjust time server 192.168.1.11 offset -0.000066sec
7Aug 10:53:31 ntpdate[1382]: adjust time server 192.168.1.11 offset 0.000133 sec
[root@serv01 ~]# date -s "10:57"
Wed Aug 7 10:57:00 CST 2013
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date
Wed Aug 7 10:57:12 CST 2013
Wed Aug 7 10:57:13 CST 2013 #可以放到后台执行
[root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done > /dev/null 2>&1 & #写到配置文件,下次启动自动执行
[root@serv02 ~]# vim /etc/rc.local
[root@serv02 ~]# tail -n1 /etc/rc.local
while :; do ntpdate 192.168.1.11; sleep 3;done >/dev/null 2>&1 & #另外一种方式(不安全:在同一网段的任何人都可以使用)
serv01
[root@serv01 ~]# chkconfig xinetd on
[root@serv01 ~]# vim /etc/xinetd.d/
chargen-dgram daytime-dgram discard-dgram echo-dgram tcpmux-server time-stream
chargen-stream daytime-stream discard-stream echo-stream time-dgram
#将disbale由yes改为no
[root@serv01 ~]# vim/etc/xinetd.d/time-stream
#将disbale由yes改为no
[root@serv01 ~]# vim/etc/xinetd.d/time-dgram
#也可以这样打开
[root@serv01 ~]# chkconfig time-stream on
[root@serv01 ~]# chkconfig time-dgram on #启动xinetd服务
[root@serv01 ~]# /etc/init.d/xinetd start
Starting xinetd: [ OK ] #安装openssh-clients
[root@serv01 ~]# yum installopenssh-clients -y
#制作公钥
[root@serv01 ~]# ssh-keygen
#拷贝到本机
[root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.11
#拷贝到serv02
[root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.12
#设置时间
[root@serv01 ~]# date -s"11:36:00"
Wed Aug 7 11:36:00 CST 2013
#安装rdate
[root@serv02 /]# yum install rdate -y
#同步时间
[root@serv02 /]# rdate -s 192.168.1.11
[root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;
Wed Aug 7 11:36:29 CST 2013
Wed Aug 7 11:36:28 CST 2013 #可以写死循环
[root@serv02 ~]# while :; do rdate -s192.168.1.11; sleep 3; done >/dev/null 2>&1 &^C
[root@serv02 ~]# vi /etc/rc.local
[root@serv02 ~]# tail -n1 /etc/rc.local
while :; do rdate -s 192.168.1.11; sleep 3;done >/dev/null 2>&1 &
我的邮箱:wgbno27@163.com  新浪微博:@Wentasy27
微信公众平台:JustOracle(微信号:justoracle)
数据库技术交流群:336882565(加群时验证 From CSDN XXX)
By Larry Wen
 
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]

服务管理——ntp的更多相关文章

  1. Linux服务管理之ntp

    NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 在计算机的世界里,时间非常地重要,例如对于火箭发射这种科研活动,对时间的统一性和准确性要 ...

  2. linux服务之ntp与chrony

    centos7.2变成了chrony RHEL从7.0开始改用chrony同步时间,原ntp同步方式也可以使用,但要安装ntp服务. [root@1st-kvm plymouth]# rpm -qa| ...

  3. centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课

    centos linux系统日常管理3  服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...

  4. linux服务之NTP及chrony时间同步

    博客园 首页 联系 管理   linux服务之NTP及chrony时间同步   一.NTP时间同步 NTP(Network Time Protocol,网络时间协议)是由RFC 1305定义的时间同步 ...

  5. 第11章 Linux服务管理

    1. 服务分类 (1)Linux的服务 ①Linux中绝大多数的服务都是独立的,直接运行于内存中.当用户访问时,该服务直接响应用户,其好处是服务访问响应速度快.但不利之处是系统中服务越多,消耗的资源越 ...

  6. CentOS 7 (RHEL 7)服务管理命令的变化

    CentOS 7 (RHEL 7)带来了新的服务管理命令,为了保持兼容原有的命令仍可以使用,以下是新旧命令的对照. 启动.停止.重启.重载.检查服务:6: service httpd start|st ...

  7. PC管理端与评委云打分配合步骤及疑难问题汇编,即如何使用PC管理端的云服务管理功能

    一.前期环境及数据准备 A.PC管理端主要流程 1.进入菜单 编辑/选项/服务器 界面,如下图所示,采用我官方所提供的云服务,不采用自己假设的AppWeb服务. 切记:AppWeb服务和云服务只能二选 ...

  8. Ubuntu 和 Redhat / Fedora 服务管理命令对比表(附Fedora16新的服务管理工具systemctl )

    以 apache/httpd 服务作为例子 任务 Red Hat / Fedora Ubuntu Ubuntu (with sysv-rc-conf or sysvconfig) 立即启动/停止某服务 ...

  9. Centos6.5 设置Tomcat8 service服务实现自启动和服务管理

    Centos6.5 设置Tomcat8 service服务实现自启动和服务管理 将tomcat设置成像apache,nginx一样. 用serviec xxxx start/stop/restart ...

随机推荐

  1. NEC红外遥控协议理解与实现

    红外发射管有2个管脚,发送的是经过38KHz时钟调制过的信号.例如下图使用PWM产生一个等占空时钟信号用于调制. 接收管收下来的信号已经经过了解调,可以直接连接系统的外部中断脚. 下面通过逻辑分析仪来 ...

  2. Linux下静态编译Qt程序

    一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...

  3. JIRA官方:JIRA源代码集成

    防火墙后的Git 使用Atlassian Stash创建和管理Git存储库,设置细粒度的权限并在代码上协作.这一切—安全.快速.可靠,更重要的是,可以部署在防火墙后面.JIRA问题关键字自动将JIRA ...

  4. MyEclipse中新建JSP(Advanced Template)文件时自动生成的

    <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="ca ...

  5. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  6. 网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【2】- 在线编程(难度:中)

    第6单元作业[2]- 在线编程(难度:中) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...

  7. C#委托的异步调用【转】

    本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: ); //模拟该方法运 ...

  8. JAVA学习第二十九课(经常使用对象API)- String类

    多线程告一段落,開始经常使用对象API的涉及,背也要背下来!.! 日后开发,遇见最多的对象是文字,也就是字符串 String类 字符串是一个特殊对象 字符串一旦初始化就不能够被改变 一.特点 publ ...

  9. 关于ASP.NET中的负载均衡

    ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同一 ...

  10. Oracle运维 专业的事情交给专业的人来做

    关于Oracle运维的重要性,随便写了几句,放在这儿和大家共勉. Oracle数据库在RDBMS中独占熬头,07年统计其市场份额占关系数据库市场的48.6%稳居首位,而且逐年增加,上升势头强劲.在使用 ...