ubuntu

Ubuntu时间戳如何同步网络时间

小樊
42
2025-07-03 11:00:56
栏目: 智能运维

在Ubuntu系统中,时间戳的同步通常是通过网络时间协议(NTP)服务来实现的。以下是同步Ubuntu系统时间戳的步骤:

使用 timesyncd 服务(推荐)

Ubuntu 20.04及之后的版本默认使用 systemdtimesyncd 服务来管理时间同步。timesyncd 服务会自动配置系统时间,并尝试与NTP服务器同步。

  1. 检查当前时间同步状态

    timedatectl status
    

    如果输出显示 System clock synchronized: yes,则表示时间同步正常。

  2. 启动NTP服务(如果尚未启动):

    sudo timedatectl set-ntp on
    

    或者,你可以手动设置NTP服务器:

    sudo timedatectl set-ntp ntp.ubuntu.com
    
  3. 验证时间同步: 再次运行 timedatectl status,确认NTP服务状态为 active

  4. 修改NTP服务器(如果需要使用其他服务器): 编辑 /etc/systemd/timesyncd.conf 文件,取消NTP和FallbackNTP行的注释,并修改为你选择的NTP服务器地址。例如:

    [Time]
    NTP=ntp.tencent.com
    FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com
    

    保存文件后,重启 timesyncd 服务:

    sudo systemctl restart systemd-timesyncd
    

使用 ntpd 服务

如果你需要更精细的控制,可以使用 ntpd 服务。

  1. 安装 ntpd

    sudo apt-get update
    sudo apt-get install ntp
    
  2. 配置 ntpd: 编辑 /etc/ntp.conf 文件,添加或修改NTP服务器地址。例如:

    server 0.pool.ntp.org
    server 1.pool.ntp.org
    server 2.pool.ntp.org
    server 3.pool.ntp.org
    
  3. 启动并启用 ntpd 服务

    sudo systemctl start ntp
    sudo systemctl enable ntp
    
  4. 验证 ntpd 服务状态: 使用命令:

    ntpq -p
    

    确认有服务器被标记为 *,并且 stratum 层级合理,offset 偏移量较小。

通过以上步骤,你可以确保Ubuntu系统的时间戳与网络时间源保持同步,从而保证系统时间的准确性和可靠性。

0
看了该问题的人还看了