利用chrony和ntp搭建时间同步服务器
环境说明
系统版本 CentOS 6.9 x86_64
Network Time Protocol(NTP,网络时间协议)用于同步它所有客户端时钟的服务。NTP服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使本地网络的所有客户端能同步时钟。
同步时钟最大的好处就是相关系统上日志文件中的数据,如果网络中使用中央日志主机集中管理日志,得到的日志结果就更能反映真实情况。在同步了时钟的网络中,集中式的性能监控、服务监控系统能实时的反应系统信息,系统管理员可以快速的检测和解决系统错误。
利用chrony搭建步骤
1、安装
- [root@m01 ~]# yum install chrony –y
2、配置
修改默认配置,设置上游时间服务器地址、允许同步时间的客户端ip地址范围
- [root@m01 ~]# vim /etc/chrony.conf
- # Use public servers from the pool.ntp.org project.
- # Please consider joining the pool (http://www.pool.ntp.org/join.html).
- server ntp1.aliyun.com iburst #上游时间服务器的地址
- server ntp2.aliyun.com iburst
- server ntp3.aliyun.com iburst
- server ntp4.aliyun.com iburst
- # Ignore stratum in source selection.
- stratumweight 0
- # Record the rate at which the system clock gains/losses time.
- driftfile /var/lib/chrony/drift
- # In first three updates step the system clock instead of slew
- # if the adjustment is larger than 10 seconds.
- makestep 10 3
- # Enable kernel synchronization of the real-time clock (RTC).
- rtcsync
- # Allow NTP client access from local network.
- allow 192.168.1.0/24 #允许同步时间的ip范围
3、启动
- [root@m01 ~]# /etc/init.d/chronyd start
- Starting chronyd: [ OK ]
- [root@m01 ~]# netstat -lntup|grep 123 #本地会启用123端口
- udp 0 0 0.0.0.0:123 0.0.0.0:* 21133/chronyd
4、测试
4.1、服务端测试
修改时间同步服务器的时间,等待片刻再查看时间
- [root@m01 ~]# date -s 20170514
- Sun May 14 00:00:00 CST 2017
- [root@m01 ~]# date
- Tue May 15 17:52:39 CST 2018
4.2、在其他机器测试
修改db01的时间,执行命令ntpdate 192.168.1.61(即自己搭建的时间同步服务器的内网ip)同步时间
- [root@db01 ~]# date -s 20170501
- Mon May 1 00:00:00 CST 2017
- [root@db01 ~]# ntpdate 192.168.1.61
- 15 May 17:55:18 ntpdate[2414]: step time server 192.168.1.61 offset 32810061.803564 sec
- [root@db01 ~]# date
- Tue May 15 17:55:21 CST 2018
利用ntp搭建步骤
1、安装
- [root@m01 ~]# yum install ntp ntpdate -y
2、配置
- [root@m01 ~]# vim /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、default定义默认访问规则,nomodify禁止远程主机修改本地服务器(默认即可)
- restrict -6 default kod nomodify notrap nopeer noquery
- #配置,notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(默认即可)
- # 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
- restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
- #指定的192.168.1.0--192.168.1.254的服务器都可以使用ntp服务器来同步时间
- server 192.168.1.61 #指定本机作为局域网中的ntp服务器
- # 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 ntp1.aliyun.com iburst #指定上游时间服务器
- server ntp2.aliyun.com iburst
- server ntp3.aliyun.com iburst
- server ntp4.aliyun.com iburst
- 后面内容省略
3、启动
- [root@m01 ~]# /etc/init.d/ntpd start #启动
- Starting ntpd: [ OK ]
- [root@m01 ~]# netstat -lntup|grep 123 #检查,默认使用123端口
- udp 0 0 192.168.1.61:123 0.0.0.0:* 21345/ntpd
- udp 0 0 10.0.0.61:123 0.0.0.0:* 21345/ntpd
- udp 0 0 127.0.0.1:123 0.0.0.0:* 21345/ntpd
- udp 0 0 0.0.0.0:123 0.0.0.0:* 21345/ntpd
- udp 0 0 fe80::20c:29ff:fe1b:6ae:123 :::* 21345/ntpd
- udp 0 0 fe80::20c:29ff:fe1b:6b8:123 :::* 21345/ntpd
- udp 0 0 ::1:123 :::* 21345/ntpd
- udp 0 0 :::123 :::* 21345/ntpd
4、测试
4.1、服务端执行ntpq -p检查
- [root@m01 ~]# ntpq -p
- remote refid st t when poll reach delay offset jitter
- ==============================================================================
- 192.168.1.61 .INIT. 16 u - 64 0 0.000 0.000 0.000
- *time5.aliyun.co 10.137.38.86 2 u 59 64 3 23.174 15.665 9.655
- +120.25.115.19 10.137.53.7 2 u 58 64 3 52.023 14.674 9.927
- +203.107.6.88 10.137.55.181 2 u 57 64 3 39.007 4.241 0.497
当所有远程服务器(不是本地服务器)的jitter值都为4000,并且reach和dalay的值是0时,就表示时间同步有问题。可能原因有2个:
1)服务器端的防火墙设置,阻断了123端口(可以用 iptables -t filter -A INPUT -p udp --destination-port 123 -j ACCEPT 解决)
2)每次重启ntp服务器之后,大约3-5分钟客户端才能与服务端建立连接,建立连接之后才能进行时间同步,否则客户端同步时间时会显示
若遇到no server suitable for synchronization found的报错信息,稍等片刻再测试即可
4.2、在其他机器测试
修改db01的时间,执行命令ntpdate 192.168.1.61(即自己搭建的时间同步服务器的内网ip)同步时间
- [root@db01 ~]# date -s 20170501
- Mon May 1 00:00:00 CST 2017
- [root@db01 ~]# ntpdate 192.168.1.61
- 15 May 17:55:18 ntpdate[2414]: step time server 192.168.1.61 offset 32810061.803564 sec
- [root@db01 ~]# date
- Tue May 15 17:55:21 CST 2018
博主原创文章,转载请务必注明出处