Linux时间同步介绍
在Linux系统中,为了避免主机时间因为在长时间运行下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。Linux系统下,一般使用ntp服务来同步不同机器的时间。NTP 是网络时间协议(Network Time Protocol)的简称,干嘛用的呢?就是通过网络协议使计算机之间的时间同步化。
系统时间与硬件时间
在展开Linux系统时间同步前,我们必须先了解一些概念:在一台计算机上我们有两个时钟:一个称之为硬件时间时钟(RTC Real Time Clock,又叫实时时钟),还有一个称之为系统时钟(System Clock)。
硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因。RTC的英文全称是Real-Time Clock,翻译过来是实时时钟芯片. RTC是PC主板上的晶振及相关电路组成的时钟电路的生成脉冲主板上的晶振及相关电路组成的时钟电路的生成脉冲,RTC经过8254电路的变频产生一个频率较低一点的OS(系统)时钟TSC,系统时钟每一个cpu周期加一周期加一,每次系统时钟在系统初起时通过RTC初始化。8254本身工作也需要有自己的驱动时钟(PIT)
系统时钟就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值。在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了
默认情况下,系统时间和硬件时间,并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行,是靠BIOS电池来维持,而系统时间,是用CPU tick来维持的。这也是系统时间长时间运行时会产生时间偏差的原因,我小时候有买过电子手表,那个时候的电子产品还相当不靠谱,电子手表走着走着就和新闻联播里面的时间对不上了,总会查那么几分钟甚至几十分钟,那个时候最苦恼的事情就是需要手动调整电子手表的时间,这也算是时间同步的一种吧!系统时间可以通过date命令查看,硬件时间可以通过clock或hwclock命令查看。我们来看看系统时间和硬件时间吧。
[root@db-server ~]# date
Wed Jan 14 14:49:16 CST 2015
[root@db-server ~]#
[root@db-server ~]# clock
Wed 14 Jan 2015 02:49:24 PM CST -0.473465 seconds
[root@db-server ~]#
[root@db-server ~]# hwclock --show
Wed 14 Jan 2015 02:49:36 PM CST -0.625346 seconds
[root@db-server ~]#
[root@db-server ~]#
如下所示,我们修改了系统时间,将时间修改为2015-01-14 08:00:00,然后我们查看硬件时间就可以发现,系统时间和硬件时间是异步运行。互补干扰。
[root@db-server ~]# date -s "2015-01-14 08:00:00"
Wed Jan 14 08:00:00 CST 2015
[root@db-server ~]# date
Wed Jan 14 08:00:02 CST 2015
[root@db-server ~]# clock
Wed 14 Jan 2015 02:52:54 PM CST -0.045672 seconds
[root@db-server ~]#
硬件时间的设置,可以用hwclock或者clock命令。其中,clock和hwclock用法相近,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
设置硬件时间
[root@db-server ~]# clock
Wed 14 Jan 2015 03:24:09 PM CST -0.941272 seconds
[root@db-server ~]# hwclock --set --date="2015-01-15 15:24:00";
[root@db-server ~]# clock
Thu 15 Jan 2015 03:24:04 PM CST -0.476775 seconds
[root@db-server ~]#
[root@db-server ~]# clock --help
hwclock - query and set the hardware clock (RTC)
Usage: hwclock [function] [options...]
Functions:
--help show this help
--show read hardware clock and print result
--set set the rtc to the time given with --date
--hctosys set the system time from the hardware clock
--systohc set the hardware clock to the current system time
--adjust adjust the rtc to account for systematic drift since
the clock was last set or adjusted
--getepoch print out the kernel's hardware clock epoch value
--setepoch set the kernel's hardware clock epoch value to the
value given with --epoch
--version print out the version of hwclock to stdout
Options:
--utc the hardware clock is kept in coordinated universal time
--localtime the hardware clock is kept in local time
--directisa access the ISA bus directly instead of /dev/rtc
--badyear ignore rtc's year because the bios is broken
--date specifies the time to which to set the hardware clock
--epoch=year specifies the year which is the beginning of the
hardware clock's epoch value
--noadjfile do not access /etc/adjtime. Requires the use of
either --utc or --localtime
[root@db-server ~]# hwclock --show
Thu 15 Jan 2015 03:40:47 PM CST -0.544844 seconds
[root@db-server ~]# clock --set --date "2015-01-14 15:41:00";
[root@db-server ~]# hwclock --show
Wed 14 Jan 2015 03:41:12 PM CST -0.740512 seconds
[root@db-server ~]#
系统时间和硬件时间的同步
我们可以使用hwclock --systohc把系统时间设置成硬件时间,如下所示(我们先修改系统时间,手工造成系统时间与硬件时间不一致)
[root@db-server ~]# date -s "2015-01-14 15:53";
Wed Jan 14 15:53:00 CST 2015
[root@db-server ~]# date
Wed Jan 14 15:53:03 CST 2015
[root@db-server ~]# hwclock --systohc
[root@db-server ~]# date
Wed Jan 14 15:53:35 CST 2015
[root@db-server ~]# hwclock --show
Wed 14 Jan 2015 03:53:36 PM CST -0.149835 seconds
我们也可以把硬件时间设置成系统时间:运行以下命令即可
hwclock --hctosys
时间同步设置
网络时间协议NTP(Network Time Protocol)是用于互联网中时间同步的标准互联网协议。NTP的用途是把计算机的时间同步到某些时间标准。目前采用的时间标准是世界协调时UTC(Universal Time Coordinated)。NTP的主要开发者是美国特拉华大学的David L. Mills教授。关于NTP概念,深入的话,都足足可以讲一篇。我们暂且只是关注如何同步时间。要同步时间,就必须有一个精确地NTP Server,关于NTP Server可以参考NTP的官方网站http://www.pool.ntp.org,在这上面我们可以找到离我们城市最近的NTP Server
同步世界我们一般使用ntpd或ntpdate这两个命令,那ntpd与ntpdate在更新时间时有什么区别?ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行
一个是校准时间,一个是调整时间。也就是说ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对。而ntpdate不会考虑其他程序是否会阵痛,直接调整时间。
如下所示,我们首先修改系统时间,然后使用ntpdate命令同步系统时间。
[root@DB-Server ~]# date
Tue Jan 27 23:44:54 CST 2015
[root@DB-Server ~]# date -s "2015-12-27 23:48:50"
Sun Dec 27 23:48:50 CST 2015
[root@DB-Server ~]# ntpdate 2.cn.pool.ntp.org
27 Jan 23:45:41 ntpdate[6930]: step time server 202.112.31.197 offset -28857832.720974 sec
[root@DB-Server ~]# date
Tue Jan 27 23:45:44 CST 2015
但是如果cpu tick有问题,这类调整往往很难一次性解决问题,所以必须结合cron命令来彻底根治这个,例如下面cron命令。
*/30 * * * * /usr/sbin/ntpdate 192.168.7.161 1>/dev/null 2>&1
关于ntpd最好单独一章介绍一下如何配置ntpd。在此不做详细介绍。
参考资料:
http://blog.csdn.net/suer0101/article/details/7868813
http://blog.csdn.net/sjx800688/article/details/7010979
http://hoya120.blog.163.com/blog/static/520294792010612103319272/
Linux时间同步介绍的更多相关文章
- linux时间同步,ntpd、ntpdate
linux时间同步,ntpd.ntpdate 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系 ...
- Linux 时间同步 05 chrony时间同步
Linux 时间同步 05 chrony时间同步 目录 Linux 时间同步 05 chrony时间同步 chrony 的优势: chrony包介绍 安装chrony 配置与外部时间服务器进行时间同步 ...
- 01 Linux入门介绍
一.Linux 初步介绍 Linux的优点 免费的,开源的 支持多线程,多用户 安全性好 对内存和文件管理优越 系统稳定 消耗资源少 Linux的缺点 操作相对困难 一些专业软件以及游戏支持度不足 L ...
- Linux时间同步方式记录
Linux时间同步 部署Hadoop集群,遇到各个linux服务器的时间不同步的问题,于是研究了一下linux的时间同步方式,特将同步方式,总结如下: A. 前提条件 a) 网络是连通 ...
- Linux Epoll介绍和程序实例
Linux Epoll介绍和程序实例 1. Epoll是何方神圣? Epoll但是当前在Linux下开发大规模并发网络程序的热门人选,Epoll 在Linux2.6内核中正式引入,和select类似, ...
- Linux入门介绍
Linux入门介绍 一.Linux 初步介绍 Linux的优点 免费的,开源的 支持多线程,多用户 安全性好 对内存和文件管理优越 系统稳定 消耗资源少 Linux的缺点 操作相对困难 一些专业软件以 ...
- linux 时间同步的2种方法(转)
linux 时间同步的2种方法 张映 发表于 2012-10-23 分类目录: 服务器相关 标签:linux, ntp, 同步, 时间服务器 由于硬件的原因,机器或多或少的根标准时间对不上,一个月的误 ...
- Linux 系统目录介绍
bin : bin 是Binary 二进制的缩写,就是可执行文件了.Bin目录下是用户常用的命令. sbin: 此目录下也是二进制文件 ,不过这里的命令是 超级用户如 root 这样的用户使用的. e ...
- Linux命令介绍
资料链接:(Linux基本命令介绍)http://note.youdao.com/share/?id=36c07917f8d3e6437c1e764c3516a3f2&type=note#/ ...
随机推荐
- swift 如何实现点击view后显示灰色背景
有这样一种场景,当我们点击view的时候,需要过0.几秒显示一个灰色或者别的颜色的背景 用button来实现,只有按下去的时候才会出现,往往在快速按下,快速抬起的时候是看不出这个变化的 下边是解决方案 ...
- objective-c 语法快速过(2)
oc类的声明和定义的常见错误 1.只有类的声明,没有类的实现 2.漏了@end 3.@interface和@implementation嵌套,也就是@interface或者@implementatio ...
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...
- Windows 10 密钥分享
Windows 10 Technical Preview for Enterprise:KEY:PBHCJ-Q2NYD-2PX34-T2TD6-233PKhttp://technet.microsof ...
- 使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和解密
在数据库实现加密与解密的文章,Insus.NET较早前也有写过,可以在本博客中可以搜索得到. 今天使用EncryptByPassPhrase和DecryptByPassPhrase来简单实现. 在数据 ...
- input框中的name和id的区别
1. 可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢?! 而同样我们也可以得到最classical的答案:ID就像是一个人的身份证号码,而 ...
- img在div中居中的问题
Img是内联元素,要设置其margin属性使其居中,就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中:(有的设计师为图片再加个div标签,然后通过 ...
- sql语句查询
1. sql语句查询某位数字或者某几位数字开头的数据,字段类型为数字类: %’: 2. sql搜索以4开头和含有李字的数据: select * from wlzbpre_user where real ...
- FASTSOCKET
FASTSOCKET It looks like there are like 3 separate optimizations, but I think the most important one ...
- 数据见50条常用sql
问题及描述: --1.学生表 Student(Sid,Sname,Sage,Ssex) --Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course ...




