本地NTP服务器与客户端配置

1. NTP 简介

    NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

    在集群中,为了保证各节点之间的时间一致,我们通常需要配置本地的 NTP 服务器。

2. 环境准备

    

os hostname ip
centos6.7 localntpserver 172.16.1.100
centos6.7 ntpclient 172.16.1.101


3. ntpserver配置

    3.1   查看是否安装有ntpd

    

chkconfig --list


    3.2 安装ntpd

yum install ntpd ntpdate

    3.3 备份 ntpd 配置

mv /etc/ntp.conf /etc/ntp.conf.bak

    3.4 修改ntpd 配置 

    vi/etc/ntp.conf

    输入以下内容

#fast ntp server
server 202.108.6.95
server 202.112.29.82
server 110.75.190.198
server cn.ntp.org.cn

#store last time
driftfile /etc/ntp/drift

#allow upper modify localhost
restrict 0.0.0.0 nomodify notrap noquery

#allow any host
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap

#level number
fudge 127.127.1.1 stratum 2

#ntp log path
statsdir /var/log/ntp/

#ntp log file
logfile /var/log/ntp/ntp.log

    3.5 初始同步时间

ntpdate 202.108.6.95

    3.6 定时将日期写入硬件,确保重启时时间异常
    安装定时工具

yum install crontab

    

    添加定时规则:crontab -e

    在末尾追加以下内容

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * command to be executed

#ntp config
0 9 * * 1 hwclock -w


    3.7 启动 ntpserver

service ntpd start

    3.8 设置 ntpd 服务开机启动

chkconfig ntpd on

4. 配置 ntpd client

客户端配置除了 /etc/ntp.conf 配置文件内容不同外,其他配置都与ntpd server 相同,因此只做简单介绍,相应步骤参考 ntpd server。

    4.1   安装 ntpd 

yum install ntpd ntpdate

    4.2 修改ntpd 配置vi/etc/ntp.conf

    输入以下内容(localntpserver为ntp server 的hostname)

#fast ntp server
server 172.16.1.92 prefer

#store last time
driftfile /etc/ntp/drift

#allow upper modify localhost
restrict 0.0.0.0 nomodify notrap noquery

#allow any host
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap

#level number
fudge 127.127.1.1 stratum 2

#ntp log path
statsdir /var/log/ntp/

#ntp log file
logfile /var/log/ntp/ntp.log

includefile /etc/ntp/crypto/pw
 
keys /etc/ntp/keys




    4.3 初始同步时间

ntpdate localntpserver

    4.4 定时将日期写入硬件,确保重启时时间异常

   添加定时规则: crontab -e

    在末尾追加以下内容

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * command to be executed

#ntp config
0 9 * * 1 hwclock -w


    4.5 启动 ntpd

service ntpd start

    4.6 设置 ntpd 开机启动

chkconfig ntpd on

    4.7 查看ntpd 状态

ntpstat




至此,NTP server 与 client 配置完成





原文地址:https://www.cnblogs.com/moonciki/p/8145829.html