目录
1. 安装ntp
查看是否安装了 ,一般是安装了
rpm -qa | grep ntp
rpm -qa | grep ntpdate
安装
yum -y install ntp ntpdate
确保3台服务器的网络可以通信,防火墙是相互放开的。
2. 服务端配置
[root@observer1 ~]# vi /etc/ntp.conf
(1)注释掉所有的restrict开头的配置
(2)修改#restrict ip mask ... nomodify notrap
表示该网络网段所有主机可以使用这个时间服务
restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap
(3)将所有server配置进行注释
(4)添加下面两行内容
本地部署,用自身的时间作为NTP服务的时间来源,如下所示
server 127.127.1.0
fudge 127.127.1.0 stratum 10
配置好的ntp.conf内容如下:
(5)启动
# 启动ntp
systemctl start ntpd
# 开机启动
systemctl enable ntpd
# 查看状态
systemctl status ntpd
#停止ntp
systemctl stop ntpd
3. 客户端配置
在agent主机上进行配置observer2、observer3
3.1 测试端口
先测试下与服务端是不是通的,端口是123
nc -vuz 192.168.23.121 123
3.2 配置ntp.conf
[root@observer2 ~]# vi /etc/ntp.conf
①注释所有restrict和server开头的配置
②添加server服务端ip
# 服务端ip地址,默认客户端每5~20分钟会同步服务端的时间(同步间隔可能会变动)
server xx.xx.xx.xx
配置好的ntp.conf内容如下
3.3 手动同步时间测试
先确保停止ntpd服务
systemctl stop ntpd
手动同步时间
ntpdate observer1
显示如下内容为成功:
6 Sep 12:17:23 ntpdate[44826]: adjust time server 192.168.23.121 offset 0.000013 sec
如果显示如下内容需要先关闭ntpd:
6 Sep 12:15:32 ntpdate[88852]: the NTP socket is in use, exiting
3.4 启动
# 启动ntp
systemctl start ntpd
# 开机启动
systemctl enable ntpd
# 查看状态
systemctl status ntpd
#停止ntp
systemctl stop ntpd
3.5 拓展
如果嫌弃默认5分钟同步慢,可以进行定时任务手动同步。
配置定时任务,每分钟同步一次
crontab -e
*/1 * * * * ntpdate observer1
停止ntpd
# 停止ntp
systemctl stop ntpd
# 禁止开机启动
systemctl disable ntpd
# 查看状态
systemctl status ntpd
4. 测试时间同步
服务端修改时间,看客户端是否同步
date -s "2024-09-06 12:05:00"