linux NTP服务搭建

一. 配置时区

[root@client ~]# tzselect  #配置正确时区
[root@www ~]# vim /etc/sysconfig/clock

二. 搭建NTP服务

1. 配置主配置文件

[root@www ~]# vim /etc/ntp.conf

logfile /var/log/ntp.log
driftfile /var/lib/ntp/drift
interface listen 192.168.100.254
interface ignore wildcard
interface listen lo

# 1. 先处理权限方面的问题,包括放行上层服务器以及开放区网用户来源:
restrict default kod nomodify notrap nopeer noquery     <==拒绝 IPv4 的用户
restrict -6 default kod nomodify notrap nopeer noquery  <==拒绝 IPv6 的用户
restrict 220.130.158.71   <==放行 tock.stdtime.gov.tw 进入本 NTP 服务器
restrict 59.124.196.83    <==放行 tick.stdtime.gov.tw 进入本 NTP 服务器
restrict 59.124.196.84    <==放行 time.stdtime.gov.tw 进入本 NTP 服务器
restrict 127.0.0.1        <==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行区网来源

# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
server 220.130.158.71 prefer  <==以这部主机为最优先
server 59.124.196.83
server 59.124.196.84
server time.nuri.net
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server ntp.tuna.tsinghua.edu.cn

# 3.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
driftfile /var/lib/ntp/drift
keys      /etc/ntp/keys

2. 启动服务

# 1. 启动 NTP 
[root@www ~]# /etc/init.d/ntpd start
[root@www ~]# chkconfig --level 35 ntpd on

3. 观察NTP运行

[root@www ~]# tail -f logfile /var/log/ntp.log
[root@www ~]# ntpstat       #这个指令可以列出我们的 NTP 服务器有跟上层联机否。
[root@www ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*tock.stdtime.go 59.124.196.87    2 u   19  128  377   12.092   -0.953   0.942

+59-124-196-83.H 59.124.196.86    2 u    8  128  377   14.154    7.616   1.533

+59-124-196-84.H 59.124.196.86    2 u    2  128  377   14.524    4.354   1.079

这个 ntpq -p 可以列出目前我们的 NTP 与相关的上层 NTP 的状态,上头的几个字段的意义为:

  • remote:亦即是 NTP 主机的 IP 或主机名啰~注意最左边的符号
    • 如果有『 * 』代表目前正在作用当中的上层 NTP
    • 如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者。
  • refid:参考的上一层 NTP 主机的地址
  • st:就是 stratum 阶层啰!
  • when:几秒钟前曾经做过时间同步化更新的动作;
  • poll:下一次更新在几秒钟之后;
  • reach:已经向上层 NTP 服务器要求更新的次数
  • delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
  • offset:时间补偿的结果,单位与 10^(-3) 秒
  • jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

三. 客户端时间更新方式

#1. 自动同步时间的前提是找到NTP服务器,如下为NTP服务器
#中国国家授时中心:210.72.145.44   ----暂时无法使用
#NTP服务器(上海) :ntp.api.bz
#中国ntp服务器:cn.pool.ntp.org
#pool.ntp.org

#2. 时间同步工具
#rdate -s
#ntpdate -u(使用-u参数会返回误差,也可以使用-s)

#3. 自动同步时间举例:
[root@client ~]# tzselect  #配置正确时区
[root@client ~]# /usr/sbin/ntpdate -u cn.pool.ntp.org  #同步时间
[root@client ~]# hwclock -w   #将时间写入blos

#4. 将以上自动同步时间加入开机启动校验
vim /etc/rc.d/rc.local   
/usr/sbin/ntpdate -u cn.pool.ntp.org> /dev/null 2>&1; /sbin/hwclock -w

#5. 将以上自动同步时间写入定时任务执行
vim /etc/crontab  或者 crontab -e
00 09 * * * root /usr/sbin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w 

猜你喜欢

转载自www.cnblogs.com/wangzengyi/p/12520835.html