liunx时钟与同步

linux的系统时钟在很多地方都要用到,要是不准,
就会出现一些奇怪的问题;

在Linux中,用于时钟查看和设置的命令主要有date、hwclock和clock。
Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。
系统时钟: 是指当前Linux Kernel中的时钟,
硬件时钟: 是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。

当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。

Linux 中的所有命令(包括函数)都是采用的系统时钟设置。
在Linux中,用于时钟查看和设置的命令主要有date、hwclock和clock。
其中,clock和hwclock用法相近,只用一个就行,
只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。

1、 date
查看系统时间
# date

设置系统时间
# date –set “07/07/06 10:19″     //(月/日/年时:分:秒)

2、hwclock/clock
查看硬件时间
# hwclock –show   // 或者
​# clock –show

设置硬件时间
# hwclock –set –date=”07/07/06 10:19″ (月/日/年 时:分:秒)   或者
# clock –set –date=”07/07/06 10:19″ (月/日/年 时:分:秒)


3、硬件时间和系统时间的同步
按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步,
但是在不重新启动的时候,需要用hwclock或clock命令实现同步。

硬件时钟与系统时钟同步:
# hwclock –hctosys   // (hc代表硬件时间,sys代表系统时间)或者
# clock –hctosys


系统时钟和硬件时钟同步:
# hwclock –systohc   // 或者
# clock –systohc


4. 和外部的NTP时间服务器同步
$ service ntpd stop
这一步是必须的,否则出出现:
25 Nov 18:10:34 ntpdate[2106]: the NTP socket is in use, exiting
的失败提示;

$ ntpdate ntp.sjtu.edu.cn
正常返回如下:
25 Nov 18:14:34 ntpdate[2164]: adjust time server 202.120.2.101 offset -0.006107 sec
错误返回如:
25 Nov 18:13:44 ntpdate[2158]: no server suitable for synchronization found

$ service ntpd start
$ chkconfig ntpd on
$ clock -w

还可以写进定时任务中,以做定时的时钟同步:
$ crontab -e
05 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1
05 17 * * * /sbin/clock -w

附上中国大概能用的NTP时间服务器地址
  server 133.100.11.8 prefer
  server 210.72.145.44
  server 203.117.180.36
  server 131.107.1.10
  server time.asia.apple.com
  server 64.236.96.53
  server 130.149.17.21
  server 66.92.68.246
  server www.freebsd.org
  server 18.145.0.30
  server clock.via.net
  server 137.92.140.80
  server 133.100.9.2
  server 128.118.46.3
  server ntp.nasa.gov
  server 129.7.1.66
  server ntp-sop.inria.frserver 210.72.145.44(中国国家授时中心服务器IP地址)
  server ntp.sjtu.edu.cn(上海交通大学网络中心NTP服务器地址)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值