Ubuntu快速搭建内网NTP Server

本文详细介绍了如何在Ubuntu系统上安装NTP服务,配置NTP同步源,允许客户端访问,并重启服务以确保时间同步。包括apt-get安装过程和常见配置步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


NTP(Network Time Protocol)------网络时间协议-----应用层协议,用来在分布式时间服务器和客户端之间进行时间同步。

是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。

安装NTP服务

运行以下命令来安装 NTP 服务器软件(通常为 “ntp” 包),Ubuntu安装NTP方法如下:

sudo apt-get install ntp
mirror@PC:~$ sudo apt-get install ntp
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package ntp is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'ntp' has no installation candidate

根据报错提示,没有来源可以安装,这种情况下,我们需要先执行sudo apt-get update 进行更新。

mirror@PC:~$ sudo apt-get update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:4 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [756 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]
……
Get:39 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [24.3 kB]
Get:40 http://archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [16.4 kB]
Get:41 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [640 B]
Get:42 http://archive.ubuntu.com/ubuntu jammy-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 27.0 MB in 10s (2820 kB/s)
Reading package lists... Done

重新执行安装NTP服务指令sudo apt-get install ntp,安装NTP包的时候,会自动安装NTP关联的其他包,请耐心等待即可。

mirror@PC:~$ sudo apt-get install ntp
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libevent-pthreads-2.1-7 libopts25 sntp
Suggested packages:
  ntp-doc
The following packages will be REMOVED:
  systemd-timesyncd
The following NEW packages will be installed:
  libevent-pthreads-2.1-7 libopts25 ntp sntp
0 upgraded, 4 newly installed, 1 to remove and 87 not upgraded.
Need to get 855 kB of archives.
After this operation, 2295 kB of additional disk space will be used.
Do you want to continue? [Y/n]
# 输入Y后开始下载NTP及关联包
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libevent-pthreads-2.1-7 amd64 2.1.12-stable-1build3 [7642 B]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 libopts25 amd64 1:5.18.16-4 [59.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/universe amd64 ntp amd64 1:4.2.8p15+dfsg-1ubuntu2 [721 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/universe amd64 sntp amd64 1:4.2.8p15+dfsg-1ubuntu2 [67.1 kB]
Fetched 855 kB in 8s (107 kB/s)
(Reading database ... 24137 files and directories currently installed.)
Removing systemd-timesyncd (249.11-0ubuntu3.9) ...
Selecting previously unselected package libevent-pthreads-2.1-7:amd64.
(Reading database ... 24123 files and directories currently installed.)
Preparing to unpack .../libevent-pthreads-2.1-7_2.1.12-stable-1build3_amd64.deb ...
Unpacking libevent-pthreads-2.1-7:amd64 (2.1.12-stable-1build3) ...
Selecting previously unselected package libopts25:amd64.
Preparing to unpack .../libopts25_1%3a5.18.16-4_amd64.deb ...
Unpacking libopts25:amd64 (1:5.18.16-4) ...
Selecting previously unselected package ntp.
Preparing to unpack .../ntp_1%3a4.2.8p15+dfsg-1ubuntu2_amd64.deb ...
Unpacking ntp (1:4.2.8p15+dfsg-1ubuntu2) ...
Selecting previously unselected package sntp.
Preparing to unpack .../sntp_1%3a4.2.8p15+dfsg-1ubuntu2_amd64.deb ...
Unpacking sntp (1:4.2.8p15+dfsg-1ubuntu2) ...
Setting up libopts25:amd64 (1:5.18.16-4) ...
Setting up ntp (1:4.2.8p15+dfsg-1ubuntu2) ...
Created symlink /etc/systemd/system/network-pre.target.wants/ntp-systemd-netif.path → /lib/systemd/system/ntp-systemd-netif.path.
Created symlink /etc/systemd/system/multi-user.target.wants/ntp.service → /lib/systemd/system/ntp.service.
invoke-rc.d: could not determine current runlevel
Setting up libevent-pthreads-2.1-7:amd64 (2.1.12-stable-1build3) ...
Setting up sntp (1:4.2.8p15+dfsg-1ubuntu2) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

配置NTP

配置NTP 同步源

通过对ntp.conf 文件进行配置NTP相关参数。

sudo vi /etc/ntp.conf

在这里插入图片描述
pool 0.ubuntu.pool.ntp.org iburst 是NTP(Network Time Protocol)配置文件中的一行,用于指定NTP服务器的同步源。

默认可以不更改,以Ubuntu的NTP 源作为同步源,通常建议使用已知的公共NTP服务器,我们配置阿里云为NTP同步源:

pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool ntp3.aliyun.com iburst
  • pool:这是指定使用NTP服务器池的关键字。NTP服务器池是一组NTP服务器,它们可以提供时间同步服务。通过使用服务器池,您可以分散请求,降低负载,并提高可用性。

  • 0.ubuntu.pool.ntp.org:这是具体的NTP服务器池的域名。在这个例子中,它指向了 Ubuntu 的NTP服务器池。这个域名将解析为多个NTP服务器的IP地址,您的NTP客户端将随机选择其中一个进行时间同步。

  • iburst:这是一个选项,用于指定NTP客户端在启动时快速与服务器同步时间。它会导致NTP客户端发送一系列请求,以迅速获取准确的时间。这对于在启动时快速同步时间非常有用。

pool 0.ubuntu.pool.ntp.org iburst ” 表示您的NTP客户端将使用 Ubuntu 的NTP服务器池来同步时间,并在启动时快速获取时间同步。

配置NTP 允许客户端访问

默认情况下,NTP服务器只允许本地访问。如果您希望其他计算机能够与您的NTP服务器进行时间同步,需要编辑 /etc/ntp.conf 文件,并将 restrict 行中的 127.0.0.1 更改为 0.0.0.0 或其他允许的IP地址。

restrict 0.0.0.0 mask 0.0.0.0   #允许所有地址访问
restrict 192.168.1.0 mask 255.255.255.0   #表示允许192.168.1.0/24 地址可以访问

在这里插入图片描述

重启NTP服务使得配置生效

保存并关闭 /etc/ntp.conf 文件后,重新启动NTP服务使得更改生效:

sudo systemctl restart ntp

重启完成后,我们再检查一下NTP的状态:

sudo systemctl status ntp

以上步骤顺利完成后,我们配置的这台NTP服务器就可以满足客户端使用了。

以Cisco交换机NTP配置为例:

NTP Server 192.168.2.1

这个时候,内网的所有客户端都可以指定NTP server作为时间同步源,确保内网所有终端时间同步一致。

推荐阅读

<think>嗯,用户之前问过MySQL主从复制的原理,现在又问Ubuntu下使用阿里云的时间同步命令。看起来他可能是在搭建服务器环境,特别是需要配置主从数据库时,时间同步是非常重要的,因为主从服务器之间的时间不一致可能导致数据不一致或者其他问题。 首先,我需要确定用户的需求。他可能是在使用Ubuntu系统,并且想要将系统时间与阿里云的时间服务器同步。时间同步通常使用NTP协议,而Ubuntu默认使用的是systemd-timesyncd,但有时用户可能更倾向于使用ntpdate或者chrony来进行同步。 接下来,我需要回忆一下在Ubuntu上配置时间同步的具体步骤。阿里云提供了自己的NTP服务器地址,比如ntp.aliyun.com或者ntp1.aliyun.com。用户可能想知道如何安装和配置这些服务。 然后,要分几种情况考虑。首先,如果用户使用systemd-timesyncd,这是Ubuntu自带的,配置比较简单,只需要修改配置文件并重启服务。另外,ntpdate是一个传统的工具,可以手动同步时间,但可能已经被弃用,推荐使用chrony或者ntpd。而chrony在现代系统中更为常见,配置灵活,适合有网络波动的情况。 需要检查用户可能使用的不同方法,并给出对应的命令和步骤。比如,安装chrony,修改配置文件中的服务器地址为阿里云的NTP服务器,然后重启服务。同时,还要提醒用户检查服务状态,确认时间是否同步成功。 另外,用户可能需要知道如何手动立即同步时间,而不仅仅是等待服务自动同步。这时候可以使用chronyc或者ntpdate命令(如果安装了的话)。 还要注意权限问题,有些命令可能需要sudo权限,需要在步骤中明确写出。同时,解释每个步骤的作用,帮助用户理解为什么这样做,而不仅仅是复制命令。 最后,考虑到用户之前的问题是关于MySQL主从复制,时间同步对于数据库集群的重要性,可能需要强调时间同步的重要性,以及不同方法的优缺点,帮助用户选择最适合的方案。</think>在Ubuntu系统中,使用阿里云NTP服务器同步时间的常用方法如下: --- ### **一、使用`systemd-timesyncd`(系统默认)** 适用于Ubuntu 16.04及以上版本(默认集成时间同步服务)。 #### **1. 修改NTP服务器配置** ```bash sudo sed -i 's/^#NTP=/NTP=ntp.aliyun.com/' /etc/systemd/timesyncd.conf ``` - 将阿里云NTP服务器 `ntp.aliyun.com` 添加到配置中。 #### **2. 重启服务并生效** ```bash sudo systemctl restart systemd-timesyncd.service sudo systemctl status systemd-timesyncd.service # 检查状态 ``` #### **3. 验证同步结果** ```bash timedatectl show-timesync # 查看当前同步状态 timedatectl # 查看时间同步源是否指向阿里云 ``` --- ### **二、使用`chrony`(推荐)** 适用于需要更高精度或复杂配置的场景。 #### **1. 安装chrony** ```bash sudo apt update && sudo apt install chrony -y ``` #### **2. 修改配置文件** 编辑 `/etc/chrony/chrony.conf`,注释默认服务器并添加阿里云NTP: ```bash sudo nano /etc/chrony/chrony.conf ``` ```ini # 原始配置(注释掉) # pool 2.debian.pool.ntp.org iburst # 添加阿里云NTP server ntp.aliyun.com iburst server ntp1.aliyun.com iburst ``` #### **3. 重启服务并检查** ```bash sudo systemctl restart chrony chronyc sources -v # 查看同步源状态 chronyc tracking # 检查时间同步精度 ``` --- ### **三、使用`ntpdate`(临时手动同步)** 适用于快速手动校准(需先安装)。 #### **1. 安装ntpdate** ```bash sudo apt install ntpdate -y ``` #### **2. 手动执行同步** ```bash sudo ntpdate ntp.aliyun.com ``` #### **3. 写入硬件时钟(可选)** ```bash sudo hwclock --systohc # 将系统时间同步到硬件时钟 ``` --- ### **四、使用`ntpd`(传统NTP服务)** 适用于需要长期后台同步的场景。 #### **1. 安装ntp** ```bash sudo apt install ntp -y ``` #### **2. 修改配置文件** 编辑 `/etc/ntp.conf`,替换默认服务器: ```ini server ntp.aliyun.com iburst server ntp1.aliyun.com iburst ``` #### **3. 重启服务** ```bash sudo systemctl restart ntp ntpq -p # 查看NTP服务器连接状态 ``` --- ### **五、阿里云NTP服务器地址** 可直接使用以下任意一个: ```bash ntp.aliyun.com ntp1.aliyun.com ntp2.aliyun.com time.pool.aliyun.com ``` --- ### **六、验证时间同步结果** 通用命令: ```bash date # 查看当前系统时间 timedatectl # 检查时区及同步状态 ``` --- ### **总结** - **推荐方案**:使用 `chrony`(精度高、适应网络波动)。 - **临时校准**:`ntpdate ntp.aliyun.com`。 - **注意**:若服务器在阿里云ECS内网,可直接用内网NTP服务器 `ntp.cloud.aliyuncs.com`(延迟更低)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Par@ish

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值