目录

NTP Server... 1

Linux Client 端时间同步... 3

Windows Client端时间同步... 4

windows简介:... 4

NTP服务端主配置文件/etc/ntp.conf 权限参数详解... 5


在搭建NTP时间服务器前,我先查看下本机的系统时间和bios时间,发现都不对

今天的准确时间是:2013 0720星期六 18:13:43

我查看的时间是:

[root@localhost~]# date

2013 02 24星期日 09:38:47 CST

[root@localhost~]# hwclock

20130224星期日 093853 -0.138265 seconds

介于这种状况,我现在开始搭建NTP时间服务器



NTP Server

1.软件包安装

[root@localhost~]# rpm -e ntp-4.2.2p1-9.el5.centos.2 --nodeps

[root@localhost~]# mount -o loop /dev/cdrom /mnt/

[root@localhost~]# rpm -ivh /mnt/CentOS/ntp-4.2.2p1-8.el5.centos.1.i386.rpm

Preparing... ########################################### [100%]

1:ntp ###########################################[100%]


PS:不能同时启动ntpdntpdate命令

ntpd:服务端程序

ntpdate:客户端程序

2.修改配置文件

1[root@localhost ~]# cat /etc/ntp.conf

restrict defaultkod nomodify notrap nopeer noquery

restrict -6default kod nomodify notrap nopeer noquery

restrict127.0.0.1

restrict -6 ::1

restrict192.168.115.110 mask 255.255.255.0 nomodify notrap[U1]

restrict133.100.11.8 mask 255.255.255.0 nomodify notrap[U2]


server133.100.11.8 prefer[U3]

server ntp.nasa.gov

server0.centos.pool.ntp.org

server1.centos.pool.ntp.org

server2.centos.pool.ntp.org



server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10


driftfile/var/lib/ntp/drift


keys /etc/ntp/keys

logfile/var/log/ntp[U4]




2[root@localhost ~]# cat /etc/ntp/step-tickers [U5]

server133.100.11.8

serverntp.nasa.gov


3[root@localhost ~]# vim /etc/sysconfig/ntpd

# Drop root toid 'ntp:ntp' by default.

OPTIONS="-untp:ntp -p /var/run/ntpd.pid"

# Set to 'yes'to sync hw clock after successful ntpdate

SYNC_HWCLOCK=yes[U6]

# Additionaloptions for ntpdate

NTPDATE_OPTIONS=""


4[root@localhost ~]# service ntpd restart

ntpd: 同步时间服务器:[确定]

同步硬件时钟到系统时钟 [确定]

启动 ntpd[确定]

[root@localhost~]# netstat -lnup|grep ntpd #查看123端口是否打开

udp 0 0 192.168.115.110:123 0.0.0.0:* 4359/ntpd

……..


5)开机自启动设置

[root@localhost~]# chkconfig --list ntpd

ntpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

[root@localhost~]# chkconfig --level 35 ntpd on

[root@localhost~]# chkconfig --list ntpd

ntpd 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭


6)查看时间是否正确

[root@localhost~]# date #查看系统时间

2013 07 20星期六 18:39:46 CST

[root@localhost~]# hwclock #查看Bios时间

20130720星期六 190732 -0.491867 seconds


7)设置防火墙,不然客户端不能与NTP Server同步

[root@localhost~]# iptables -A INPUT -p UDP -i eth0 -s 192.168.115.0/24 --dport 123 -j ACCEPT


8)查看日志信息

[root@localhost~]# cat /var/log/ntp

20 Jul 18:15:46ntpd[4359]: synchronized to LOCAL(0), stratum 10

20 Jul 18:15:46ntpd[4359]: synchronized to 202.112.31.197, stratum 2

20 Jul 18:21:05ntpd[4359]: synchronized to 133.100.11.8, stratum 1



9[root@localhost ~]# ntpstat

unsynchronised

polling server every 64 s

[root@localhost~]# ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

*clock.tl.fukuok.GPS. 1 u 83 64 326 226.603 -31.129 20.566

xntp-nasa.arc.na.GPS. 1 u 21 64 273 339.440 -143.72 13.075

+dns1.synet.edu.202.118.1.46 2 u 16 64 377 67.670 -51.317 13.595

+dns2.synet.edu.202.118.1.46 2 u 21 64 377 66.114 -50.780 13.107

LOCAL(0) .LOCL. 10 l 24 64 377 0.000 0.000 0.015


PSntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“noserver suitable for synchronization found”错误。


Linux Client 端时间同步

1)检查是否有ntpdate命令

[root@localhost~]# which ntpdate

/usr/sbin/ntpdate

该命令也由软件包ntp-4.2.2p1-8.el5.centos.1.i386.rpm提供


2)查看系统时间和Bios时间

[root@localhost~]# date

2013 07 19星期五 18:09:25 EDT

[root@localhost~]# hwclock

20130720星期六 183118 -0.021029 seconds


3)现在命令行执行同步命令,看是否ok

[root@localhost~]# /usr/sbin/ntpdate 192.168.115.110

20 Jul 06:40:08ntpdate[10706]: step time server 192.168.115.110 offset -43202.020606 sec


4)再次查看系统时间和Bios时间,验证同步是否成功

[root@localhost~]# date

2013 07 20星期六 06:40:16 EDT

[root@localhost~]# hwclock

20130720星期六 184022 -0.019742 seconds


5)此时,系统时间已经同步。下面我们要把它放入定时任务,且要把同步的系统时间写入BIOS

[root@localhost~]# crontab –e #NTP Server的同步操作放入定时任务

10 6 * * */sbin/hwclock -w > /dev/null

[root@localhost~]# crontab –l #查看计划任务

10 6 * * */sbin/hwclock -w > /dev/null


[root@localhost123 yum.repos.d]# cat>/var/spool/cron/root

10 6 * * * /sbin/hwclock -w > /dev/null


Windows Client端时间同步

sc config W32Time start= AUTO

net start W32Time

w32tm /config/syncfromflags:manual /manualpeerlist: 192.168.115.110

w32tm /config /update

w32tm /resync


windows简介:

微软从Windows 2000后支持NTP,由Win32 Time服务提供。

配置的命令行代码如下::

rem : 将时间同步服务设为自动,默认被禁用 :

sc config W32Time start= AUTO

rem : 启动时间同步服务 :

net start W32Time

rem : 设置同步选项192.168.115.110 为时间服务器ip

w32tm /config /syncfromflags:manual/manualpeerlist: 192.168.115.110


rem : 更新同步选项 :

w32tm /config /update

rem : 执行同步 :

w32tm /resync

命令执行截图:

212308784.jpg

此时,windows时间同步ok


NTP服务端主配置文件/etc/ntp.conf 权限参数详解


权限的设定主要以restrict 这个参数来设定,格式如下:

restrict IP地址 mask 子网掩码参数

其中 IP 可以是IP地址,也可以是default default 就是指所有的IP

参数有以下几个:

ignore :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询

注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制。

server这个参数设定上级时间服务器,格式如下:

server IP地址或域名 [prefer]

IP地址或域名就是我们指定的上级时间服务器,如果 Server 参数最后加上 prefer,表示我们的 NTP 服务器主要以该部主机时间进行校准。

driftfile格式如下:

driftfile 文件名

在与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。

注意: driftfile 后面接的文件需要使用完整的路径文件名,不能是链接文件,并且文件的权限需要设定成 ntpd守护进程可以写入。