NTP(Network Time Protocol)------网络时间协议-----应用层协议,用来在分布式时间服务器和客户端之间进行时间同步。
是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。
运行以下命令来安装 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.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 0.ubuntu.pool.ntp.org iburst ” 表示您的NTP客户端将使用 Ubuntu 的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 地址可以访问
-
保存并关闭 /etc/ntp.conf 文件后,重新启动NTP服务使得更改生效:
- sudo systemctl restart ntp
-
重启完成后,我们再检查一下NTP的状态:
- sudo systemctl status ntp
-
以上步骤顺利完成后,我们配置的这台NTP服务器就可以满足客户端使用了。
以Cisco交换机NTP配置为例:
- NTP Server 192.168.2.1
-
这个时候,内网的所有客户端都可以指定NTP server作为时间同步源,确保内网所有终端时间同步一致。