每天执行一次 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. Android开发问题积累 <加载在线Gif><WebView无法加载网页图片>

    在线Gif加载 解决办法 Glide完美解决 Glide.with(context).load(pic).placeholder(R.drawable.loading).into(imageView) ...

  2. JSON学习笔记-5

    JSON.parse() 1.从服务器接受数据进行解析(一般是字符串) 2.解析前要确保你的数据是标准的 JSON 格式,否则会解析出错.可以使用线工具检测:https://c.runoob.com/ ...

  3. 观察者设计模式( Observable类Observer接口)

    如果要想实现观察者模式,则必须依靠Java.util包中提供的Observable类和Observer接口. class House extends Observable{ // 表示房子可以被观察  ...

  4. oracle 实现主键自增

    -- 创建表 drop table test; create table test(id number(10), name varchar2(10)); -- 创建对列 drop sequence s ...

  5. windows 设置CapsLock键开启大写后使用shift键取消大写

    1.打开控制面板——>时钟.语言和区域——>更改键盘或其它输入法——>更改键盘——>高级键设置——>要关闭Caps Lock 2.ok

  6. c# 多线程之-- System.Threading Timer的使用

    作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...

  7. JAVA 判断字符长度

    /** * 判断姓名字符长度 * 字符串为空返回0:有中文返回中文字符数:无中文返回2 * @param CustomerSn * @return */ private Integer getName ...

  8. python之线程、进程

    线程语法 class Thread(_Verbose): """A class that represents a thread of control. This cla ...

  9. Oracle使用order by排序关于null值处理

    select * from dual order by age desc nulls last select * from test order by age asc nulls first sqls ...

  10. 教你使用Paw解析http请求

    教你使用Paw解析http请求 软件下载地址: 链接: http://pan.baidu.com/s/1gdzmjq7 密码: 3mpb 这款应用的图片像极了百度,哈哈. 支持的请求方式: 完整的显示 ...