Centos 5.6下面架设NTP服务器

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://navyaijm.blog.51cto.com/4647068/809455

最近公司在搞运维自动化这一块,要实现批量更新服务器的配置文件等等操作,需要用到NTP服务器,下面把搭建的过程分享一下:
1.安装ntp服务包
 

   # yum install ntp -y
2. 让本服务器时间与 3.cn.pool.ntp.org 时间同步;
   [root@localhost ~]# ntpdate
  3.cn.pool.ntp.org
3. 自动同步服务器时钟
   [root@localhost ~]# crontab -e
   * */4 * * * /usr/sbin/ntpdate 3.cn.pool.ntp.org
   [root@localhost ~]# service crond restart
4.配置
ntp.conf
   [root@localhost ~]# vi /etc/ntp.conf
restrict default nomodify notrap noquery
restrict 10.20.220.0 mask 255.255.254.0 nomodify notrap
restrict 10.20.216.0 mask 255.255.254.0 nomodify notrap
restrict 10.20.221.0 mask 255.255.254.0 nomodify notrap
restrict 127.0.0.1
restrict -6 ::1
server 3.cn.pool.ntp.org
server 10.20.220.251
server time.lib.tsinghua.edu.cn
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 8
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
logfile /var/log/ntp.log
broadcastdelay 0.008


5.
启动或停止时间服务
     # service ntpd start
     # service ntpd stop
     # service ntpd restart
6.验证ntp服务已经运行
     # pgrep ntpd
     #123端口是否开启
7.初始同步
     # ntpdate -u 3.cn.pool.ntp.org
8.确认同步成功
[root@localhost ~]# ntpq -p
         remote                     refid           st t when poll reach     delay     offset   jitter
==============================================================================
*61.153.197.226   66.220.9.122         2 u   188   256   377     25.124     12.510     2.999
  10.20.220.251     .INIT.                   16 u       - 1024       0       0.000       0.000     0.000
  web.lib.tsinghu .INIT.                   16 u       - 1024       0       0.000       0.000     0.000
+114.80.81.1         209.81.9.7             2 u   168   256   377     27.492   -31.989     1.283
  LOCAL(0)               .LOCL.                     8 l     23     64   377       0.000       0.000     0.001

9.linux客户端设置NTP

目前 Linux 系统上面有两个时间,一个是 Linux 系统,另一个则是 BIOS 时间(真正的硬件记录的时间)!

我们可以使用 date 这个指令来手动修正目前主机的时间,date 这个指令仅修正 Linux 时间而已,所以需要以 hwclock 这个指令来将 BIOS 时间也更新才行

[root@test root]# date MMDDhhmmYYYY
MM:月份
DD:日期
hh:小时
mm:分钟
YYYY:公元年

 
[root@test root]# hwclock [-rw]

-r:检视目前的 BIOS 时间
-w:将目前 Linux 的时间写入 BIOS 当中!

[root@localhost ~]# date ; hwclock -r
2011年 03月 23日 星期三 01:03:59 CST
2011年03月23日 星期三 03时05分00秒  -0.914570 seconds

 date 与 hwclock -r 所显示的时间是『不一致的』!
# 因为 Linux 时间与 BIOS 时间不一致所导致的一个问题!
# 需要以 hwclock -w 来将 Linux 时间写入 BIOS

[root@localhost ~]# hwclock -w
[root@localhost ~]# date ; hwclock -r
2011年 03月 23日 星期三 01:03:59 CST
2011年03月23日 星期三 01时04分00秒  -0.814770 seconds


#自动同步服务器时钟
[root@localhost ~]# crontab -e
# 加入这一行:
10 5 * * * root /usr/bin/ntpdate 10.20.220.251 ; /sbin/hwclock -w

[root@localhost ~]# service crond restart

10.windows客户端设置NTP

设置 Windows Time 服务:在“控制面板”中打开“管理工具”,再打开“服务”;在服务中找到“Windows Time”,双击服务名称,在打开的“Windows Time的属性”中设置“启动类型”为“自动”,再按下启动按钮。

在“运行(R)”中用“cmd”命令进入在DOS命令行窗体;执行以下命令(注意空格):

net time /setsntp:192.168.1.232

net stop w32time

net start w32time

w32tm –s

(设置时间服务器地址)

(停止时间服务)

(启动时间服务)

(立即同步时间)

 

11.客户端常见错误:

错误1.Server dropped: Strata too high

ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。

ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。

这是因为NTP server还没有和其自身或者它的server同步上。

以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

server  127.127. 1.0
fudge  127.127. 1.0  stratum  8 

 

ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

ntp server上使用命令:

#  watch ntpq -p


出现画面:

Every 2.0s: ntpq -p                                                                                                             Thu Jul 10 02:28:32 2008

     remote           refid      st t when poll reach   delay   offset jitter

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

 192.168.30.22   LOCAL(0)         8 u   22   64    1    2.113 179133.   0.001

 LOCAL(0)        LOCAL(0)        10 l   21   64    1    0.000   0.000  0.001

注意LOCAL的这个就是与自身同步的ntp server

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0175次的变更,每一次是poll的值的秒数,是64*5=320秒的时间。

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。

错误2.Server dropped: no data

从客户端执行
netdate –d时有错误信息如下:

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

192.168.30.22: Server dropped: no data

server 192.168.30.22, port 123

.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found

出现这个问题的原因可能有2

1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:

#  ntpq -c version


下面是来自ntp官方网站的说明:
The behavior of 
notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client)ntpd

解决:

notrust去掉。


2
。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。

可以用命令

# service iptables stop


来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

 

本文出自 “屌丝运维男” 博客,请务必保留此出处http://navyaijm.blog.51cto.com/4647068/809455

目录
相关文章
|
4月前
|
安全 Linux Shell
使用SCP命令在CentOS 7上向目标服务器传输文件
以上步骤是在CentOS 7系统上使用SCP命令进行文件传输的基础,操作简洁,易于理解。务必在执行命令前确认好各项参数,尤其是目录路径和文件名,以避免不必要的传输错误。
507 17
|
3月前
|
Ubuntu 安全 小程序
服务器版本的CentOS和Ubuntu哪个更适合你?
但是以上的比较并不说明Ubuntu是不稳定的或者是不安全的,只是以上比较过程中,在稳定性方面Ubuntu稍微逊色了一点。由于Ubuntu在个人桌面电脑的使用率远远高于CentOS,用Ubuntu搭建服务器,如果遇到什么问题,寻找解决方案相对比较容易,这让Ubuntu在选择方面更优于CentOS。如果你是一个初学者,那么毫无疑问Ubuntu是更适合的选择。如果你正在经营自己的公司,在这两者之间,CentOS会更好一些。
|
4月前
|
安全 关系型数据库 网络安全
安全加固:启动PostgreSQL 14服务器SSL加密的方法指南在CentOS 7环境中
通过上述步骤,你可以为PostgreSQL数据库服务器设置SSL加密,从而增加数据在传输中的安全性。确保维持证书的有效性,并且定期更新和管理密钥,以防止未授权访问。
232 0
|
Linux
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
1297 1
|
XML 应用服务中间件 Linux
在一个centos服务器上同时部署tomcat,django,flask
在一个centos服务器上同时部署tomcat,django,flask
254 0
|
Java 应用服务中间件 Linux
Centos7云服务器部署Tomcat
Centos7云服务器部署Tomcat
Centos7云服务器部署Tomcat
|
数据采集 弹性计算 网络协议
[CentOS Python系列] 一.阿里云服务器安装部署及第一个Python爬虫代码实现
本文将详细讲解阿里云服务器如何安装及部署本地Python环境,并搭建代码。
1857 1
[CentOS Python系列] 一.阿里云服务器安装部署及第一个Python爬虫代码实现
|
弹性计算 Java Linux
CentOS7系统云服务器Java Web环境镜像部署操作演示
本篇文档介绍如何在ECS实例上使用镜像部署Java Web环境。
2869 0
|
弹性计算 JavaScript 开发工具
CentOS7.2系统的云服务器ECS如何部署部署Node.js项目
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效,非常适合运行在分布式设备的数据密集型的实时应用。
2353 0
下一篇
oss云网关配置