如来神掌第一式第二十二招----NTP 详解

###############################################################################
# Name : Mahavairocana                                                                                                                                           
# Author : Mahavairocana                                                                                                                                         
# QQ : 10353512                                                                                                                                                    
# WeChat : shenlan-qianlan                                                                                                                                      
# Blog : http://www.cnblogs.com/Mahavairocana/                                                                                                       
# Description : You are welcome to reprint, or hyperlinks to indicate the                                                                        
#                    source of the article, as well as author information.                                                                                ###############################################################################

第一步 安装

[root@s ~]# yum install ntp

第二步 配置

[root@s ~]# mv /etc/ntp.conf /etc/ntp.conf.bak

[root@s ~]# vi /etc/ntp.conf

restrict default ignore   #关闭所有的 NTP 要求封包
restrict 127.0.0.1        #开启本机网络接口
restrict 192.168.100.254  #开启本机IP
#开启另一台服务器
restrict 192.168.100.240 mask 255.255.255.255 nomodify
#开启本局域网
restrict 192.168.100.0 mask 255.255.255.0 nomodify

#上层主机, prefer 为主要更新服务器
#我们主要以 time.nist.gov 为主
server 64.90.182.55 prefer
server 210.72.145.44 #国家授时中心

#允许上层主机访问
restrict 64.90.182.55
restrict 210.72.145.44

#当不能连接上层服务器时,使用本机的
server 127.0.0.1
fudge 127.0.0.1 stratum 8

 

第三步 启动

[root@s ~]# service ntpd start

[root@s ~]# netstat -nlp | grep 123

udp        0      0 192.168.100.254:123         0.0.0.0:*                               19834/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               19834/ntpd          
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               19834/ntpd          
udp        0      0 ::1:123                     :::*                                    19834/ntpd          
udp        0      0 fe80::21f:d0ff:fe60:2665:123 :::*                                    19834/ntpd          
udp        0      0 :::123                      :::*                                    19834/ntpd 

#查看与上层服务器的状态

[root@s ntp]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*nist1-ny.ustimi .ACTS.           1 u   63   64  137  378.564  -101.08  27.667
 210.72.145.44   .ACTS.           1 u  316    8   20   96.528  -67.287   0.001
 localhost       .INIT.          16 l    -   64    0    0.000    0.000   0.000

remote - 本机和上层ntp的ip或主机名,"+"表示优先,"*"表示次优先
refid -  参考上一层ntp主机地址
st -     stratum阶层
when -   多少秒前曾经同步过时间
poll -   下次更新在多少秒后
reach -  已经向上层ntp服务器要求更新的次数
delay -  网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

第四步 客户端连接

[root@c2 html]# ntpdate 192.168.100.254
16 Feb 15:23:47 ntpdate[23757]: no server suitable for synchronization found

后来一查(排除防火墙阻止123端口),原来要服务器的 reach 大于 17 才可以。

如果要打开防火墙,就要开放 udp 123 端口

[root@c2 html]# iptables -A INPUT -p udp --dport 123 -j ACCEPT

[root@c2 html]# ntpdate 192.168.100.254
16 Feb 15:32:37 ntpdate[23914]: step time server 192.168.100.254 offset 416.114308 sec

然后加入 crontab 定时

00 23 * * * root /usr/sbin/ntpdate 192.168.100.254; /sbin/hwclock -w

 

附: 修改系统时间

[root@s ~]# date 051916302012            #月日时分年
2012年 05月 19日 星期六 16:30:00 CST

[root@s ~]# clock -w

把系统时间调整为北京时间:

[root@s ~]# rm -f /etc/localtime

[root@s ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 
原文地址:https://www.cnblogs.com/Mahavairocana/p/8290144.html