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

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

步骤:

1、在终端中输入:

$ crontab -e

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

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

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

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

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

*/ * * * * /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
* * * * root cd / && run-parts --report /etc/cron.hourly
* * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
* * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
* * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

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

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

# m h dom mon dow user  command
* * * * root cd / && run-parts --report /etc/cron.hourly
* * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
* * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
* * 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: 五 -- :: CST
Universal time: 四 -- :: UTC
RTC time: 四 -- ::
Time zone: Asia/Shanghai (CST, +)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
$ timedatectl set-ntp true

再看状态:

$ timedatectl status
Local time: 五 -- :: CST
Universal time: 四 -- :: UTC
RTC time: 四 -- ::
Time zone: Asia/Shanghai (CST, +)
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() for details [Time]
#Servers=.debian.pool.ntp.org .debian.pool.ntp.org .debian.pool.ntp.org .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() for details [Time]
#Servers=.debian.pool.ntp.org .debian.pool.ntp.org .debian.pool.ntp.org .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 --

Debian 8 时间同步的更多相关文章

  1. 使用NTP协议服务器时间同步

    NTP是用来使系统和一个精确的时间源保持时间同步的协议.建议大家在自己管理的网络中建立至少一台时间服务器来同步本地时间,这样可以使得在不同的系统上处理和收集日志和管理更加容易.我们分别从windows ...

  2. Debian系统 + XFCE桌面初识,基础环境搭建

    有幸分享个人的Linux下的习惯配置,具体操作可能阐述得比较粗糙. 在图形化界面进行配置操作,十分简便舒心. Linux发行版:Debian9.5(Stretch) 桌面Sesion:XFCE4 一. ...

  3. ROS 时间同步问题

    0. 问题 两台ubuntu主机无法与一台debian主机使用分布式通信,摄像头发出的话题机器人收不到,考虑是时间同步的问题. 也可能是系统不统一的问题; 今天在家实验了一下,时间差6min,照样可以 ...

  4. [RTC]系统时间NTP和RTC同步,Debian的时区配置

    转自:http://www.cnblogs.com/jiu0821/p/5999566.html Debian的时区配置 一. 修改时区 1. 查看当前时区 命令 : "date -R&qu ...

  5. Linux定时任务工具crontab详解及系统时间同步

    Linux配置自动时间同步 linux下时间同步的两种方法分享 tail -f /var/log/cron linux下定时执行任务的方法  在LINUX中你应该先输入crontab -e,然后就会有 ...

  6. Debian Gun/linux基本用法

    添加软件源:vim /etc/apt/sources.list 在文本中添加如下内容:deb http://mirrors.163.com/debian/ stretch main non-free ...

  7. ambari过程中要求各个节点时间同步

    设置时间同步 控制节点机器 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #设置时区为北京时间,这里为上海,因为centos里面只有上海... ...

  8. Debian以下的ntp服务(ntpdate)的安装

    /*********************************************************************  * Author  : Samson  * Date   ...

  9. debian 9 安装后需做的几件事

    debian 9 安装后需做的几件事 安装环境:X86 >> Debian 9 Linux/GNU apt源更新 注意连上有线网络 刚安装好的debian系统中,/etc/apt/sour ...

随机推荐

  1. Mysql 删除数据表的三种方式详解

    用法: 1.当你不再需要该表时, 用 drop; 2.当你仍要保留该表,但要删除所有记录时, 用 truncate; 3.当你要删除部分记录或者有可能会后悔的话, 用 delete. 删除强度:dro ...

  2. Xcode 7安装KSImageNamed 不启作用

    1. 到 ~/Library/Developer/Xcode/Plug-ins  删除KSImageNamed.xcplugin 2. 在~/Library/Developer/Xcode/Plug- ...

  3. linux 文件搜索命令find、-name、-iname、通配符*?、-size、-user、-group、-amin、-cmin、-mmin、-a、-o、-exec/-ok、-inum

    尽可能规划好目录结构,少用文件搜索操作,因为它会占用大量的系统资源 find /bin/find find [搜索范围] [匹配条件] find /etc -name initfind /etc -n ...

  4. 结对编程的感想&收获

    关于结对编程的感想.感受,见我的另一篇随笔——<构建之法>结对编程   感想 下面我来谈谈本次结对编程的收获以及发现的问题 收获 ①这是我人生中第一次做UI界面设计,刚拿到这个题目还是比较 ...

  5. zabbix系列之七——安装后配置二Userparameters

    1User parameters(用户自定义参数) 1.1配置 描述 详细 备注 简介 1执行zabbix中未预定义的agent check时使用 配置 1)    zabbix agent的配置文件 ...

  6. Oracle 启用归档

    [applprod@erp10 ~]$ watch ps -fu applprod[applprod@erp10 ~]$ kill -9 82902 84923 [applprod@erp10 ~]$ ...

  7. Semaphore-信号灯机制

    我们创建一个可扩展大小的线程池,并且需要在线程池内同时让有限数目的线程并发运行时,就需要用到Semaphore(信号灯机制),Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目 ...

  8. 计算机中的换行符、回车符、\n、\r、\n\r 怎么区分啊?

    '\r'是回车,前者使光标到行首,(carriage return)'\n'是换行,后者使光标下移一格,(line feed) \r 是回车,return\n 是换行,newline对于换行这个动作, ...

  9. JDBC方式执行SQL,支持CRUD返回LIST

    背景: 用惯了Mybatis,接收一个老项目使用Hibernate,特别不习惯.新的功能需要系统后台定时执行任务,顾使用JDBC封装工具类执行 源代码 import java.sql.Connecti ...

  10. 一、HTML概述 二、web相关的概念 三、HTML的常用标签

    一.HTML概述###<1>概念 HTML:Hypertext Markup Language,超文本 标记语言,用来描述网页的一种语言. 非编程语言,由浏览器直接解释运行. ###< ...