NTP服务器搭建

来源:互联网 发布:中航软件 中心 编辑:程序博客网 时间:2024/05/21 06:19
SUSE Linux Enterprise Server 11 (i586)


NTP(网络时间协议)机制是用于同步网络上的系统时间的协议。首先,计算机从作为可靠时间源的服务器获得时间。然后将此计算机用作网络中其他计算机的时间源。这样做有双重目的,既可维护绝对时间,又可保持网络中所有计算机系统时间的同步。


1.查看系统时钟(Linux 内核时钟)和设置系统时钟
boco@TJ-Unicom-Group-GnCol2:~> date
Mon Jun 27 14:06:29 CST 2011
#date -s 091713272003.30    设置系统时钟的操作  
#date 月日时分年.秒               通用的设置格式


2.查看硬件时钟和设置硬件时钟
Linux将时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟两种。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电(石英钟)的那个主板硬件时钟,这个时钟可以在BIOS的Standard BIOS Feture”项中进行设置。
TJ-Unicom-Group-GiCol2:~ # hwclock                 另一种写法     hwclock --show   查看硬件时钟
Mon Jun 27 15:12:29 2011  -0.346217 seconds
hwclock --set --date="MM/DD/YYYY hh:mm:ss"  通用的设置格式


3.同步系统时钟和硬件时钟
Linux系统(笔者使用的是Red Hat 8.0,其它系统没有做过实验)默认重启后,硬件时钟和系统时钟同步。如果不大方便重新启动的话(服务器通常很少重启),使用clock或hwclock命令来同步系统时钟和硬件时钟。
# hwclock --hctosys                                 硬件时钟同步系统时钟(与硬件时钟为基准) ,--hctosys表示Hardware Clock to SYStem clock   clock 命令在suse11 上用不了
# hwclock -w   或者 hwclock --systohc     系统时钟同步硬件时钟(与系统时钟为基准),--systohc表示  SYStem clock to Hardware Clock
#hwclock --help          查看所有参数和格式


4.安装NTP软件包
安装之前先检测是否这些软件包已安装,方法如下:
TJ-Unicom-Group-SysLogCol1:~ # rpm -qa | grep ntp
ntp-4.2.4p6-1.15
yast2-ntp-client-2.17.14-1.8
如果没有安装,需要进行安装
linux: ~#rpm -ivh ntp-4.2.4p6-1.15.i586.rpm    
此安装包位于D:\工作软件\SUSE 11 32位 操作系统 镜像ISO\SUSE_SLES-11-0-1\suse\i586\ntp-4.2.4p6-1.15.i586.rpm


5.设置时钟同步– NTP 配置文件模式(配置文件模式是长久有效,命令模式临时有效) SUSE 11版
NTP 修改配置文件/etc/ntp.conf
server 10.3.64.61                           设置时钟服务器(源),重启NTP服务即可,上级时间服务器的IP或者域名
# local clock (LCL)
fudge 127.127.1.0  stratum 10       fudge设置Stratum(层)
# LCL is unsynchronized
启动/停止/重启/状态ntp服务
# /sbin/service ntp start
# /sbin/service ntp stop
# /sbin/service ntp restart        
# /sbin/service ntp status
     remote           refid      st t when poll reach   delay   offset  jitter
=============================================================================
*10.3.64.61      132.174.32.151   5 u   40   64   17    0.226    0.533   0.133     这是NTP启动状态running,指向时钟源10.3.64.61
Checking for network time protocol daemon (NTPD):   running
TJ-Unicom-Group-LupsCol2:/etc # ntpq -p                     监视NTP->只有启动NTP服务才能监视NTP,否则ntpq: read: Connection refused
     remote           refid      st t when poll reach   delay   offset  jitter
=============================================================================
 LOCAL(0)        .LOCL.          10 l   58   64    1    0.000    0.000   0.001                   监视NTP,但没有指向时钟源
     remote           refid      st t when poll reach   delay   offset  jitter
=============================================================================
*10.3.64.61      132.174.32.151   3 u   31   64  377    0.714  207.690 147.835     监视NTP,已经指向时钟源10.3.64.61,目的NTP是否正常如果发现在remote这列之前会出现星号和+号,说明已经跟这个服务器同步成功了
TJ-Unicom-Group-Analyse1:~ # service ntp status                                    这是NTP没有启动的状态
Checking for network time protocol daemon (NTPD):   unused                                                            
TJ-Unicom-Group-GnCol1:/etc # ntpdate 10.3.64.61      NTP命令模式,手工同步NTP,有警告WARNING信息,但可以同步(配置文件模式是长久有效,命令模式临时有效)
       !!!!!!!!!!!!!!  WARNING !!!!!!!!!!!!!!!!!!
The ntpdate program is deprecated and should not be used any more.  To
quote the upstream ntp developers:
  "The functionality ntpdate offered is now provided by the ntpd daemon
  itself.  If you call ntpd with the command line option -q it will
  retrieve the current time and set it accordingly."
Please check the Network Time Protocol (NTP) daemon man page and
  http://support.ntp.org/bin/view/Dev/DeprecatingNtpdate
for further information.
You can replace the ntpdate call with "rcntp ntptimeset" to achieve an
inital poll of the servers specified in /etc/ntp.conf.
The program /usr/sbin/sntp offers comparable functionality to ntpdate.
Specifically
  sntp -P no -r pool.ntp.org
is equivalent to
  ntpdate pool.ntp.org
For further details please refer to the man page of sntp.
TJ-Unicom-Group-GnCol1:/etc # date
Mon Jul  4 11:32:56 CST 2011
TJ-Unicom-Group-GnCol1:/etc # ntpq -p                          监视NTP->只有启动NTP服务才能监视NTP,否则ntpq: read: Connection refused

ntpq: read: Connection refused                                        目的NTP是否正常



6.开机自动启动NTP服务
TJ-Unicom-Group-Analyse1:~ # chkconfig -list | grep ntp                    开机不启动时显示off,0~6指系统的运行级别
ntp                       0:off  1:off  2:off  3:off  4:off  5:off  6:off
chkconfig ntp on                                                                                 开机自动启动NTP服务
TJ-Unicom-Group-Analyse1:~ # chkconfig -list | grep ntp
ntp                       0:off  1:off  2:off  3:on   4:off  5:on   6:off              确认服务已启动,启动服务时显示on

7.查看NTP服务日志(就能观察到时间是否在同步)
#cat /var/log/ntp
 10 Jun 15:28:38 ntpd[2022]: ntpd exiting on signal 15
 5 Jul 16:23:38 ntpd[17978]: synchronized to 10.3.64.61, stratum 5
 5 Jul 16:29:18 ntpd[17978]: time reset +339.397617 s
 5 Jul 16:29:18 ntpd[17978]: kernel time sync status change 0001

8.使用crontab设置时钟同步NTP的任务计划
* * * * * /usr/sbin/ntpdate 10.3.64.61 &>/dev/null;/sbin/hwclock -w    创建一个cron让它自动运行以下命令每天同步一次NTP,并把执行结果和错误信息都重定向/dev/null->系统时钟同步硬件时钟。
* * * * * /usr/sbin/ntpdate 10.3.64.61 &>/dev/null                      这是另一种写法,把命令的结果和错误信息都写入文件
* * * * * /usr/sbin/hwclock -w                                                      系统时钟同步硬件时钟
* * * * *  su - root -c "/sbin/hwclock -w"                                      先切换用户为root,在执行“系统时钟同步硬件时钟”
0-59/10 * * * * /usr/sbin/ntpdate 10.3.64.61 &>/root/ntplog;/sbin/hwclock -w &>/root/ntptb  表示每隔10分钟同步一次NTP时间,并把执行结果和错误信息都重定向/root/ntplog->系统时钟同步硬件时钟
0 10 * * * /usr/sbin/ntpdate 10.3.64.61 &>/root/ntplog;/sbin/hwclock -w &>/root/ntptb  每天10点整同步一次NTP时间,并把执行结果和错误信息都重定向/root/ntplog->系统时钟同步硬件时钟 
9.cron文件的目录和cron执行日志目录
  /var/spool/cron/tabs/root       cron文件的目录
  /var/adm/cron/log               cron执行日志目录

Red Hat Enterprise Linux Server release 6.1 (Santiago) 
概念:如果你作为ntp客户端,去跟别的服务器同步时钟,不需要启动什么服务,直接输ntpdate 10.3.64.61就可以了。如果你是作为ntp服务器(时钟源)的话,那么得启动ntpd服务,service ntpd start!
特点:当我们手工改变了时间后,ntp服务会自动修复时间差(不用我们人工参与),但不是实时的,需要一点时间。


1.安装NTP软件包
安装之前先检测是否这些软件包已安装,方法如下
[root@TJ-Unicom-Group-Gn-02 etc]# rpm -qa | grep ntp      有ntpdate-4.2.4p8-2.el6.x86_64和ntp-4.2.4p8-2.el6.x86_64包就ok了
ntpdate-4.2.4p8-2.el6.x86_64                                                一般在OS系统安装后都会自带这2个包
ntp-4.2.4p8-2.el6.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch


2.查看系统所在时区和修改系统时钟
[root@TJ-Unicom-Group-Gn-02 etc]# date -R
Wed, 15 Aug 2012 14:39:52 +0800
修改系统时钟
date -s "2012-08-14 09:00:00"        特点:当我们手工改变了时间后,ntp服务会自动修复时间差(不用我们人工参与),但不是实时的,需要一点时间。
或者
date -s 08/14/2012                   #月/日/年
date -s 09:37:00
或者
date MMDDhhmmYYYY.ss


3.启动/停止/重启/状态ntp服务
# /sbin/service ntpd start       启动
# /sbin/service ntpd stop       停止
# /sbin/service ntpd restart    重启    
# /sbin/service ntpd status     状态
[root@TJ-Unicom-Group-Gn-02 etc]# service ntpd start       启动NTP
Starting ntpd: [  OK  ]
[root@TJ-Unicom-Group-Gn-02 etc]# service ntpd status    正在运行NTP  【ntpd is stopped  已停止NTP】
ntpd (pid  630) is running...
                                        
4.设置时钟同步– NTP 配置文件模式(配置文件模式是长期有效,命令模式手工同步NTP临时有效) Red Hat 6.1版
NTP 修改配置文件/etc/ntp.conf
server [IP or hostname] [prefer]       其中[IP or hostname]为上级时间服务器的IP或者域名,主机名:[prefer]参数是可选的,加上prefer后,同步时会优先先访问这一条。
server  10.3.64.61                                                   上级时钟服务器的IP或者域名,设置时钟服务器(源),重启NTP服务即可
# local clock
fudge   127.127.1.0 stratum 10                                fudge设置Stratum(层)
[root@TJ-Unicom-Group-Gn-01 ntp]# echo 127.127.1.0 > /etc/ntp/step-tickers        把127.127.1.0 加入 step-tickers文件
[root@TJ-Unicom-Group-Gn-01 ntp]# cat /etc/ntp/step-tickers
127.127.1.0
[root@TJ-Unicom-Group-Gn-02 etc]# service ntpd restart                            重启NTP服务即可,如果不重启不能ntp -p 监控
Shutting down ntpd: [  OK  ]
Starting ntpd: [  OK  ]


5.监控NTP   date -s "2012-08-14 09:00:00"
     remote           refid      st t when poll reach   delay   offset  jitter
=============================================================================
*10.3.64.61      132.174.32.151   3 u   10   64   17    0.692   12.746  31.794
[root@TJ-Unicom-Group-Gn-02 etc]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter    只有启动NTP服务才能监控NTP
=============================================================================
*10.3.64.61      132.174.32.151   3 u   40   64  377    0.739  -70.969  24.713    如果发现在remote这列之前会出现星号和+号,说明已经跟这个服务器同步成功了,如果没有星号,说明正在同步还没有完成。
如果没有配置NTP初始化文件,即是 run NTP  也不能监控NTP服务 会报错 No association ID's returned  没有关联ID【如果不重启也不能监控ntp】

6.命令模式手工同步NTP(临时有效)
[root@TJ-Unicom-Group-Gn-01 ~]#  ntpdate 10.3.64.61                                                NTP命令模式,手工同步NTP
15 Aug 13:57:53 ntpdate[1187]: step time server 10.3.64.61 offset 243.079201 sec      补偿243秒
15 Aug 14:08:28 ntpdate[11893]: step time server 10.3.64.61 offset 315.259333 sec    补偿315秒
注意:
[root@TJ-Unicom-Group-Gn-03 ~]# ntpdate 10.3.64.61                                 
15 Aug 15:30:46 ntpdate[12948]: adjust time server 10.3.64.61 offset 0.385539 sec
[root@TJ-Unicom-Group-Gn-03 ~]# service ntpd status
ntpd is stopped
[root@TJ-Unicom-Group-Gn-03 ~]# service ntpd start                     当启动NTP服务后,在使用ntpdate 10.3.64.61【NTP在使用套接字,退出】
Starting ntpd: [  OK  ]
[root@TJ-Unicom-Group-Gn-03 ~]# service ntpd status
ntpd (pid  12975) is running...
[root@TJ-Unicom-Group-Gn-03 ~]# ntpdate 10.3.64.61
15 Aug 15:31:05 ntpdate[12986]: the NTP socket is in use, exiting
7.开机自动启动NTP服务
[root@TJ-Unicom-Group-Gn-03 ~]# chkconfig --help                                     打开帮助
chkconfig version 1.3.47 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage:   chkconfig [--list] [--type <type>] [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig --override <name>
         chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>   
[root@TJ-Unicom-Group-Gn-03 ~]# chkconfig --list | grep ntp                          开机不启动时显示off,0~6指系统的运行级别
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@TJ-Unicom-Group-Gn-02 etc]# chkconfig --level 2345 ntpd on               开机自动启动NTP服务  2345是系统运行级别
[root@TJ-Unicom-Group-Gn-02 etc]# chkconfig --list | grep ntp
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off                             开机自动启动显示on
ntpdate         0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@TJ-Unicom-Group-Gn-05 etc]# chkconfig --level 2345 ntpd on               开机自动启动显示on【中文版】
[root@TJ-Unicom-Group-Gn-05 etc]# chkconfig --list | grep ntp
ntpd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
ntpdate         0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭
 
#############################################################################
 
HP-UX TJGRDB B.11.31 U ia64 2891597638 unlimited-user license
1.HP-Unix 设置时钟同步– NTP 配置文件模式(配置文件模式是长久有效,命令模式临时有效)
   NTP 修改配置文件/etc/ntp.conf
   #trustedkey 3 4 5 6 14           # define trusted keys
   #requestkey 15                      # key (7) for accessing server variables
   #controlkey 15                       # key (6) for accessing server variables
   #authdelay 0.000159             # authentication delay (SPARC4c/65 SS1+ MD5)
   server 10.3.64.61                   设置时钟服务器(源),重启NTP服务即可
   fudge  127.127.1.1     stratum 10
   ftfile /etc/ntp.drift
   启动/停止ntp服务
   TJGRDB[#/etc]/sbin/init.d/xntpd stop   如果NTP服务the NTP socket is in use, exiting,请先stop服务再start服务
   TJGRDB[#/etc]/sbin/init.d/xntpd start  启动NTP服务,服务只有启动/停止,没有重启选项
   TJGRDB[#/etc]ntpq -p            监视NTP->只有启动NTP服务才能监视NTP,否则ntpq: read: Can't assign requested address请求的地址不能指定。
       remote           refid      st t when poll reach   delay   offset    disp
   =============================================================================
   *10.3.64.61      132.174.32.151   5 u   28   64   37     0.64    7.971  878.46
   NTP命令模式,手工同步NTP服务(配置文件模式是长久有效,命令模式临时有效)
   TJGRAPP[#/etc]ntpdate 10.3.64.61
    7 Jul 11:35:27 ntpdate[6382]: step time server 10.3.64.61 offset 412.957168 sec   同步时间服务,补偿412.957168秒
   TJGRAPP[#/etc]ntpdate 10.3.64.61
    7 Jul 12:36:54 ntpdate[25942]: adjust time server 10.3.64.61 offset 0.123488 sec  调整时间服务,补偿0.123488
2.使用crontab设置时钟同步NTP的任务计划
   0-59/10 * * * * /usr/sbin/ntpdate 10.3.64.61 &>/ntplog     每隔10分钟同步一次NTP时间
   0 10 * * * /usr/sbin/ntpdate 10.3.64.61 > /ntplog               每天10点整同步一次NTP时间并把执行结果重定向到ntplog文件
   cron文件的目录和cron执行日志目录
   /var/spool/cron/crontabs/root      cron文件的目录
   /var/adm/cron/log                          cron执行日志



原创粉丝点击