Chinaunix首页 | 论坛 | 博客
  • 博客访问: 424016
  • 博文数量: 121
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-16 16:28
文章分类

全部博文(121)

文章存档

2021年(3)

2018年(1)

2017年(5)

2016年(9)

2015年(23)

2014年(80)

我的朋友

分类: LINUX

2014-02-11 16:40:33

NTP时间服务器:


使用环境:当一个计算机群体需要需要进行时间的严格同步,即可使用NTP服务器。


NTP通信协议原理:


1.首先主机启动NTP。


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


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


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


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


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


安装:


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


[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时间服务器采用类似于分层的架构:




关于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


#添加如下命令:


restrict default nomodify noquery notrap 
restrict 202.112.10.60 
restrict 202.106.199.36 
restrict 202.112.7.150 
restrict 127.0.0.1 mask 255.0.0.0 
restrict 192.168.0.0 mask 255.255.255.0 nomodify


# 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


# Use public servers from the pool.ntp.org project. 
# Please consider joining the pool (). 
server 0.centos.pool.ntp.org 
server 1.centos.pool.ntp.org 
server 2.centos.pool.ntp.org


#进行如下替换


server time.buptnet.edu.cn 
server s1b.time.edu.cn 
server s1c.time.edu.cn 
server s1d.time.edu.cn


#broadcast 192.168.1.255 key 42         # broadcast server 
#broadcastclient                        # broadcast client 
#broadcast 224.0.1.1 key 42             # multicast server 
#multicastclient 224.0.1.1              # multicast client 
#manycastserver 239.255.254.254         # manycast server 
#manycastclient 239.255.254.254 key 42 # manycast client


# Undisciplined Local Clock. This is a fake driver intended for backup 
# and when no outside source of synchronized time is available.


#加入如下代码: 
server 202.112.10.60 prefer #以这台主机最优先 
server 202.106.199.36 
server 202.112.7.150


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服务器顺利连接上,耐心等待。


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


#vi /etc/crontab


写入:


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


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






备注: 


利用 restrict 来管理权限控制   在 ntp.conf 档案内可以利用『 restrict 』来控管权限,这个参数的设定方式为: 


restrict [你的IP] mask [netmask_IP] [parameter] 


其中 parameter 的参数主要有底下这些: 


     * 


       ignore 
       拒绝所有类型的 NTP 连线; 


     * 


       nomodiy 
       用户端不能更改 NTP 伺服器的时间参数,这即表示用户端不能使用 ntpc 与 ntpq 这两支程式来修改伺服器罗。 但用户端仍可透过这部主机来进行网路校时的; 


     * 


       noquery 
       用户端不能够使用 ntpq, ntpc 等指令来查询时间伺服器,等於不提供 NTP 的网路校时罗; 


     * 


       notrap 
       不提供 trap 这个远端事件登录 (remote event logging) 的功能。 


     * 


       notrust 
       拒绝没有认证的用户端。 


那如果你没有在 parameter 的地方加上任何参数的话,这表示『该 IP 或网段不受任何限制』的意思喔!一般来说,我们可以先关闭 NTP 的使用权限,然后在一个一个的启用允许登入的网段。 


     * 


       利用 server 设定上层 NTP 伺服器 


上层 NTP 伺服器的设定方式为: 


server [IP or hostname] [prefer] 


在 server 后端可以接 IP 或主机名称,鸟哥个人比较喜欢使用主机名称来设定说! 至於那个 perfer 表示『优先使用』的主机罗~有够简单吧!    


阅读(961) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~