当前位置:   article > 正文

最详细Ubuntu 时间同步教程_ubuntu时间同步

ubuntu时间同步

前言

我的网站教书先生免费API:https://api.oioweb.cn/,用的宿迁服务器,宿迁服务器真是个大坑货,屏蔽了123udp端口(据说是为了防止攻击)只能用他们机房的ntp服务器去同步。默认的话服务器只要联网就可以自动同步,现在我们需要手动配置一下了。但是系统时间这块我也不是很懂,但总是给问题解决了,正好记录一下以备不时之需。
首先是先看下服务器有没有自动同步

  • 查看时间同步状态
timedatectl status
  • 1

输出大概是这样的信息

root@ubuntu:~# timedatectl status
               Local time: Sun 2023-10-22 20:52:23 CST
           Universal time: Sun 2023-10-22 12:52:23 UTC
                 RTC time: Sun 2023-10-22 12:52:23
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: n/a
          RTC in local TZ: no
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

System clock synchronizedno 则是没有同步,yes就是正常同步的,那这个教程就不用继续看了

  • 设置时间为上海时间
sudo timedatectl set-timezone Asia/Shanghai
  • 1

方法1

此方法为系统自带的时间同步,强烈推荐,博主手残 一下安装了ntp,导致系统自带的systemd-timesyncd无法同步了(这块不是很懂,就一直启动不了systemd-timesyncd了),也就只能用ntp去同步了,此方法是使用系统自带的同步方式,说下怎么配置。

配置文件

/etc/systemd/timesyncd.conf
  • 1

打开后默认大概是这样的

#  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.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

需要自定义的话,就给FallbackNTP取消注释,按下面的格式换成自己要用的ntp服务器地址

[Time]
NTP=your.ntp.server
  • 1
  • 2

将"your.ntp.server"替换为您想要使用的NTP服务器的地址。您可以指定多个NTP服务器,以空格分隔。

重新启动systemd-timesyncd服务

重新启动systemd-timesyncd服务以使更改生效:

sudo systemctl restart systemd-timesyncd
  • 1

现在,systemd-timesyncd服务将使用您自定义的NTP服务器地址进行时间同步。还有一点需要注意,你查询的时候它不是立马就显示正常同步的,你需要等它自动同步以后,它才会显示yes,不然你会以为没同步 一直改。

方法2

如果你跟我一样,因为systemd-timesyncd无法同步,就直接安装了其他时间同步软件,并且systemd-timesyncd一直无法启动了,那你就看这个方法吧。

安装ntp

sudo apt-get install ntp
  • 1

修改配置文件

  • 原版配置文件
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 自定义ntp服务器
# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

把server取消注释,换成自己需要的ntp服务器地址,下面的pool你也原模原样的复制一条,给ntp服务器地址改一下,然后保存。

  • 重启NTP服务

    配置文件更改后,需要重新启动NTP服务以使更改生效。运行以下命令:

sudo service ntp restart
  • 1

**注意:**如果你这时就查看同步状态的话,它也不是立马就显示yes的,需要等它同步以后状态才会更新

检查NTP同步状态

ntpq -p
  • 1

这将显示NTP服务器的状态信息,包括各个NTP服务器的延迟、偏移和同步状态。如果NTP服务器成功同步,你将看到"reach"列中的数字大于0,以及"when"列中的时间戳,表示上次同步时间。

  • 大概如下
root@ubuntu:~# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 ntp.jsidc.com   .POOL.          16 p    -   64    0    0.000   +0.000   0.000
 0.debian.pool.n .POOL.          16 p    -   64    0    0.000   +0.000   0.000
 1.debian.pool.n .POOL.          16 p    -   64    0    0.000   +0.000   0.000
 2.debian.pool.n .POOL.          16 p    -   64    0    0.000   +0.000   0.000
 3.debian.pool.n .POOL.          16 p    -   64    0    0.000   +0.000   0.000
*180.101.160.71  114.118.7.161    2 u   57   64  377    0.410   -5.814   3.178
+180.101.160.70  203.107.6.88     3 u    6   64  377    1.211   -4.406   0.643
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

检查系统时间

使用 date 命令来检查系统时间,确保它与你预期的时间一致:

date
  • 1
  • 输出如下
root@ubuntu:~# date
Sun Oct 22 09:08:13 PM CST 2023
  • 1
  • 2

手动同步时间

有时,手动同步时间可以解决问题。你可以尝试运行以下命令手动同步时间:

sudo ntpdate -s time.nist.gov
  • 1

这会强制同步系统时间。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/796816
推荐阅读
相关标签
  

闽ICP备14008679号