
正文
NTP时间服务器的配置
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1、NTP简介
NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机,时间同步的一种协议。
NTP服务器利用NTP协议来提供时间同步服务。
2 、环境准备
主机名 服务器/客户端 外网IP内网IP
ntp-s Server10.0.0.11192.168.56.11
ntp-c Client - 192.168.56.12
3、安装NTP软件包
yum install -y ntp ntpdate
4、配置NTP
操作前备份
cp /etc/ntp.conf{,.ori}
修改配置文件,红色字体为修改部分

[root@ntp-s ~]# grep -vE "^$|#" /etc/ntp.conf
driftfile /var/lib/ntp/drift #<==记录上次的NTP server与上层NTP server联接所花费的时间
restrict default nomodify #<==允许所有(nomodify)主机进行时间同步
restrict 127.0.0.1
restrict ::1
server ntp1.aliyun.com #<==同步时间服务器,阿里云NTP服务器
server ntp2.aliyun.com prefer #<==优先使用该NTP服务器
server ntp3.aliyun.com
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

5、启动NTP
注意:如果有ntpdate定时任务,需要先注释掉或关闭,再启动,否则两者会有冲突。
ntp服务默认侦听的端口是udp的123端口

ntpdate ntp2.aliyun.com#<==先让本机的时间与互联网的时间保持同步
systemctl start ntpd
systemctl status ntpd
systemctl enable ntpd
systemctl list-unit-files|grep ntpd
netstat -tunlp|grep 123

6、查看本机和上层服务器的时间同步结果

[root@ntp-s ~]# ntpq -p #<==查看本机和上层服务器的时间同步结果( p == peer )
remote refid st t when poll reach delay offset jitter
==============================================================================
*time5.aliyun.co 10.137.38.86 2 u 37 64 77 55.129 22.421 6.118
120.25.115.19 10.137.38.86 2 u 39 64 77 22.665 20.670 6.239
120.25.115.20 10.137.38.86 2 u 37 64 77 22.948 14.326 5.291

7、NTP客户端
注意:客户机要等几分钟再和新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。

[root@linux-node2 ~]# date -s "20110101"#<==修改当前时间
Sat Jan 1 00:00:00 CST 2011
[root@linux-node2 ~]# date +%F
2011-01-01
[root@linux-node2 ~]# ntpdate 192.168.56.11#<==手动执行与NTP server进行时钟同步
11 Feb 21:46:14 ntpdate[19828]: step time server 192.168.56.11 offset 193009546.214391 sec
[root@linux-node2 ~]# date
Sat Feb 11 21:46:19 CST 2017

8、客户端添加到定时任务
[root@linux-node2 ~]# crontab -l
#time sync by ChenDianHu at 2016-06-28
*/5 * * * * /usr/sbin/ntpdate 192.168.56.11 >/dev/null 2>&1







