立即注册 登录
About云-梭伦科技 返回首页

韩克拉玛寒的个人空间 https://www.aboutyun.com/?9721 [收藏] [复制] [分享] [RSS]

日志

关于Centos安装配置NTP

热度 3已有 1695 次阅读2014-12-11 10:01 |个人分类:Linux

NTP是用来使系统和一个精确的时间源保持时间同步的协议。建议大家在自己管理的网络中建立至少一台时间服务器来同步本地时间,这样可以使得在不同的系统上处理和收集日志和管理更加容易,而且不会出错。

官方网站是:http://www.ntp.org/

从上面我们可以获得Linux下的源代码,以及互联网上的公开的NTP服务器,其地址列表如下:
http://ntp.isc.org/bin/view/Servers/NTPPoolServers
http://ntp.isc.org/bin/view/Servers/WebHome

一、NTP服务原理
NTP协议是通过Server和Client的方式进行的,通讯的过程是:
1、主机需要启动这个NTP服务
2、Client会向NTP Server发送出调校时间的message
3、然后NTP Server会送出目前的标准时间给Client
4、Client接收了来自Server的时间后,会据以调整自己的时间,以实现网络校时。

虽然网络上有很多公开的NTP服务器,但在使用的时候需要注意。由于网络传输是有延时的,当Client对一个延时比较厉害的服务器发送请求后,直到最 后Client获得时间,期间可能会出现一个由于网络延时导致的的时间差。所以,建议大家选择离自己最近的NTP服务器作为校验的标准。

二、安装
首先,编译安装ntp server
rpm -qa | grep ntp
若没有找到,则没有安装NTP服务。
NTP可以使用yum直接安装
# yum install -y  ntp

还可以下载源码编译安装
# tar zxvf ntp-4.2.6.tar.gz
# cd ntp-4.2.6
# ./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
# make && make install

三、NTP配置
NTP的配置文件/etc/ntp.conf默认配置内容为:

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# 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 notrap
#restrict 127.0.0.1 mask 255.0.0.0
#resrrict 192.168.56.0 mask 255.255.255.0 nomodify notrap

# 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 (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org    //发现在Centos中 配置这个没用
#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
server 192.168.56.100  # 考虑到我网络配置是  host-key。所以配置了我本地一台master主机

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

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
server 192.168.56.100 prefer #the comptuar is true
#server 192.168.56.101
#server 192.168.56.102
#server 192.168.56.103

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 8

# Enable public key cryptography.
#crypto

#includefile /etc/ntp/crypto/pw

# 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

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats              

四、NTP的配置文件/etc/ntp.conf说明

1)restrict
在ntp.conf档案内可以使用restrict控制权限,设定方式为:
restrict [IP] mask [Netmask IP] [Parameter]
restrict <IP 地址> <子网掩码> | <网段> <子网掩码> [ignore|noquery|notrap|nomodiy|notrust|nokod]

其中parameter的参数主要有底下这些:
ignore
拒绝所有类型的NTP连线;

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

noquery
用户端不能够使用ntpq、ntpc等指令来查询时间伺服器,等于不提供NTP的网路矫正服务;

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

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

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

restrict配置示例:

# 允许本地所有操作
restrict 127.0.0.1 
restrict -6 ::1

# 允许的局域网络段
restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap

开放这个网段(192.168.1.1 ~ 192.168.1.255)但不能修改
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# 开放这个(192.168.1.1)IP但不能修改
restrict 192.168.1.1 mask 255.255.255.0 nomodify notrap

2)server
server设定上层NTP伺服器,设定方式为:
server [IP or Hostname] [prefer]

在server后端可以接IP或主机名,perfer表示优先使用。

server示例:
# 使用上层的internet ntp服务器
server 220.130.158.71 prefer
server 220.130.158.51
# 如果无法与上层ntp server通信以本地时间为标准时间
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 8  (stratum 范围值:0-15)

3)driftfile
driftfile记录时间误差,设定的方式如下∶
driftfile [可以被 ntpd 写入的目录与档案]

因为预设的NTP Server本身的时间计算是依据 BIOS 的晶片震荡周期频率来计算的,但是这个数值与上层Time Server不见得会一致啊。所以NTP这个daemon (ntpd) 会自动的去计算我们自己主机的频率与上层Time server的频率,并且将两个频率的误差记录下来,记录下来的档案就是在driftfile后面接的完整档名当中。

注意:
driftfile后面接的档案需要使用完整路径档名;
该档案不能是连结档;
该档案需要设定成ntpd这个daemon可以写入的权限;
该档案所记录的数值单位为∶百万分之一秒 (ppm)。

五、启动服务
# /etc/rc.d/init.d/ntpd start
或者
# service ntpd start
为了使NTP服务可以在系统引导的时候自动启动,执行:
# chkconfig ntpd on

六、验证服务端同步
在ntp server上启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令进行同步时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:
# watch ntpq -p

Every 2.0s: ntpq -p                                     Tue Jun  5 19:58:11 2012

remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+61.153.197.226  204.152.184.72   2 u  126  128  377   82.990  -14.722   5.011
*114.80.81.13    216.218.192.202  2 u   71  128  377   54.234   -9.614   8.453

七、客户端同步
客户端也要安装ntp:  yum install -y ntp

在客户端使用ntpdate进行更新时间
# ntpdate  192.168.56.100

也可以设置自动调整时间
# crontab -l   查看crontab的查看脚本命令
# crontab -e

编辑文件添加下面一行
13 5,9,14,19 * * * /usr/sbin/ntpdate 192.168.56.100

该语句是在5、9、14、19点13分对时。
备注:NTP服务,只在服务端启动,客户端不用启动。


八、NTP相关档案和指令

/etc/ntp.conf
这是NTP唯一配置文件。

/usr/share/zoneinfo/
这个目录不是NTP提供的而是Linux本身提供的,在这个目录下的档案规定了各主要时区的时间设定档案。例如台湾地区的时区设定档案在/usr/share/zoneinfo/Asia/Taipei,这个目录里面的档案与下面要谈的两个档案clock与localtime是有关系的。

/etc/sysconfig/clock
这个目录不是NTP提供的而是Linux的主要时区设定档案啊,每次开机后Linux 会自动的读取这个档案来设定自己系统所预设要显示的时间说。在我们台湾地区的本地时间设定中,这个档案内应该会出现一行“ZONE="Asia/Taipei"”的字样, 这表示我们的时间设定档案要使用/usr/share/zoneinfoe/Asia/Taipei设置档案。

/etc/localtime
这个档案是本地端的时间设定档,clock档案里面规定了使用的时间设定档 (ZONE) 为/usr/share/zoneinfo/Asia/Taipei,此时Linux系统就会将Taipei那个档案复制一份成为/etc/localtime,系统时间显示就会以Taipei那个时间设定档案为准。

如果现在我这部主机搬到日本东京去了,那么我应该如何调整时间呢?因为localtime主要是分析与UTC 时间的时差来显示的格式,只要将/etc/sysconfig/clock里面的ZONE设定成为Asia/Tokyo并且将/usr/share/zoneinfo/Asia/Tokyo复制到/etc/localtime,这样就能显示时间为日本东京的时间了。

/bin/date
这个是Linux系统上面常见的日期与时间相关指令。

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

/usr/sbin/ntpd
这就是NTP的主要daemon档案,要启动他才能提供NTP服务。这个指令预设会参考/etc/ntp.conf里面的设定喔。

/usr/sbin/ntpdate
这个就是Client端用来连接NTP Server的主要执行档案,如果您没有要启用NTP而仅想要使用NTP Client功能的话,那么只会用到这个指令而已啦。

/usr/sbin/ntptrace
这个指令可以用来追踪某部时间伺服器的时间对应关系,底下我们会介绍如何使用。

路过

雷人
2

握手

鲜花

鸡蛋

刚表态过的朋友 (2 人)

发表评论 评论 (3 个评论)

回复 poppowerlb2 2014-12-11 21:21
亲眼见证了作者花好几天的时间来搭建环境,作者的分享可以帮助很多人少走弯路,谢谢!
回复 韩克拉玛寒 2014-12-18 17:08
poppowerlb2: 亲眼见证了作者花好几天的时间来搭建环境,作者的分享可以帮助很多人少走弯路,谢谢!
哈哈,那叫一个痛苦啊
回复 Sherry 2015-2-4 18:30
  

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条