[收藏学习]关于Linux服务器时间同步nptdate

centos下的ntp的安装和配置

2009-07-13 10:17

安装
yum install ntp

配置文件 /etc/ntp.conf
restrict default kod nomodify
notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
# 用restrict控管权限
# nomodify - 用户端不能更改ntp服务器的时间参数
# noquery - 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
# notrap - 不提供trap远端登陆
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
# 以下是两个ntp服务器
restrict cn.pool.ntp.org
restrict 164.67.62.194

# 用server设定上层ntp服务器,“prefer”表示优先
server cn.pool.ntp.org prefer
server 164.67.62.194

server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# 系统时间与bios事件的偏差记录
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
# 日志
logfile /var/log/ntp.log
broadcastdelay 0.008

一个说明:
我在vmware下搭建的ntp服务器,别人可以同步它,它自己的时间却不对。照理是它同步上层,下层同步它呀。后来找了蛮久的资料,发现这个文件:/etc/ntp/step-tickers,说是ntp服务器无法主动同步上层服务器,所以将地址写入该文件中即可。

我在物理机里暂时没发现这个问题,也许是物理机本来自己的时间就正确,就难以发现它有没有主动同步上层服务器。

运行ntp
service ntpd start/stop/restart
chkconfig ntpd on

查看ntp的端口,应该看到123端口
netstat -unlnp

查看ntp服务器有无和上层连通
[root@S5 ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
注意:此命令出现上述synchronised结果比较慢,我的用了大概5分钟。

查看ntp服务器与上层间的联系
[root@S5 ~]# ntptrace -n 127.0.0.1
127.0.0.1: stratum 2, offset 0.064524, synch distance 0.121167
210.72.145.44: timed out, nothing received***Request timed out

查看ntp服务器与上层ntp服务器的状态
[root@S5 ~]# ntpq -p
其中,
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

客户端同步时间的方法
方法一:使用 ntpdate
ntpdate [IP_Address]
看下面的例子,其中192.168.1.5是我在内网搭建的ntp服务器
[root@zy180 ~]# date
Thu Sep 25 13:04:17 CST 2008
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 14:33:13 ntpdate[10302]: step time server 192.168.1.5 offset 5327.700798 sec
[root@zy180 ~]# date
Thu Sep 25 14:33:18 CST 2008

ntpdate 的特点:
1,ntpdate只运行一次就结束,即只同步一次。所以要使用ntpdate同步时间的话要将该命令写入cron中。
vi /etc/crontab
* * * * * root ntpdate 192.168.1.5 && hwclock -w
之所以使用hwclock -w命令稍后再说。
2,ntpdate同步时间是跳跃的,如上面的显示,时间立即就同步了,这将可能导致文件时间标记、监控数据的紊乱。

方法二:使用ntpd服务
step1:/etc/ntpd.conf 写入 server [IP_Address]
step2:chkconfig ntpd on
step3:service ntpd start

大概过一刻钟开始起作用。

FAQ:系统报错 - Sep 25 11:59:39 localhost kernel: set_rtc_mmss: can't update from 7 to 59
==================================网上查得源码==========================================
if (abs(real_minutes - cmos_minutes) < 30) { update_cmos() } else { printk("set_rtc_mmss: can't update from %d to %d\n", cmos_minutes, real_minutes); } ========================================================================================
bios时间与系统时间差异超过30分钟,就会报上述错误,只要修改bios时间或系统时间即可。

虚拟机下个人体验:
我的vm客户端用ntpd同步服务器后,慢慢变成标准时间,可接着又比标准时间越来越慢,然后出现上述报错。所以我觉得ntpd的同步频率比我的vm时钟频率跑得慢。

ntpd 的特点:
1,ntpd服务始终运行,定期同步时间。
2,ntpd服务是慢慢改变时间直至标准时间。
3,如果系统时间与bios时间差异超过30分钟,ntpd就会报错。

总结:
1,推荐使用 ntpd 服务。
2,ntpd开启后,无法使用ntpdate,看如下例子:
[root@zy180 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 15:25:22 ntpdate[12334]: the NTP socket is in use, exiting

附:linux的时间、时区
1,时间
系统时间:date 命令
bios时间:hwclock命令 (-r 读取bios时间,-w 写入bios时间)
每次开机时系统重新读bios时间。
所以前面ntpdate命令时的cron脚本需要用到hwclock,以避免机器重启后时间混乱。

2,时区
linux的所有时区文件都放在 /usr/share/zoneinfo 下。
linux系统的时区文件是 /etc/localtime,这是个时间格式的档案,不是ACSII码。它与/usr/share/zoneinfo/下的自己的时区文件是一样的。
我的时区是Asia-Shanghai,用diff比较两个文件,如下:
[root@zy180 ~]# diff /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@zy180 ~]#
在 /etc/sysconfig/clock 文件中也写有本时区的信息:
[root@zy180 ~]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false

NTP时间服务器:

使用环境:当一个计算机群体需要需要进行时间的严格同步,即可使用NTP服务器。

NTP通信协议原理:

1.首先主机启动NTP。

2.客户端会向NTP服务器发送调整时间的message。

3.然后NTP server会送出当前的标准时间给client

4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。

NTP这个deamon采用了123端口。(UDP)

“当我们要利用Tim server来进行实践的同步更新时,就需要使用NTP软件提供的ntpdate来连接端口123”

安装:

首先检查是否安装了组建:

[root@testmechine ~]# rpm -qa ntp 
ntp-4.2.2p1-9.el5.centos.1

假如没有安装则yum install ntp来进行安装。

所需要的配置文件:

1./etc/ntp.conf

linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。

2./usr/share/zoneinfo/

这个里面规定了这个主要时区的时间设置文件。

3./etc/sysconfig/clock

这个文件是linux的主要时区设置文件,每次开机后linux会自动读取这个文件来设置系统所默认的显示时间,可以看看它里面到底设置了什么:

[root@testmechine sysconfig]# cat /etc/sysconfig/clock 
# The ZONE parameter is only evaluated by system-config-date. 
# The timezone of the system is defined by the contents of /etc/localtime. 
ZONE="Asia/Shanghai" 
UTC=true 
ARC=false

4./etc/localtime

本地端时间配置文件。

5./bin/date

这个是时间的修改命令,除了输出时间,还可以修改时间。

6./sbin/hwclock

因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。

7./usr/sbin/ntpd

这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。

8./usr/sbin/ntpdate

这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。

9,/usr/sbin/ntptrace

可以用来追踪某台时间服务器的时间对应关系。

NTP时间服务器采用类似于分层的架构:

关于ntp.conf的设置:

[root@testmechine ~]# cat /etc/ntp.conf 
# Permit time synchronization with our time source, but do not 
# permit the source to query or modify the service on this system. 
#注销原来的命令

restrict default kod nomodify notrap nopeer noquery 
restrict -6 default kod nomodify notrap nopeer noquery

#添加如下命令:

restrict default nomodify noquery notrap 
restrict 202.112.10.60 
restrict 202.106.199.36 
restrict 202.112.7.150 
restrict 127.0.0.1 mask 255.0.0.0 
restrict 192.168.0.0 mask 255.255.255.0 nomodify

# 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 
restrict -6 ::1

# 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.centos.pool.ntp.org 
server 1.centos.pool.ntp.org 
server 2.centos.pool.ntp.org

#进行如下替换

server time.buptnet.edu.cn 
server s1b.time.edu.cn 
server s1c.time.edu.cn 
server s1d.time.edu.cn

#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 202.112.10.60 prefer #以这台主机最优先 
server 202.106.199.36 
server 202.112.7.150

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

# 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 
[root@testmechine ~]#

以上是ntp.conf的最终设置。

然后我们修改/etc/sysconfig/ntpd:

# Drop root to id 'ntp:ntp' by default. 
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"

# Set to 'yes' to sync hw clock after successful ntpdate 
SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改

# Additional options for ntpdate 
NTPDATE_OPTIONS=""

启动NTP:

[root@testmechine ~]# /etc/init.d/ntpd start 
Starting ntpd: [ OK ]、

看看端口启动情况:

[root@testmechine ~]# netstat -nltpu|grep ntpd 
udp        0      0 192.168.0.20:123            0.0.0.0:*                               7237/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               7237/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               7237/ntpd           
udp        0      0 ::1:123                     :::*                                    7237/ntpd           
udp        0      0 fe80::20a:ebff:fe93:123     :::*                                    7237/ntpd           
udp        0      0 :::123                      :::*                                    7237/ntpd           
[root@testmechine ~]#

但是要15分钟才会和上层的NTP服务器顺利连接上,耐心等待。

服务器算大功告成了,现在我们就看客户端怎么设置了。在另外一台机器上进行:

#vi /etc/crontab

写入:

10 5 * * * root /usr/sbin/ntpdate 192.168.0.20 &&/sbin/hwclock -w

这样每天的5点10分,linux系统都会自动向我们的新建NTP时间服务器192.168.0.20 进行时间的同步操作

本文参考:http://hi.baidu.com/chenshake/blog/item/908714238a4a09539358076b.html

转载请注明出处:http://www.cnblogs.com/haochuang/ 8年IT工作经验,5年测试技术与管理,2年产品与项目管理,曾参与过云计算\云存储\车联网产品研发工作; 业余自媒体人,有技术类垂直微信公众号;如有招聘或求职方面需求,请Mail to uetest@qq.com ;或通过 QQ:363573922 微博:@念槐聚 联系;

时间: 2024-09-19 09:30:58

[收藏学习]关于Linux服务器时间同步nptdate的相关文章

ios4年 想学习java linux服务器开发 请教一下学习步骤

问题描述 我因为主体是做ios开发近期想学学javalinux服务器开发不想学什么html之类的我就是想学习服务器什么postget方法以及调用数据库之类的请问我应该从哪里学起应该看些什么东西?是不是得租一个linxu服务器阿里云的按流量收费的服务器我来学习可以吗?请大家推荐个教程吧 解决方案 解决方案二:建议还是自己买个小本装linux吧,阿里云那个只是用来一个人学习的话,不划算解决方案三:虚拟机装个linux不就行了,红帽和乌班图都不错啊,而且都有图形化界面.再说,java和操作系统有什么关

Linux服务器时间同步设置方法

下面我来介绍相关教程 以下命令都需要用root权限执行,否则会提示权限不足 首先确定服务器有没有ntpdate这个软件: which ntpdate如果没有找到,就需要安装一个ntpdate. 在Redhat.CentOS下使用这个命令:  代码如下 复制代码 yum install -y ntp 在Debian.Ubuntu下使用这个命令:  代码如下 复制代码 apt-get install -y ntpdate 安装好以后执行以下命令,通过授时服务器获得准确的时间 ntpdate -u p

值得收藏的20个Linux服务器性能优化技巧_Linux

    Linux是一种开源操作系统,它支持各种硬件平台,Linux服务器全球知名,它和Windows之间最主要的差异在于,Linux服务器默认情况下一般不提供GUI(图形用户界面),而是命令行界面,它的主要目的是高效处理非交互式进程,响应时间并不是那么重要,相反,能够长时间处理高负载才是最关键的.Linux高可用服务器集群解决方案让IT系统管理员可以从容应对许多常见的硬件和软件故障,允许多台计算机一起工作,为关键服务正常运行提供保障,系统管理员可以不中断服务执行维护和升级.     Linux

[收藏学习]Linux的实时监测命令

十八.  和系统运行状况相关的Shell命令:     1. Linux的实时监测命令(watch):     watch 是一个非常实用的命令,可以帮你实时监测一个命令的运行结果,省得一遍又一遍的手动运行.该命令最为常用的两个选项是-d和-n,其中-n表示间隔多少秒 执行一次"command",-d表示高亮发生变化的位置.下面列举几个在watch中常用的实时监视命令:     /> watch -d -n 1 'who'   #每隔一秒执行一次who命令,以监视服务器当前用户登

Linux服务器反向代理软件varnish

 Varnish作为反向代理软件,应用于Web 前端,其良好的Cache性能使得网站承载能力有很大提升,下面记录下最近使用在Varnish学习与应用中的实践体会,本篇主要内容:安装 配置 监控. 一.varnish安装 通常有两种方法: 1.下载安装文件,本地编译安装 2.连到官网,自动安装(比较简单) https://www.varnish-cache.org/installation/redhat 二.配置文件 1.配置Varnish 启动运行参数,如指定对应执行脚本,即vcl文件Varni

企业级Linux服务器安全防护要点

  随着开源系统Linux的盛行,其在大中型企业的应用也在逐渐普及,很多企业的应用服务都是构筑在其之上,例如Web服务.数据库服务.集群服务等等.因此,Linux的安全性就成为了企业构筑安全应用的一个基础,是重中之重,如何对其进行安全防护是企业需要解决的一个基础性问题,基于此,本文将给出十大企业级Linux服务器安全防护的要点. 1.强化:密码管理 设定登录密码是一项非常重要的安全措施,如果用户的密码设定不合适,就很容易被破译,尤其是拥有超级用户使用权限的用户,如果没有良好的密码,将给系统造成很

十大最流行的Linux服务器

  随着Linux不断发展,Linux所支持的文件系统类型也在迅速扩充.很多的数据中心服务器上都运行着Linux,可以节省大量的许可证费用及维护费用.但伴随着Linux新版本的发行,其中每一个不同版本的Linux所支持的文件系统类型都有所不同. 那么,对于这种情况来说,Linux系统家族中也划分着针对不同的用户群,比如Ubuntu被认为Linux新用户最容易操作的平台,而Slackware Linux是需要经过有着一定应用基础的用户,CentOS是一个企业级的发行版,特别适合对稳定性,可靠性和功

0基础学习搭建发布服务器

问题描述 0基础学习搭建发布服务器 我现在正在从事iphone和android弱联网休闲游戏开发,客户端和服务器通信协议是http,现在我想自己搭建对应的游戏服务器,服务器很简单的,只记录玩家的一些基本信息,并做一些基本的逻辑判断. c++, c#, java我都会,不过服务器是0基础 我想以最快的速度先搭建并发布一个最简单的服务器(windows server还是linux无所谓,哪个简单先来哪个)开始,比如客户端发送过来一个简单的"1+1"消息,服务器返回"2"

RedHat Linux服务器安全配置细节

1.概述 Linux服务器版本:RedHat Linux AS 2.1 对于开放式的操作系统---Linux,系统的安全设定包括系统服务最小化.限制远程存取.隐藏重要资料.修补安全漏洞.采用安全工具以及经常性的安全检查等. 本文主要从用户设置.如何开放服务.系统优化等方面进行系统的安全配置,以到达使Linux服务器更安全.稳定. 2.用户管理 在Linux系统中,用户帐号是用户的身份标志,它由用户名和用户口令组成. 系统将输入的用户名存放在/etc/passwd文件中,而将输入的口令以加密的形式