Debian 8 时间同步

每天执行一次 sudo ntpdate ntp.ubuntu.com 逐渐觉得麻烦了,有没有自动执行的方法?

在Linux中用户可以执行例行性的工作,使用crontab这个命令。 

步骤:

1、在终端中输入:

$ crontab -e

2、在打开的文件中输入:

16 10 * * * echo "hello">>/tmp/test.txt

表示10点16分的时候发送“hello”到test.txt这个文件中。

这个方法是普通用户在操作,如果要用到root用户使用的命令怎么办呢?比如ntpdate这个命令普通用户是没法使用的。

如果这样写,希望每一分钟更新一次系统时间:

*/1 * * * * /usr/sbin/ntpdate ntp.ubuntu.com>>/tmp/ntpdate.log

结果发现并没有执行这个任务。

以上 crontable-e 是针对用户的cron来设计的,如果是系统任务,则需编辑 /etc/crontab 这个文件即可。

在终端中输入:

$ sudo vi /etc/crontab

打开后看到有这几行:

# m h dom mon dow user  command
  17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
  25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

分别表示每小时、每天、每周、每月要执行的脚本。

我们来建立一个每小时要执行的任务:

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
* */1 * * *     root    /usr/sbin/ntpdate ntp.ubuntu.com>/tmp/ntpdate.log 这是每分钟执行一次
0 * * * *     root    /usr/sbin/ntpdate ntp.ubuntu.com>/tmp/ntpdate.log 这是每小时执行一次

然后重启服务:

sudo  /etc/init.d/cron restart

 或者 sudo crontab -e 建一个: 

@reboot         /usr/sbin/ntpdate ntp.ubuntu.com > /tmp/ntpdate.log

 


 

Updated:2016-10-22

以上方法似乎不奏效,后来发现Debian 从 jessie 开始使用systemd来管理任务。

$ timedatectl status 
      Local time: 五 2016-10-21 01:34:20 CST
  Universal time: 四 2016-10-20 17:34:20 UTC
        RTC time: 四 2016-10-20 17:34:20
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
$ timedatectl set-ntp true

再看状态:

$ timedatectl status 
      Local time: 五 2016-10-21 01:36:57 CST
  Universal time: 四 2016-10-20 17:36:57 UTC
        RTC time: 四 2016-10-20 17:36:58
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

打开 /etc/systemd/timesyncd.conf:

# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# See timesyncd.conf(5) for details

[Time]
#Servers=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

加入时间更新服务器:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# See timesyncd.conf(5) for details

[Time]
#Servers=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
Servers=ntp.ubuntu.com

这样每次开机就会从ntp.ubuntu.com自动更新时间了。

参见:How to manage system services on Debian Jessie

 https://blog.sleeplessbeastie.eu/2015/04/27/how-to-manage-system-services-on-debian-jessie/

 

查看硬件时间

# hwclock

把硬件的时间写到操作系统(如果hwclock是昨天,则此命令会把系统当前的时间改到昨天):

# hwclock --hctosys

把系统当前的时间写到硬件时钟(写到BIOS ?):这才是我们需要的

# hwclock --systohc

 

-- END --

posted @ 2016-10-14 11:55  ibg  阅读(5113)  评论(0编辑  收藏  举报