服务器时间同步--ntp、ntpdate方式

1、配置ntp

  • 安装ntp
命令1: yum list | grep ntp //查看是否有相关安装包
/*
[root@test1 ~]# yum list| grep ntp
ntp.x86_64                         4.2.6p5-28.el7.centos   @xmcsdn_local_iso_repo
ntpdate.x86_64                     4.2.6p5-28.el7.centos   @xmcsdn_local_iso_repo
fontpackages-devel.noarch          1.44-8.el7              xmcsdn_local_iso_repo
fontpackages-filesystem.noarch     1.44-8.el7              xmcsdn_local_iso_repo
fontpackages-tools.noarch          1.44-8.el7              xmcsdn_local_iso_repo
ntp-doc.noarch                     4.2.6p5-28.el7.centos   xmcsdn_local_iso_repo
ntp-perl.noarch                    4.2.6p5-28.el7.centos   xmcsdn_local_iso_repo
python-ntplib.noarch               0.3.2-1.el7             xmcsdn_local_iso_repo
sntp.x86_64                        4.2.6p5-28.el7.centos   xmcsdn_local_iso_repo
*/
命令2: yum install -y ntp
  • 修改配置文件ntp.conf
命令1: vim /etc/ntp.conf

/*
添加以下内容:
restrict 127.0.0.1
restrict 10.8.31.0 mask 255.255.255.128 nomodify

server 10.8.19.92 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 8
*/

命令2: service ntpd restart  //重启ntp

命令3: service ntpd status   //查看状态

  • 配置完,其他主机可以通过ntpdate命令,直接同步该主机的时间

2.配置ntpdate

  • 配置定时任务
命令1:crontab -e
/*
添加以下内容:其中 10.8.19.92 为ntp配置的主机IP
*/15 * * * * /usr/sbin/ntpdate 10.8.19.92;/sbin/hwclock -w
*/
  • 手动同步测试
命令1:ntpdate 10.8.19.92
/*
8 Feb 15:09:34 ntpdate[1055368]: step time server 10.8.19.92 offset 347.512054 sec
*/
    
命令2:ntpdate -d 10.8.19.92 //可以看到详细信息,方便debug
/*
 8 Feb 16:18:33 ntpdate[1086631]: ntpdate 4.2.6p5@1.2349-o Fri Apr 13 12:52:28 UTC 2018 (1)
Looking for host 10.8.19.92 and service ntp
host found : 10.8.19.92
transmit(10.8.19.92)
receive(10.8.19.92)
transmit(10.8.19.92)
receive(10.8.19.92)
transmit(10.8.19.92)
receive(10.8.19.92)
transmit(10.8.19.92)
receive(10.8.19.92)
server 10.8.19.92, port 123
stratum 9, precision -24, leap 00, trust 000
refid [10.8.19.92], delay 0.02577, dispersion 0.00000
transmitted 4, in filter 4
reference time:    e5aca5a8.c2e82a9b  Tue, Feb  8 2022 16:17:44.761
originate timestamp: e5aca5df.19db082f  Tue, Feb  8 2022 16:18:39.100
transmit timestamp:  e5aca5df.19ffaa4d  Tue, Feb  8 2022 16:18:39.101
filter delay:  0.02583  0.02579  0.02577  0.02577 
         0.00000  0.00000  0.00000  0.00000 
filter offset: -0.00071 -0.00071 -0.00070 -0.00068
         0.000000 0.000000 0.000000 0.000000
delay 0.02577, dispersion 0.00000
offset -0.000701

 8 Feb 16:18:39 ntpdate[1086631]: adjust time server 10.8.19.92 offset -0.000701 sec

*/

常见错误

错误1: the NTP socket is in use, exiting

解决:
	命令1: lsof -i:123   //看占用123端口的进程
/*
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ntpd    124171  ntp   16u  IPv4 420557      0t0  UDP *:ntp 
ntpd    124171  ntp   17u  IPv6 420558      0t0  UDP *:ntp 
ntpd    124171  ntp   18u  IPv6 420562      0t0  UDP localhost:ntp 
ntpd    124171  ntp   19u  IPv6 420563      0t0  UDP [fe80::1251:72ff:fe31:f52b]:ntp 
ntpd    124171  ntp   20u  IPv6 420564      0t0  UDP [fe80::6a91:d0ff:fe60:a9d4]:ntp 
ntpd    124171  ntp   21u  IPv4 420565      0t0  UDP localhost:ntp 
ntpd    124171  ntp   22u  IPv4 420566      0t0  UDP 218.94.214.29:ntp
*/
	命令2: kill -9 124171       //杀掉pid
	命令3: ntpdate 10.8.19.92   //再次手动同步时间


版权声明:本文为qq_38930129原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。