NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。
NTP服务器就是利用NTP协议提供时间同步服务的。
# 系统自带ntp [root@m01 ~]# rpm -aq ntp ntp-4.2.6p5-10.el6.centos.x86_64 #如果系统没有ntp,就安装 yum -y install ntp
[root@m01 ~]# vim /etc/ntp.conf #restrict default kod nomodify notrap nopeer noquery restrict 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 iburst server ntp1.aliyun.com server time.nist.gov #使用sed修改即可 sed -i ‘s@restrict default kod nomodify@#restrict default kod nomodify@g‘ /etc/ntp.conf sed -i ‘8a restrict default nomodify‘ /etc/ntp.conf sed -i ‘25a server ntp1.aliyun.com\nserver time.nist.gov‘ /etc/ntp.conf sed -i ‘s@server 0.centos@#server 0.centos@g‘ /etc/ntp.conf sed -i ‘s@server 1.centos@#server 1.centos@g‘ /etc/ntp.conf sed -i ‘s@server 2.centos@#server 2.centos@g‘ /etc/ntp.conf sed -i ‘s@server 3.centos@#server 3.centos@g‘ /etc/ntp.conf
# 如果计划任务有时间同步,先注释,两种用法会冲突。 [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 start Starting 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 ~]# ntpstat synchronised 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.61 11 Jan 18:35:10 ntpdate[4462]: the NTP socket is in use, exiting
[root@m01 ~]# chkconfig ntpd on [root@m01 ~]# chkconfig --list ntpd ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
客户机要等几分钟再与新启动的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.61 11 Jan 18:39:56 ntpdate[3623]: step time server 172.16.1.61 offset 1017582.583577 sec [root@backup ~]# date Wed Jan 11 18:39:58 CST echo "#tim sync with m01(172.16.1.61) by ersa" >> /var/spool/cron/root echo "*/5 * * * * /usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1" >> /var/spool/cron/root
原文:http://iersa.blog.51cto.com/9257603/1891163