第1章 NTP时间服务器1.1 NTP简介NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机时间同步的一种协议。
NTP服务器利用NTP协议来提供时间同步服务。
1.2 环境准备1.2.1主机规划表主机名 服务器/客户 外网IP 内网IP ntp-s Server 10.0.0.41 172.16.1.41 ntp-c Client 10.0.0.31 172.16.1.31 1.2.2查看系统环境1
2
3
4
5
6
7
8
9
10
| [iyunv@ntp-s ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[iyunv@ntp-s ~]# uname -r
2.6.32-573.el6.x86_64
[iyunv@ntp-s ~]# /etc/init.d/iptables status
iptables:未运行防火墙。
[iyunv@ntp-s ~]# getenforce
Disabled
[iyunv@ntp-s ~]# /etc/init.d/iptables status
iptables:未运行防火墙。
|
1.3 安装NTP服务CentOS6.7默认已安装ntp服务。如果没安装,就用yum安装即可。
1
2
| [iyunv@ntp-s ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64
|
1.4 配置NTPNTP服务默认的配置文件为:/etc/ntp.conf
1.4.1操作前备份1
2
3
4
| [iyunv@ntp-s ~]# cp /etc/ntp.conf{,.ori}
[iyunv@ntp-s ~]# ll /etc/ntp.conf{,.ori}
-rw-r--r--. 1 root root 1778 2015-04-28 18:11/etc/ntp.conf
-rw-r--r-- 1 root root 1778 2016-07-28 15:28 /etc/ntp.conf.ori
|
1.4.2配置完成如下1
2
3
4
5
6
7
8
9
| [iyunv@ntp-s ~]# grep -vE "^$|#" /etc/ntp.conf #过滤空行和注释行
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
restrict default nomodify
server ntp1.aliyun.com
server time.nist.gov
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
|
1.4.3NTP配置重要参数详解1
2
3
4
| driftfile /var/lib/ntp/drift #记录上次的NTP server与上层NTP server联接所花费的时间
restrict default nomodify #允许所有主机进行时间同步
server ntp1.aliyun.com #同步时间服务器
server 210.72.145.44 prefer #prefer表示优先
|
1.5 启动NTP服务~注意:如果有ntpdate定时任务,需要先关闭;否则两者会有冲突。 1
2
3
4
| [iyunv@ntp-s ~]# /etc/init.d/ntpd start #启动服务
正在启动 ntpd: [确定]
[iyunv@ntp-s ~]# chkconfig ntpd on #加入开机自启动
[iyunv@ntp-s ~]# chkconfig --list ntpd
|
1.5.1查看本机和上层服务器的时间同步结果1
| [iyunv@ntp-s ~]# ntpq -p
|
1.6 客户端同步客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。
1
2
3
4
5
6
| [iyunv@ntp-c ntpstats]# date -s"20160606" #修改当前时间
2016年 06月 06日 星期一 00:00:00 CST
[iyunv@ntp-c ntpstats]# ntpdate 172.16.1.41 #与NTP server进行时钟同步
28 Jul 15:55:27 ntpdate[8510]: step time server172.16.1.41 offset 4550114.397444 sec
[iyunv@ntp-c ntpstats]# date
2016年 07月 28日 星期四 15:55:30 CST
|
1.7 定时任务(客户端)1
2
3
| [iyunv@ntp-c ~]# crontab -l
#time sync by ChenDianHu at 2016-06-28
*/5 * * * * /usr/sbin/ntpdate 172.16.1.41>/dev/null 2>&1
|
1.8 命令说明1
2
| ntpdate ntp1.aliyun.com #向阿里云时间服务器,立即同步更新时间
ntpq -p peers #查看本机和上层服务器的时间同步结果
|
|