linux下时间同步

简介:

方法1:


与一个已知的时间服务器同步

复制代码代码如下:

1
ntpdate  time .nist.gov

其中 time.nist.gov 是一个时间服务器.


删除本地时间并设置时区为上海

复制代码代码如下:

1
2
rm  -rf  /etc/localtime
ln  -s  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime


方法2:

linux自动同步时间

vi /etc/crontab

加上一句:

复制代码代码如下:

1
00 0 1 * * root rdate -s  time .nist.gov

其中rdate本身是用来获取远程时间服务器上时间用的,带上 -s 参数,就可以将获取到的时间应用到本地系统。


配置时间服务器配置(18.40.33.30)

复制代码代码如下:

1)、# rpm -ivh ntp-4.1.2-4.EL3.1.i386.rpm

2)、# vi /etc/ntp.conf

注释一行:

restrict default ignore

加入一行:

复制代码代码如下:只允许18.40.33.***网段的客户机进行时间同步

restrict default kod nomodify notrap nopeer noquery 《 ===== 拒绝所有 IPv4 的用户

restrict -6 default kod nomodify notrap nopeer noquery  《 ===== 拒绝 I 所有 Pv6 的用户

之后增加一行:

restrict 18.40.33.0 mask 255.255.255.0 notrust nomodify notrap


参数介绍:

ignore: 拒绝所有类型的 NTP 连接

Nomodify: 客户端不能使用 ntpc 与 ntpq 这两个程序来修改服务器的时间参数,但客户端仍可通过这部主机来进行网络校时

Notrap :不提供 trap 这个远程事件登陆( remote event logging) 的功能

Noquery: 客户端不能够使用 ntpc 与 ntpq 等命令来查询时间服务器,等于不提供NTP 的网络校时

Notrust :拒绝没有认证的客户端

如果使用在没有参数的地方添加,表示该 IP 或者网段不受任何限制

如:

172.25.25.25 表示该 IP 不受任何限制

一般可以先关闭 NTP 的同步权限,再一个一个的添加可以同步的客户端 IP 或网段。

----------------------------------------------------------------------------------------

# Permit all access over the loopback interface.  This could

允许所有本地回环接口。

# be tightened as well, but to do so would effect some of

# the administrative functions.

restrict 127.0.0.1  《=== 放行本机回环 IPv4 形式

restrict -6 ::1 《 ==== 放行本机回环 IPv6 形式

# Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 《 ==== 放行局域网来源

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 0.rhel.pool.ntp.org ====== 上层同步的服务器

server 1.rhel.pool.ntp.org ====== 上层同步的服务器

server 2.rhel.pool.ntp.org ====== 上层同步的服务器

#broadcast 192.168.1.255 key 42 # broadcast server

#broadcastclient # broadcast client

#broadcast 224.0.1.1 key 42 # multicast server

#multicastclient 224.0.1.1 # multicast client

#manycastserver 239.255.254.254 # manycast server

#manycastclient 239.255.254.254 key 42 # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup

# and when no outside source of synchronized time is available. 

如果在上层时间服务器不能提供服务的时候,则使用这里进行本机的同步

server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

# Drift file.  Put this in a directory which the daemon can write to.

# No symbolic links allowed, either, since the daemon updates the file

# by creating a temporary in the same directory and then rename()'ing

# it to the file.

driftfile /var/lib/ntp/drift

TIP :

因为 NTP 默认本身的时间计算式依据 BIOS 的芯片振荡周期频率来计算的,但是这个数值与上层 time server 不见得一致,所以 NTP 这个 daenon ( ntpd )会自动去计算我们自己主机的频率与上层 time server 的频率,并且将两个频率的误差记录下来,记录下来的文件就是在 driftfiile 后面的完整文件名指定的文件。

该文件必须指定完整的路径

该文件不能是链接文件

该文件需要设置成 ntpd 这个 daemon 可以写入的权限

改文件所记录的数值单位是百万分之一秒( ppm )

# Key file containing the keys and key identifiers used when operating

# with symmetric key cryptography. 

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.

#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.

#requestkey 8

# Specify the key identifier to use with the ntpq utility.

#controlkey 8


3)、# vi /etc/ntp/step-tickers

加入一行:

pool.ntp.org

这样每次ntpd启动时,会自动连接该国际标准时间服务器;

4)、# service ntpd start

5)、# netstat -an |grep 123

确保该端口以udp方式开放


时间客户端配置(18.40.33.36)

1)、# ntpdate 18.40.33.30

25 Apr 13:31:58 ntpdate[31153]: step time server 10.40.34.208 offset 31.053643 sec

应该显示同步成功

注:时间服务器端重启ntpd服务后,在客户端同步需要等5分钟后在同步,不然会报no server suitable for synchronization found


2)、# crond -e

加入

复制代码代码如下:

0-59/10 * * * * /usr/sbin/ntpdate 18.40.33.30


每隔10分钟同步一次时间


0 21 * * * ntpdate 18.40.33.30 >> /root/ntpdate.log 2>&1
每天晚上9点进行同步


NTP 服务报错解析:


当用ntpdate -d 来查询时会发现导致  no server suitable for synchronization found  的错误的信息有以下 2 个:  
错误 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                                                    Tue Dec  7 05:52:55 2010

     remote           refid      st t when poll reach   delay   offset  jitter

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

*LOCAL(0)        .LOCL.           8 l   22   64  377    0.000    0.000   0.001

注意 LOCAL 的这个就是与自身同步的 ntp server 。 
注意 reach 这个值,在启动 ntp server 服务后,这个值就从 0 开始不断增加,当增加到17 的时候,从 0 到 17 是 5 次的变更,每一次是 poll 的值的秒数,是 64 秒 *5=320 秒的时间。 
如果之后从 ntp 客户端同步 ntp server 还失败的话,用 ntpdate  – d 来查询详细错误信息,再做判断。 
错误 2.Server dropped: no data 
从客户端执行 netdate  – d 时有错误信息如下: 
transmit(18.40.33.30)  
transmit(18.40.33.30) 
transmit(18.40.33.30) 
transmit(18.40.33.30) 
transmit(18.40.33.30) 
18.40.33.30: Server dropped: no data 
server 18.40.33.30, 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 的设置。



      本文转自ling118 51CTO博客,原文链接:http://blog.51cto.com/meiling/1652675,如需转载请自行联系原作者




相关文章
|
8月前
|
监控 网络协议 Linux
Linux日期和时间管理指南:日期、时间、时区、定时任务和时间同步
Linux日期和时间管理指南:日期、时间、时区、定时任务和时间同步
117 0
linux时间同步
linux时间同步实战操作
|
1月前
|
Linux 网络安全 开发工具
linux 时间同步 chrony
linux 时间同步 chrony
36 1
|
6月前
|
Linux
linux两台服务器时间同步
linux两台服务器时间同步
206 0
|
6月前
|
运维 Linux
linux时间同步以及各种时间格式显示
linux时间同步以及各种时间格式显示
59 0
|
11月前
|
Linux 网络安全
配置Linux服务器时间同步
配置Linux服务器时间同步
配置Linux服务器时间同步
|
12月前
|
安全 Linux 网络安全
Linux 时间同步 -NTP 服务器
本文主要介绍如何配置 Linux 服务器集群的时间同步,技术实现上使用 NTP 服务器和客户端同步时间。
448 0
|
Unix Linux
Linux 使用 chrony 进行 NTP 时间同步及自建方法
chrony是网络时间协议的实现。它可以替代ntpd,后者是NTP的参考实现。它在类Unix操作系统上运行,并在GNU GPL v2下发布。
350 0
|
网络协议 Linux
工作总结之服务器时间不同步导致平台验证失败及Linux系统时间同步方法
在Windwos中,系统时间的设置很简单,界面操作,通俗易懂,而且设置后,重启,关机都没关系。系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不间断。
246 0
工作总结之服务器时间不同步导致平台验证失败及Linux系统时间同步方法
|
Linux Windows
【看表情包学Linux】Redirect 重定向 | 时间相关指令 | 文件查找 | 打包与压缩(二)
本章仍然是继续对Linux 常用指令进行介绍,将讲解重定向、时间相关的指令、文件查找和打包压缩等指令。我们将初次理解 "Linux下一切皆文件"这一概念,我将通过一个有趣的故事去讲解它。
147 0
【看表情包学Linux】Redirect 重定向 | 时间相关指令 | 文件查找 | 打包与压缩(二)