linux实现自动时间同步

深度链接 / 2023-12-06 21:48:36 / 211

如何使linux服务器时间与NTP服务器时间同步?

1、NTP服务器 

Network Time Protocol (NTP)是用来使计算机时间同步的一种协议。NTP服务器可以自行搭建也可以使用一些现有NTP服务器

2、安装ntpdate

#查看是否已经安装ntpdate
[root@wrx ~]# rpm -qa | grep ntpdate
#安装
[root@wrx ~]# yum install ntpdate
#查看是否已经安装成功
[root@wrx ~]# rpm -qa | grep ntpdate
ntpdate-4.2.6p5-10.el6.centos.2.i686
[root@wrx ~]#

3、ntpdate命令

#使用
[root@wrx ~]# ntpdate ntp1.aliyun.com #ntpdate + NTP服务器域名或者ip地址
#将ntpdate设置为开机启动
[root@wrx ~]# chkconfig --add ntpdate
[root@wrx ~]# chkconfig ntpdate on
[root@wrx ~]# chkconfig --list
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
blk-availability	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
htcacheclean   	0:off	1:off	2:off	3:off	4:off	5:off	6:off
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ip6tables      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iptables       	0:off	1:off	2:off	3:off	4:off	5:off	6:off
iscsi          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
iscsid         	0:off	1:off	2:off	3:on	4:on	5:on	6:off
lvm2-monitor   	0:off	1:on	2:on	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
memcached      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
multipathd     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ntpdate        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
#添加一个定时任务,每天21点时间同步一次
[root@wrx ~]# crontab -e
* 21 * * * /usr/sbin/ntpdate ntp1.aliyun.com
#查看定时任务
[root@wrx ~]# crontab -l
* 21 * * * /usr/sbin/ntpdate ntp1.aliyun.com