每天执行一次 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. RNN & LSTM & GRU 的原理与区别

      RNN 循环神经网络,是非线性动态系统,将序列映射到序列,主要参数有五个:[Whv,Whh,Woh,bh,bo,h0][Whv,Whh,Woh,bh,bo,h0],典型的结构图如下: 和普通神经网 ...

  2. 记一次Android studio升级之后的坑

    像往常一样打开Android studio,但这次它提示我升级!说是什么为了更好的体验,在好奇心的驱使下,我毅然地点击了“update”按钮.升级之后,编译项目,报出了N多个error,我的心都慌完! ...

  3. Vue 框架-12-Vue 项目的详细开发流程

    Vue 框架-12-Vue 项目的详细开发流程 首先,如果你还不了解 Vue 脚手架怎么搭建? 默认的环境中有哪些文件? 文件大概是什么作用? 那么,您要先查看之前的文章才有助于你理解本篇文章: Vu ...

  4. [转] iOS 动画库 Pop 和 Canvas 各自的优势和劣势是什么?

    iOS 动画库 Pop 和 Canvas 各自的优势和劣势是什么? http://www.zhihu.com/question/23654895/answer/25541037 拿 Canvas 来和 ...

  5. Windows批量修改文件夹及子文件夹下文件的扩展名

    实例:将 D:/backup 目录下所有后缀名为 “.zip” 的文件替换为 “.exe” 后缀 bat批处理: @echo off rem 不显示执行过程 D: rem 切换至指定盘符 cd D:/ ...

  6. 《C++ Primer Plus》读书笔记之十—类和动态内存分配

    第12章 类和动态内存分配 1.不能在类声明中初始化静态成员变量,这是因为声明描述了如何分配内存,但并不分配内存.可以在类声明之外使用单独的语句进行初始化,这是因为静态类成员是单独存储的,而不是对象的 ...

  7. 资料整理,SQL Server ,面试前复习笔记

    T-SQL 要掌握的知识点分类 SQL 面向数据库执行查询 SQL 从数据库取回数据 SQL 在数据库中插入新的记录 SQL 更新数据库中的数据 SQL 从数据库删除记录 SQL 创建新数据库 SQL ...

  8. 【Alpha】Daily Scrum Meeting——blog1

    团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 忘记拍了,能补上不? 2. 每个人的工作 3. 燃尽图

  9. 使用Oracle的instr函数与索引配合提高模糊查询的效率

    使用Oracle的instr函数与索引配合提高模糊查询的效率 一般来说,在Oracle数据库中,我们对tb表的name字段进行模糊查询会采用下面两种方式:1.select * from tb wher ...

  10. setnx redis

    使用锁 1)setnx(lockkey, 当前时间+过期超时时间) ,如果返回1,则获取锁成功:如果返回0则没有获取到锁,转向2.2.)get(lockkey)获取值oldExpireTime ,并将 ...