---
title: "Linux与远程时间服务器同步"
author: "hanjiangit"
date: "2010-01-28T11:06:16Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2010-01-28T11:07:59Z"
id: 581876
url: "https://wallboy.iteye.com/blog/581876"
---

\# touch /var/log/ntpdate.log  
//创建一个日志文件  
#crontab -e  
进入crontab编辑模式，使用方法同vi  
输入 0 23 * * * /usr/sbin/ntpdate asia.pool.ntp.org &gt;&gt; /var/log/ntpdate.log  
保存退出  
这样就完成了你的系统到每天23：00去asia.pool.ntp.org 同步时间，并将同步的日志放到  
0-59/2 * * * * /usr/sbin/ntpdate asia.pool.ntp.org &gt;&gt; /var/log/ntpdate.log  
0 0-23/2 * * * /usr/sbin/ntpdate 210.72.145.44 &gt;&gt; /var/log/ntpdate.log  
保存退出  
这样就完成了你的系统到每隔2分钟去asia.pool.ntp.org 同步时间，并将同步的日志放到  
/var/log/ntpdate.log  
Linux时间同步设置2009-06-19 10:291.首先需了解linux内一任务计划工具crontab  
crontab可以定时去执行你要做的动作  
直接用crontab命令编辑  
crontab -u //设定某个用户的cron服务，一般root用户在执行这个命令的时候需要此参数  
crontab -l //列出某个用户cron服务的详细内容  
crontab -r //删除某个用户的cron服务  
crontab -e //编辑某个用户的cron服务  
一般使用crontab -e来编辑一个计划动作，编辑模式同vi编辑器相同  
crontab内的基本编辑格式如下  
*　　*　　*　　*　　*　　command  
分　 时　 日　 月　周　 命令  
第1列表示分钟1～59 每分钟用\*或者 \*/1表示  
第2列表示小时1～23（0表示0点）  
第3列表示日期1～31  
第4列表示月份1～12  
第5列标识号星期0～6（0表示星期天）  
第6列要运行的命令  
crontab文件的一些例子：  
30 21 * * * /etc/init.d/smb restart  
上面的例子表示每晚的21:30重启smb 。  
45 4 1,10,22 * * /etc/init.d/smb restart  
上面的例子表示每月1、10、22日的4 : 45重启smb 。  
10 1 * * 6,0 /etc/init.d/smb restart  
上面的例子表示每周六、周日的1 : 10重启smb 。  
0,30 18-23 * * * /etc/init.d/smb restart  
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启smb 。  
0 23 * * 6 /etc/init.d/smb restart  
上面的例子表示每星期六的11 : 00 pm重启smb 。  
\* \*/1 * * * /etc/init.d/smb restart  
每一小时重启smb  
\* 23-7/1 * * * /etc/init.d/smb restart  
晚上11点到早上7点之间，每隔一小时重启smb  
0 11 4 * mon-wed /etc/init.d/smb restart  
每月的4号与每周一到周三的11点重启smb  
0 4 1 jan * /etc/init.d/smb restart  
一月一号的4点重启smb  
2.寻找一个网络时间服务器，比如一些国家授时中心  
微软公司授时主机(美国)  
time.windows.com  
台警大授时中心(台湾)  
asia.pool.ntp.org  
中科院授时中心(西安)  
210.72.145.44  
网通授时中心(北京)  
219.158.14.130  
3.我们的电脑怎样去同步授时中心的时间  
ntpdate asia.pool.ntp.org  
上面此命令就是去同步授时中心的时间  
如果要让系统每天23：00去同步，这样可以使用上面所将的crontab设置计划同步  
如下命令  
#crontab -e  
进入crontab编辑模式，使用方法同vi  
输入 0 23 * * * ntpdate asia.pool.ntp.org &gt;&gt; /var/log/ntpdate.log  
保存退出  
这样就完成了你的系统到每天23：00去asia.pool.ntp.org 同步时间，并将同步的日志放到  
/var/log/ntpdate.log  
下面补充一下系统时间的设置  
date -s可以直接设置系统时间  
比如将系统时间设定成1996年6月10日的命令如下。  
#date -s 06/10/96  
将系统时间设定成下午1点12分0秒的命令如下。  
#date -s 13:12:00  
注意，这里说的是系统时间，是linux由操作系统维护的。  
在系统启动时，Linux操作系统将时间从CMOS中读到系统时间变量中，以后修改时间通过修改系统时间实现。为了保持系统时间与CMOS时间的一致性，Linux每隔一段时间会将系统时间写入CMOS。由于该同步是每隔一段时间（大约是11分钟）进行的，在我们执行date -s后，如果马上重起机器，修改时间就有可能没有被写入CMOS,这就是问题的原因。如果要确保修改生效可以执行如下命令。  
#clock -w  
这个命令强制把系统时间写入CMOS