良许Linux教程网 干货合集 Linux时间与internet时间同步

Linux时间与internet时间同步

在使用linux时(虚拟机),经常会发现使用一段时间后,linux时间和我的宿主机(真实机)的时间不一致,而宿主机的时间确实是internet时间,安装linux时选择的时区也是Asia/Shanghai,那么今天我分享的即为如何让linux时间与internet时间同步。

Linux时间与internet时间同步

一、安装ntp

[root@server-2 ~]# yum install -y ntpdate

二、同步时间

// 方式一、使用域名连接,要经过DNS解析,速度慢。
[root@server-2 ~]# ntpdate pool.ntp.org
// 方式二、使用IP连接,超级快。
[root@server-2 ~]# ntpdate 120.24.81.91

http://www.pool.ntp.org是NTP的官方网站,在这上面我们可以找到离我们国家的NTP Server cn.pool.ntp.org.它有3个服务器地址: 服务器一: 1.cn.pool.ntp.org 服务器二: 2.asia.pool.ntp.org 服务器三: 3.asia.pool.ntp.org (直接用域名有时有问题,可以先Ping出他们的IP,然后用IP地址同步)

// 出现以下信息说明成功

Feb 21:23:06 ntpdate[62910]: step time server 182.92.12.11 offset -40.589470 sec

三、将系统时间写入到系统硬件当中,避免重启服务器时间覆盖

// 显示hardwareclock系统硬件时间

[root@server-2 ~]# hwclock

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

[root@server-2 ~]# hwclock -w

四、设定计划任务同步网络时间

crontab格式如下:

# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *  command to be executed

设定crontab计划任务有两种方式:

1、写在/etc/crontab里

代码:

11 * * * root ntpdate 210.72.145.44

每天11点与中国国家授时中心同步时间 每天11点与中国国家授时中心同步时间 当然前提是

apt-get install ntpdate

代码也可是

11 * * * root ntpdate us.pool.ntp.org

2、使用命令crontab -e

crontab -e
   10 5 * * * root ntpdate us.pool.ntp.org;hwclock -w

这样每天5:10自动进行网络校时,并同时更新BIOS的时间

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部