Linux服务器同步Intetnet时间

时间:2023-03-10 05:16:33
Linux服务器同步Intetnet时间

Linux服务器同步Intetnet时间

在使用linux服务器(虚拟机)的过程中,经常会发现使用一段时间后,linux服务器的时间和我的宿主机的时间不一致,而宿主机的时间确实是internet时间,安装linux时选择的时区也是Asia/Shanghai,下面说说虚拟机和Internet时间同步的方法。

1、先查看当前服务器(linux系统)时间:

[root@heyong ~]# date
Thu Jan 17 03:24:35 CST

2.hwclock命令   (即hardwareclock系统硬件时间)

[root@heyong ~]# hwclock
Thu Jan :: PM CST -0.332157 seconds

将系统时间写入到系统硬件当中

[root@heyong ~]# hwclock -w

3.ntpdate

ntpdate 是一个linux时间同步服务软件,查看本机是否安装ntpdate服务,如果没有安装,请 yum install -y ntpdate;

同步时间:

[root@heyong ~] ntpdate time.nist.gov

或者

[root@heyong ~] ntpdate ntp1.aliyun.com

执行:ntpdate ntp1.aliyun.com

[root@heyong ~]# ntpdate ntp1.aliyun.com
Jan :: ntpdate[]: step time server 120.25.115.20 offset 31.542281 sec

出现上述结果代表时间同步成功,上面的大致意思为调整时间为服务器120.25.115.20的时间,相差31.542281秒的时间

如果上面time.nist.gov服务器同步不了,可以换下面几个时间服务器试试:

time.nist.gov
time.nuri.net
.asia.pool.ntp.org
.asia.pool.ntp.org
.asia.pool.ntp.org
.asia.pool.ntp.org

4.定时执行时间同步任务

[root@kvm01 ~]# crontab -e

* */ * * * root ntpdatetime.nuri.net;hwclock -w 

即:每隔一个小时同步一下internet时间。