第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查看系统环境
[[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [[email protected] ~]# uname -r 2.6.32-573.el6.x86_64 [[email protected] ~]# /etc/init.d/iptables status iptables:未运行防火墙。 [[email protected] ~]# getenforce Disabled [[email protected] ~]# /etc/init.d/iptables status iptables:未运行防火墙。
1.3 安装NTP服务
CentOS6.7默认已安装ntp服务。如果没安装,就用yum安装即可。
[[email protected] ~]# rpm -qa ntp ntp-4.2.6p5-5.el6.centos.x86_64
1.4 配置NTP
NTP服务默认的配置文件为:/etc/ntp.conf
1.4.1操作前备份
[[email protected] ~]# cp /etc/ntp.conf{,.ori} [[email protected] ~]# 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配置完成如下
[[email protected] ~]# 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配置重要参数详解
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定时任务,需要先关闭;否则两者会有冲突。
[[email protected] ~]# /etc/init.d/ntpd start #启动服务 正在启动 ntpd: [确定] [[email protected] ~]# chkconfig ntpd on #加入开机自启动 [[email protected] ~]# chkconfig --list ntpd
1.5.1查看本机和上层服务器的时间同步结果
[[email protected] ~]# ntpq -p
1.6 客户端同步
客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。
[[email protected] ntpstats]# date -s"20160606" #修改当前时间 2016年 06月 06日 星期一 00:00:00 CST [[email protected] 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 [[email protected] ntpstats]# date 2016年 07月 28日 星期四 15:55:30 CST
1.7 定时任务(客户端)
[[email protected] ~]# crontab -l #time sync by ChenDianHu at 2016-06-28 */5 * * * * /usr/sbin/ntpdate 172.16.1.41>/dev/null 2>&1
1.8 命令说明
ntpdate ntp1.aliyun.com #向阿里云时间服务器,立即同步更新时间 ntpq -p peers #查看本机和上层服务器的时间同步结果
时间: 2024-08-01 22:44:05