Linux 时钟同步

文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部
作者:Anker Lee
本文原文地址:http://ankerlee.github.io/2015/05/21/Linux-时钟同步


时间同步配置

配置时间服务器

[root@localhost etc]# date
20150521日 星期四 22:04:18 CST #错误的时间
[root@localhost etc]# ntpdate
-bash: ntpdate: command not found
[root@localhost etc]# yum install ntpdate
...这里省略安装过程中的输出文本
Complete!
[root@localhost etc]# ntpdate time.nist.gov
21 May 14:06:11 ntpdate[1316]: step time server 128.138.141.172 offset -28801.756893 sec
[root@localhost etc]# date
20150521日 星期四 14:06:18 CST #正确的时间

推荐几个权威的时间同步服务器:

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

自动时间同步设置

[root@localhost etc]# service crond status
crond (pid  1129) 正在运行...
[root@localhost etc]# vim /etc/crontab
#编辑crontab文件,在最后面追加*/10 * * * * ntpdate time.nist.gov,表示没10分钟同步一次时间
[root@localhost etc]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

For details see man 4 crontabs

Example of job definition:
.---------------- minute (0 - 59)
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  * user-name command to be executed

*/10 * * * * ntpdate time.nist.gov
[root@localhost etc]#

crontab文件格式和含义:

minute: 表示分钟,可以是从0到59之间的任何整数。
hour:表示小时,可以是从0到23之间的任何整数。
day:表示日期,可以是从1到31之间的任何整数。
month:表示月份,可以是从1到12之间的任何整数。
week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。

在以上各个字段中,还可以使用以下特殊字符:
星号():代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如
/10,如果用在minute字段,表示每十分钟执行一次。