首页 技术 正文
技术 2022年11月15日
0 收藏 381 点赞 4,106 浏览 2333 个字

linux 时间同步的2种方法

张映 发表于 2012-10-23

分类目录: 服务器相关

标签:linuxntp同步时间服务器

由于硬件的原因,机器或多或少的根标准时间对不上,一个月的误差几秒到几分钟不等。对于服务器来说时间不准,会有很多麻烦。例如,支付的时候,无法下单,游戏无法登录等。自己用的PC就无所谓了,手动改一下就行了。

一,用ntpdate从时间服务器更新时间

如果你的linux系统根本没有ntpdate这个命令

  1. yum install ntp

安装完了之后,你不要做什么配置,也不需要,直接测试一下

  1. [root@localhost ~]# ntpdate time.nist.gov
  2. 22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec

如果出去上面的内容说明,同步成功了。然后在crontab(etc/crontab)里面加上以下内容。

  1. */10 * * * * ntpdate time.nist.gov   #域名或IP

每隔十分钟同步一次。推荐几个时间服务器。

time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org

二,用ntp搭建自己的时间服务器

上面我们是利用别人的时间服务器来同步时间,这些时间服务器都是比较权威的。当我们自己搭建时间服务器就不用crontab来定时去跑。

1,安装时间服务器ntp

  1. yum install ntp

2,配置ntp

  1. [root@localhost ~]# cat /etc/ntp.conf |awk ‘{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}’
  2. restrict default ignore   //默认不允许修改或者查询ntp,并且不接收特殊封包
  3. restrict 127.0.0.1        //给于本机所有权限
  4. restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify  //给于局域网机的机器有同步时间的权限
  5. server time.nist.gov prefer      //设置时间服务器,加prefer表示优先
  6. server 0.asia.pool.ntp.org
  7. server 1.asia.pool.ntp.org
  8. server 2.asia.pool.ntp.org
  9. server  127.127.1.0     # local clock
  10. fudge   127.127.1.0 stratum 10
  11. driftfile /var/lib/ntp/drift
  12. keys /etc/ntp/keys

3,启动 ntp

  1. [root@localhost ~]# /etc/init.d/ntpd start

4,查看并测试

  1. [root@localhost ~]# netstat -upnl |grep ntpd   //查看时程
  2. [root@localhost ~]# ntpq -pn    //查看同步的服务器IP
  3. remote           refid      st t when poll reach   delay   offset  jitter
  4. ==============================================================================
  5. 50.77.217.185   .INIT.          16 u    –   64    0    0.000    0.000   0.000
  6. 202.90.158.4    .INIT.          16 u    –   64    0    0.000    0.000   0.000
  7. 202.71.100.89   .INIT.          16 u    –   64    0    0.000    0.000   0.000
  8. 202.134.1.10    .INIT.          16 u    –   64    0    0.000    0.000   0.000
  9. *127.127.1.0     .LOCL.          10 l   18   64  377    0.000    0.000   0.001
  10. [root@localhost ~]# ntpstat   //同步的结果
  11. synchronised to local net at stratum 11
  12. time correct to within 12 ms
  13. polling server every 512 s

remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。
refid:参考的上一层NTP主机的地址
st:即stratum阶层
when:几秒前曾做过时间同步更新的操作
poll:下次更新在几秒之后
reach:已经向上层NTP服务器要求更新的次数
delay:网络传输过程钟延迟的时间
offset:时间补偿的结果
jitter:Linux系统时间与BIOS硬件时间的差异时间

推荐第一种方法,虽然搭建个时间服务器,还是比较简单的,但是我觉得没有这个必要。如果用别人的时间服务器,每隔10分钟同步一次,可以精确到毫秒。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892