以172.20.1.152作为时钟服务器,其他服务器根据这台服务器进行时钟同步。
#检查是否已经安装,出现了红框中的就说明已经安装了 rpm -qa | grep ntp #安装ntp yum install ntp
vi /etc/ntp.conf #代表允许172.20网段的服务器与此服务器进行时间同步,根据需要进行替换id和子网掩码即可
# Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. restrict 172.20.1.0 mask 255.255.255.192 nomodify notrap server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10
service ntpd start
chkconfig ntpd on
#之后还需要对ntp的默认端口123进行放行,这个一定要配,除非你关了防火墙,否则会报错,no server suitable for #synchronization found iptables -I INPUT -p tcp --dport 123 -j ACCEPT iptables -I INPUT -p udp --dport 123 -j ACCEPT
#检查是否已经安装,出现了红框中的就说明已经安装了 rpm -qa | grep ntp #安装ntp yum install ntp
vi /etc/ntp.conf
记得把地址替换为你自己的那台时间服务器的地址
server 172.20.1.152 fudge 172.20.1.152 stratum 10
service ntpd start
chkconfig ntpd on
ntpdate -d 172.20.1.152 #显示以下内容代表成功
[root@host-172-20-1-158 tomcat9-llqt-7003]# ntpdate -d 172.20.1.152 25 Jun 09:58:57 ntpdate[13538]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 172.20.1.152 and service ntp host found : 172.20.1.152 transmit(172.20.1.152) receive(172.20.1.152) transmit(172.20.1.152) receive(172.20.1.152) transmit(172.20.1.152) receive(172.20.1.152) transmit(172.20.1.152) receive(172.20.1.152) server 172.20.1.152, port 123 stratum 11, precision -24, leap 00, trust 000 refid [172.20.1.152], delay 0.02586, dispersion 0.00000 transmitted 4, in filter 4 reference time: e660efe3.56e033ce Sat, Jun 25 2022 10:22:27.339 originate timestamp: e660f01a.2e8a535d Sat, Jun 25 2022 10:23:22.181 transmit timestamp: e660ea67.47801f45 Sat, Jun 25 2022 9:59:03.279 filter delay: 0.02621 0.02591 0.02586 0.02589 0.00000 0.00000 0.00000 0.00000 filter offset: 1458.902 1458.902 1458.902 1458.902 0.000000 0.000000 0.000000 0.000000 delay 0.02586, dispersion 0.00000 offset 1458.902283 25 Jun 09:59:03 ntpdate[13538]: step time server 172.20.1.152 offset 1458.902283 sec
ntpq -p #when代表最近同步时间
#查看已有定时任务 crontab -l #添加定时任务 crontab -e #每天凌晨一点20,自动从172.20.1.152同步一次 20 1 * * * ntpdate 172.20.1.152