1. 两台ubuntu系统都安装ntp
sudo apt install ntp
2 关闭防火墙
sudo iptables -t filter -A INPUT -p udp --destination-port 123 -j ACCEPT
3. 其中一台做主机ntp 服务器 修改配置文件
sudo vim /etc/ntp.conf
增加部分:
# Needed for adding pool entries
restrict xxx.xxx.xxx.0 mask 255.255.255.0 nomodify notrap #增加允许同步从设备的局域网段
xxx部分需要根据自定义局域网网段修改
4.启动ntp服务
sudo /etc/init.d/ntp restart
5. 可以查看时钟同步状态
ntpq -p
6.另外一台做ntp客户端 修改配置文件
sudo vim /etc/ntp.conf
增加、修改部分:
server xxx.xxx.xxx.xxx #增加同步的服务器的ip地址
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
#pool 0.ubuntu.pool.ntp.org iburst
#pool 1.ubuntu.pool.ntp.org iburst
#pool 2.ubuntu.pool.ntp.org iburst
#pool 3.ubuntu.pool.ntp.org iburst
# Use Ubuntu's ntp server as a fallback.
#pool ntp.ubuntu.com #注释掉原有Ubuntu的对时服务的路由
7.ntpq -p 查看时钟同步状态,若有新添加的服务器ip地址则成功
8. 关闭客户端的ntp服务
sudo /etc/init.d/ntp stop
需要关闭客户端的ntp服务,否则会提示ntp服务的套接字已存在,而且该ntp服务会自动开启,因此每次都需要手动关闭
9.手动对时
sudo ntpdate 172.16.4.244 #ntp 服务器的ip
若提示 no server suitable for synchronization found
原因是nptd启动后,大概需要5分钟的时间来同步上一级服务器的时间或自身的时间。
确保ntp服务器已经打开的情况下,隔一段时间重试。