当前位置:K88软件开发文章中心电脑基础基础应用03 → 文章内容

Linux下如何使用命令同步时钟

减小字体 增大字体 作者:华军  来源:华军资讯  发布时间:2019-1-30 23:11:56

  如果Linux系统中的硬件时钟和系统时钟不同步的话,就可能导致一些错误,在需要集中记录服务器日志的环境中,时间同步那是相当的重要的。本文为大家介绍两种同步linux时间的方法,供大家参考。  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服务器地址)  上面就是小编总结的使用命令同步Linux时钟的方法介绍了,一般使用data、hwclock和clock命令,而data命令是比较常用的命令,如果你的系统时钟不同步,那就赶紧改过来吧。

Linux下如何使用命令同步时钟