centos时间不同步的解决方法(centos时间同步)


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

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

yum install ntp

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


复制代码
代码如下:

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

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

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

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

time.nist.gov
time.nuri.net
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org

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

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

1,安装时间服务器ntp

1.yum install ntp 

2,配置ntp


复制代码
代码如下:

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

3,启动 ntp

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

4,查看并测试


复制代码
代码如下:

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

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

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

作者:海底苍鹰

CentOS系统时间与UTC时间不一致的解决方法

装好centos 发现date不是咱想要的那个时间,肿么办

1、vi /etc/sysconfig/clock   #编辑时间配置文件

     ZONE="Asia/Shanghai"
     UTC=false                          #设置为false,硬件时钟不于utc时间一致
     ARC=false

2、ln -sf /usr/share/zoneinfo/Asia/Shanghai    /etc/localtime    #linux的时区设置为上海时区

3、ntpdate 192.43.244.18    #对准时间

如果没有安装ntp服务器,刚需要先执行以下命令:

yum install ntp #安装ntp服务器

4、/sbin/hwclock --systohc   #设置硬件时间和系统时间一致并校准

好了,我们的CentOS linux系统时间和计算机硬件时间终于都是cst时间了,并且都是上海时区,终于是正常啦。

centos vps修改时间及时区不同步 终极解决方案

由于初次使用国内hyper-v架构的VPS而且买前要指定装windows还是centos系统,因为木有重装的面板,对于用惯debian的我真是折磨,操作过程中遇到各种小问题。问客服直接一句只懂winows环境,让你装WIN你非要装linux。好吧centos只能自己摸索了。由于hyper-v是微软的一款虚拟化产品,早先是不支持linux的,又因为debian的公司化运营,使debian进入高速发展阶段,就连google也抛弃linux定制而转入debian开发阵营,这让微软感到了一些的压力,于是乎微软拉上centos,这就是为什么hyper-v只支持centos,而不支持其它linux衍生版本了,而且支持的还不是那么靠谱。吐槽完,那就来看正题吧。

  正题:hyper-v架构的centos5.5 VPS 装完lnmp时间不同步,导致网站某些功能无法使用。

  网上找了各种方法均告失败。然后自己又仔细找了一些文档终于搞定了,泪奔啊。

  linux的时间管理很奇怪他分为系统时间和硬件时间还细分成什么UTC时间。-_-|||。

  硬件时间从根本上讲是CMOS时钟

  linux查看硬件时间:hwclock --show

  linux查看系统时间:date -R

  centos VPS的时间及时区同步。

  系统时间部分:

  1. 编辑时区配置文件:vi /etc/sysconfig/clock

  ZONE="Asia/Shanghai"

  UTC=false #把UTC设置为false关闭状态

  ARC=false

  2.运行如下命令

  #删除默认配置

  rm -rf /etc/localtime

  #重新指定配置

  ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  #同步系统时间

  ntpdate -u pool.ntp.org

  3.硬件时间部分:

  #将硬件时间调整成与目前的系统时钟一致。

  hwclock --systohc
  或clock --systohc

  #强制系统时间写入CMOS中防止重启失效

  hwclock -w
  或clock -w

  以上步骤虽然成功了但是我这hyper-v架构的centos系统时间走的不准,1天能快几分钟的样子。

  用crontab来同步一下时间即可解决。

  #编辑计划任务表

  vi /etc/crontab

  #表中最后添加下面这行内容

  30 5 * * * root /usr/sbin/ntpdate -u pool.ntp.org && /sbin/clock --systohc

  #每天5点半同步时间,clock --systohc是硬盘时间同步系统时间的意思。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3