欢迎访问我的独立博客

ntpd时间同步 安装与配置

1,安装
yum -y install ntp
vim /etc/ntp.conf
默认配置:
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
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
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
修改:
允许任何IP的客户机都可以进行时间同步
将“restrict default kod nomodify notrap nopeer noquery”这行修改成:
restrict default nomodify
启动前先关闭防火墙
service ntpd start
客服端测试
ntpdate  192.168.1.101
 最好客户端安装ntpd,指定时间server为上面配置的服务器地址,推荐
server 192.168.1.101
ntpd是步进式的逐渐调整时间,测试时如果你的ntpd服务器时间是准的,客户端不一定要ntpd服务启动时就自动对准,但过一段时间,就同步了。
TODO
加上防火墙后测试:
==============艺搜参考==========
 为什么要使用ntpd而不是ntpdate?

原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。


原理
    1. 首先主机启动NTP。
    2. 客户端会向NTP服务器发送调整时间的message。
    3. 然后NTP server会送出当前的标准时间给client
    4. client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。

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


设置时区:cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum -y install ntp
vim /etc/ntp.conf
server 127.127.1.0   # 允许本机地址一切的操作
fudge 127.127.1.0 stratum 8   //这两行在任意地方添加
 service ntpd start  //启动ntp服务
客户端同步:
ntpdate  192.168.1.105
ntpdate: no server suitable for synchronization found 
原因:1,防火墙原因
iptables  -I  INPUT -p udp  --dport  123  -j ACCEPT    //ntp  默认使用 udp 的123号端口
客户端安装ntpd,指定时间server为上面配置的服务器地址,推荐



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

 ntpq –p     #查看本机和上层服务器的时间同步结果










wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p8.tar.gz
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
允许任何IP的客户机都可以进行时间同步
将“restrict default kod nomodify notrap nopeer noquery”这行修改成:
restrict default nomodify

fudge 127.127.1.1 stratum 10 //这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
driftfile /etc/ntp/drift //记录上次的NTP server与上层NTP server联接所花费的时间

只允许192.168.18.***网段的客户机进行时间同步
在restrict default nomodify notrap noquery(表示默认拒绝所有IP的时间同步)之后增加一行:
restrict 192.168.18.0 mask 255.255.255.0 nomodify
以守护进程启动ntpd
/usr/local/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid
NTP(The Network Time Protocol) 是网络时间协议
自己搭建时间服务器需要指定一个上层的时间服务器,然后它可以向局域网内的其它机器 
提供同步服务。
restrict IP地址 mask 子网掩码 参数 
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP 
参数有以下几个: 
ignore :关闭所有的 NTP 联机服务 
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。 
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网 
noquery :不提供客户端的时间查询 
注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!
posted @ 2016-09-30 17:57  github.com/starRTC  阅读(890)  评论(0编辑  收藏  举报