1. NTP简介
NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。
NTP服务器就是利用NTP协议提供时间同步服务的。
2. NTP服务器安装
# 系统自带ntp[root@m01 ~]# rpm -aq ntpntp-4.2.6p5-10.el6.centos.x86_64#如果系统没有ntp,就安装yum -y install ntp
3. 配置NTP服务
[root@m01 ~]# vim /etc/ntp.conf#restrict default kod nomodify notrap nopeer noqueryrestrict default nomodify# nomodify客户端可以同步# 将默认时间同步源注释改用可用源#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 iburstserver ntp1.aliyun.comserver time.nist.gov#使用sed修改即可sed -i 's@restrict default kod nomodify@#restrict default kod nomodify@g' /etc/ntp.confsed -i '8a restrict default nomodify' /etc/ntp.confsed -i '25a server ntp1.aliyun.com\nserver time.nist.gov' /etc/ntp.confsed -i 's@server 0.centos@#server 0.centos@g' /etc/ntp.confsed -i 's@server 1.centos@#server 1.centos@g' /etc/ntp.confsed -i 's@server 2.centos@#server 2.centos@g' /etc/ntp.confsed -i 's@server 3.centos@#server 3.centos@g' /etc/ntp.conf
4. 启动NTP服务器
# 如果计划任务有时间同步,先注释,两种用法会冲突。[root@m01 ~]# crontab -l#time sync by ersa at 2016-12-26#*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1[root@m01 ~]# /etc/init.d/ntpd startStarting ntpd: [root@m01 ~]# ntpq -p remote refid st t when poll reach delay offset jitter==============================================================================+time5.aliyun.co 10.137.38.86 2 u 103 128 377 3.758 -1.650 33.961*nist1-lnk.binar .ACTS. 1 u 121 128 377 227.922 1.217 26.958[root@m01 ~]# ntpstatsynchronised to NTP server (216.229.0.179) at stratum 2 time correct to within 125 ms polling server every 128 s[root@m01 ~]# ntpdate 10.0.0.6111 Jan 18:35:10 ntpdate[4462]: the NTP socket is in use, exiting
5. NTP服务器开机启动
[root@m01 ~]# chkconfig ntpd on[root@m01 ~]# chkconfig --list ntpdntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6. 客户机时间同步
客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found
错误
#客户端测试[root@backup ~]# date -s "20161231"Sat Dec 31 00:00:00 CST #172.16.1.61 是服务器 m01(10.0.0.61) 的内网地址[root@backup ~]# ntpdate 172.16.1.6111 Jan 18:39:56 ntpdate[3623]: step time server 172.16.1.61 offset 1017582.583577 sec[root@backup ~]# dateWed Jan 11 18:39:58 CST echo "#tim sync with m01(172.16.1.61) by ersa" >> /var/spool/cron/rootecho "*/5 * * * * /usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1" >> /var/spool/cron/root