NTP时间服务器:

使用环境:当同一个程序运行在多台服务器上时,有时需要这些服务器的时钟相同,但是当服务器运行一定时间之后,各个服务器上的时钟会有一定差异,此时就需要让所有服务器自动校准各自时钟,以下解决应用于不连接外网的局域网,需求是同步所有服务器时钟.

NTP通信协议原理:

1.首先主机启动NTP。

2.客户端会向NTP服务器发送调整时间的message。

3.然后NTP server会送出当前的标准时间给client

4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。

NTP这个deamon采用了123端口。(UDP)

“当我们要利用Time server来进行实时的同步更新时,就需要使用NTP软件提供的ntpdate来连接端口123”

选定其中一台可连接外网的服务器作为基准服务器,即提供时钟服务,局域网ip为192.168.1.1:

首先检查是否安装了组建:

[root@testmechine ~]# rpm -qa ntp 
ntp-4.2.2p1-9.el5.centos.1

假如没有安装则yum install ntp来进行安装。

所需要的配置文件:

1./etc/ntp.conf

linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。

2./usr/share/zoneinfo/

这个里面规定了这个主要时区的时间设置文件。

3./etc/sysconfig/clock

这个文件是linux的主要时区设置文件,每次开机后linux会自动读取这个文件来设置系统所默认的显示时间,可以看看它里面到底设置了什么:

[root@testmechine sysconfig]# cat /etc/sysconfig/clock 
# The ZONE parameter is only evaluated by system-config-date. 
# The timezone of the system is defined by the contents of /etc/localtime. 
ZONE="Asia/Shanghai" 
UTC=true 
ARC=false

4./etc/localtime

本地端时间配置文件。

5./bin/date

这个是时间的修改命令,除了输出时间,还可以修改时间。

6./sbin/hwclock

因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。

7./usr/sbin/ntpd

这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。

8./usr/sbin/ntpdate

这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。

9,/usr/sbin/ntptrace

可以用来追踪某台时间服务器的时间对应关系。

关于ntp.conf的设置:

[root@testmechine ~]# cat /etc/ntp.conf 
# Permit time synchronization with our time source, but do not 
# permit the source to query or modify the service on this system. 
restrict default kod nomodify notrap nopeer noquery 
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could 

# be tightened as well, but to do so would effect some of 
# the administrative functions. 
restrict 127.0.0.1 
restrict -6 ::1

# Hosts on local network are less restricted. 
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

#其中:

#用restrict控管权限
#nomodify - 用户端不能更改ntp服务器的时间参数
#noquery - 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
#notrap - 不提供trap远端登陆

# Use public servers from the pool.ntp.org project. 
# Please consider joining the pool (http://www.pool.ntp.org/join.html). 

# 用server设定上层ntp服务器,“prefer”表示优先

server 210.72.145.44 prefer #以这台时间服务器主机最优先 
server 0.centos.pool.ntp.org 
server 1.centos.pool.ntp.org 
server 2.centos.pool.ntp.org

server  127.127.1.0     # local clock 

fudge   127.127.1.0 stratum 10

# Drift file.  Put this in a directory which the daemon can write to. 
# No symbolic links allowed, either, since the daemon updates the file 
# by creating a temporary in the same directory and then rename()'ing 
# it to the file. 
driftfile /var/lib/ntp/drift

# Key file containing the keys and key identifiers used when operating 
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted. 
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility. 
#requestkey 8

# Specify the key identifier to use with the ntpq utility. 
#controlkey 8 
[root@testmechine ~]#

以上是ntp.conf服务器端的设置。

然后我们修改/etc/sysconfig/ntpd:

# Drop root to id 'ntp:ntp' by default. 
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"

# Set to 'yes' to sync hw clock after successful ntpdate 
SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改

# Additional options for ntpdate 
NTPDATE_OPTIONS=""

启动NTP:

[root@testmechine ~]# /etc/init.d/ntpd start 
Starting ntpd: [  OK  ]、

看看端口启动情况:

[root@testmechine ~]# netstat -nltpu|grep ntpd 
udp        0      0 192.168.0.20:123            0.0.0.0:*                               7237/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               7237/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               7237/ntpd           
udp        0      0 ::1:123                     :::*                                    7237/ntpd           
udp        0      0 fe80::20a:ebff:fe93:123     :::*                                    7237/ntpd           
udp        0      0 :::123                      :::*                                    7237/ntpd           
[root@testmechine ~]#

但是要15分钟才会和上层的NTP服务器顺利连接上,耐心等待。

 

chkconfig ntpd on // 让ntp服务开机启动 

初始同步
# ntpdate -u 210.72.145.44

确认同步成功
# ntpq -p

查看ntp服务器有无和上层连通

# ntpstat

查看ntp服务器与上层间的联系

ntptrace -n 127.0.0.1

查看ntp服务器与上层ntp服务器的状态
[root@S5 ~]# ntpq -p
其中,
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

 

当然,如果要提供时间服务,还必须相应的设置iptable防火墙的配置。CentOS的时间服务使用udp 123端口。

 

服务器算大功告成了,现在我们就看客户端怎么设置了。在另外一台机器上进行:

第一种方式:不想启动ntp服务,只使用ntp客户端ntpdate

#vi /etc/crontab

写入:

10 5 * * * root /usr/sbin/ntpdate 192.168.1.1 &&/sbin/hwclock -w

这样每天的5点10分,linux系统都会自动向我们的新建NTP时间服务器192.168.1.1 进行时间的同步操作。

第二种方式:
1.安装ntp服务器
2.修改/etc/ntp.conf,在server最前面加入
server 192.168.1.1
3.自动启动ntp
chkconfig ntpd on  
4.启动ntp
service ntpd start