每天执行一次 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 CSTUniversal time: 四 2016-10-20 17:34:20 UTCRTC time: 四 2016-10-20 17:34:20Time zone: Asia/Shanghai (CST, +0800)   NTP enabled: no
NTP synchronized: noRTC in local TZ: noDST active: n/a

$ timedatectl set-ntp true

再看状态:

$ timedatectl status Local time: 五 2016-10-21 01:36:57 CSTUniversal time: 四 2016-10-20 17:36:57 UTCRTC time: 四 2016-10-20 17:36:58Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes
NTP synchronized: noRTC in local TZ: noDST 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 --

转载于:https://www.cnblogs.com/ibgo/p/5959610.html

Debian 8 时间同步相关推荐

  1. Debian 设置时间同步

    备份&修改 root@gpu002:/etc/systemd# cp /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.conf.bak.o ...

  2. debian时间同步_如何在Debian 10上设置时间同步

    debian时间同步 介绍 (Introduction) Accurate timekeeping has become a critical component of modern software ...

  3. Debian/Ubuntu/Raspbian 时间同步

    时区设置 先使用如下命令查看自己系统设置的时区 date -R 如果输出的不是+8000,那么需要调整时区: dpkg-reconfigure tzdata 选择asia,中国的城市即可. 调整时间 ...

  4. Linux系统时间同步:ntpdate--在内网中同步时间

    之前写过一篇文章:Debian/Ubuntu/Raspbian 时间同步 我这里引用一下: 先使用如下命令查看自己系统设置的时区 date -R 如果输出的不是+8000,那么需要调整时区: dpkg ...

  5. linux debian ntp,Debian下面的ntp服务(ntpdate)的安装

    NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内 /********** ...

  6. Linux上下载chronyd安装包,如何在Linux中安装和使用Chrony时间同步

    原标题:如何在Linux中安装和使用Chrony时间同步 来自:Linux迷 https://www.linuxmi.com/ Chrony是网络时间协议(NTP)的灵活实现.它用于同步来自不同NTP ...

  7. Linux时间同步(内外网/内网/外网)

    2019独角兽企业重金招聘Python工程师标准>>> 内外网集群的时间同步① server端:可以访问外网的机器 client端:在内网里的机器 一.server端安装ntp校时包 ...

  8. Debian自动化安装

    定制Debian的自动化安装 1 更改grub配置文件,修改安装系统选项,加载安装配置文件(/boot/grub/grub.cfg) if loadfont $prefix/font.pf2 ; th ...

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

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

最新文章

  1. CentOS7部署ELK5.2
  2. php 字符串转时间戳_php如何将时间字符串转换为时间戳
  3. 如何用Python从本地将一个文件备份到Google Drive
  4. SQLServer中进行sql除法运算结果为小数时显示0的解决方案
  5. golang 赋值错误 no new variables on left side of :=
  6. 浅谈Logistic回归及过拟合
  7. IIS怎么设置本地域名解析(本地域名测试配置)
  8. 探索MySql.Data.dll
  9. lintcode 中等题:A + B Problem A + B 问题
  10. 【招聘(北京)】北森测评招聘 .NET 架构师、高级工程师
  11. python提示对话框自动关闭_Python实现定时自动关闭的tkinter窗口方法
  12. nyoj 269 VF 动规
  13. git向码云上提交项目
  14. 电子书 Java程序员面试宝典(第4版).pdf
  15. 笔记本电脑怎么拆开后盖_联想笔记本电脑怎么拆开后盖_联想笔记本怎么拆
  16. [推荐]《人一生要读的60本书》
  17. 安卓的个性化彩色二维码的完美实现
  18. 珠宝类主播带货:为什么大家现在都选择去做直播基地?
  19. 服务器gpt分区不能安装系统,安装Win10原版系统提示“Windows无法安装到GPT分区形式磁盘”怎么办...
  20. Prim 算法的实现

热门文章

  1. 【Hive】命令行提示符中显示当前所在数据库
  2. 【解决方案 二】---设置mysql5.7编码集为utf8mb4
  3. springboot之全局处理异常封装
  4. 解决<c:if>无else的问题
  5. 【Golang】解决Go test执行单个测试文件提示未定义问题
  6. 未经授权的ADB Android设备
  7. 这四行棘手的C代码背后的概念
  8. Heroku + node.js错误(Web进程在启动后60秒内未能绑定到$ PORT)
  9. win11beta版如何升级正式版 Windows11beta升级正式版的步骤方法
  10. urtlebot3启动键盘控制后 /cmd_vel 会持续输出数据,在键盘不给信号时 /cmd_vel 续输,导致此功能与导航同时启动,会导致小车自动行走时一顿一顿的