Debian 8 时间同步
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 --
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)