百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> Linuxt系统ntp时间服务器环境配置
分享文章到:

Linuxt系统ntp时间服务器环境配置

发布时间:01/15 来源: 浏览: 关键词:
ntp就是一个网络时间协议了,很多的主机都可以通过ntp来对时间了,下面一起来看看Linuxt系统ntp时间服务器环境配置例子吧。


环境:

ntp server:CentOS 6.6 x86_64 192.168.8.250
ntp client:CentOS 6.6 x86_64 192.168.8.90

一、NTP介绍

1.ntp服务介绍

NTP(The Network Time Protocol)是网络时间协议,用以同步网络内计算机的时间。
它通过udp包交换,用特定算法进行协商,从而把计算机上的时间与时间服务器上的时间保持 一致。通过互联网它支持的误差是10ms,局域网则可以达到200微秒。
NTP时间服务器分为多层,从0层到4层,每层依次与上一次服务器同步,最高层的服务器则直接连接一个高精度的时间设备,比如原子钟、GPS或者电波时等。
自己搭建时间服务器需要指定一个上层的时间服务器,然后它可以向局域网内的其它机器提供同步服务。

2.ntp时间同步方式选择

ntp同步方式在linux下一般两种:使用ntpdate命令直接同步和使用ntpd服务平滑同步。两者区别如下:
现有一台设备,系统时间是13:00,真实的当前时间(准备同步的上级时间服务器)是12:30。如果我们使用ntpdate同步(ntpdate -u 目标ntp服务器IP),操作系统的时间立即更新为12:30,假如,我们的系统有个定时更新时间的任务,是在每天12:140运行,那么实际今天这个任务已经运行过了(当前时间是12:00嘛),现在被ntpdate修改为12:30,那么意味着10分钟后,又会执行一次任务。这个在生产环境上是有风险的,比如说数据库记录写入时间之类的。为了解决这个问题的方法就是时间平滑更改,不会让一个小时间点在一天内经历两次,这就是ntpd服务方式平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的。

二、安装

官网:http://www.ntp.org/
可以使用编译安装也可以直接使用yum进行安装
#.编译安装

版本号:ntp-4.2.8p2
cd /usr/local/src
wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p2.tar.gz
tar -zxvf ntp-4.2.8p2.tar.gz
cd ntp-4.2.8p2
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make -j3 && make install

#yum安装
yum -y install ntp
chkconfig ntpd on

三、配置文件

1、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
# 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 iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
#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
# 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

2、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、nptc等指令来查询时间服务器,等于不提供ntp的网络修正服务;
    notrap    不提供trap这个远端事件登录(remote event logging)的功能
    notrust    拒绝没有认证的用户端
restrict配置示例:
# 允许本地所有操作
restrict 127.0.0.1
restrict -6 ::1
#允许的局域网络段
restict 10.0.0.0 mask 255.0.0.0 nomodify notrap
restrict 192.168.0.0 maks 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 10
3)driftfile
driftfile记录时间误差,设定的方式如下:
driftfile [可以被ntpd写入的目录与文档]
因为预设的NTP server本身的时间计算是依据BIOS的晶片震荡周期频率来计算的,但是这个数值与上层Time Server不见得会一致,所以NTP这个daemon(ntpd)会自动的去计算我们自己主机的频率与上层Time server的频率,并且将两个频率的误差记录下来,记录下来的档案就是在driftfile后面接的完整文件当中。
注意:
driftfile后面接的档案需要使用完整路径档名;
该档案不能是连结档
该档案需要设定成ntpd这个daemon可以写入的权限;
该档案所记录的数值单位为:百万分之一秒(ppm)。

3、操作管理

启动服务    /etc/init.d/ntpd start
查看ntp服务的运行状况
watch ntpq -p

参数说明:
    * :响应的NTP服务器归精确的服务器
    remote:它指的就是本地机器所连接的远程NTP服务器
    refid:它指的是给远程服务器(eg.210.72.145.44)提供的时间同步的服务器
    st:远程服务器的层级别(stratum)。由于NTp是层型结构,有顶端的服务器,多层的Relay Server再到客户端。所以服务器从高到低可以设定为1-16。为了减缓负荷和网络堵塞,原则 上应该避免直接连接到级别为1的服务器的。
    when:上一次成功请求之后到现在的秒数
    poll:本地机和远程服务器多少时间进行一次同步(单位为秒)。在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快的调整到正常的时间范围。之后poll值会逐渐增大,同步的频率也就会相应减小
    reach:这是一个八进制值,用来测试能否和服务器连接,每成功连接一次它的值就会增加
    offset:这是个最关键的值,它告诉了我们本地机和服务器之间的时间差别。offset越接近于0,我们就和服务器的时间越接近
    jitter:这是一个用来做统计的值,它统计了在特定个连续的连接数里offset的分布情况,简单地说这个数值的绝对值越小,我们和服务器的时间就越精确。

四、客户端同步
同步命令:

ntpdate 192.168.8.250    (时间服务器IP)
自动同步时间:
crontab -e
0 1 * * * /usr/sbin/ntpdate 192.168.8.250    每天凌晨1点同步时间
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/Shanghai””的内容,这表示我们的时间设定档案要使用/usr/share/zoneinfo/Asia/Shanghai设置文件
/etc/localtime
这个档案是本地端的时间设定文件,clock文件里面规定了使用的时间设置文件为/usr/share/zoneinfo/Asia/Shanghai,此时Linux系统就会将Shanghai那个文件复制一份成为/etc/localtime,系统时间显示就会以Shanghai那个时间设定文件为准了。
/bin/date
这个是Linux上面常见的日期与时间相关指令
/sbin/hwclock
这是一个root才能执行的指令,因为Linux系统上面BIOS时间与Linux系统的时间是分开的,所以使用date这个指定调整了时间之后,还需要使用hwclock才能将修改后的时间写入BIOS中。
/usr/sbin/ntpd
这就是NTP的主要daemon文件,要启动他才能提供NTP服务。
/usr/sbin/ntpdate
这个就是Client端用来连接NTP Server的主要执行档案,如果没有要启用NTp仅想使用NTP Client功能的话,那么只会用到这个指令了。
/usr/sbin/ntptrace
这个指令可以用来追踪某部时间服务器时间对应关系

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有5人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板