本文摘自PHP中文网,作者清浅,侵删。
Linux实现时间同步的方法的有两种,分别是:1、使用ntpdate从时间服务器来更新时间,不需要自己做任何配置;2、使用ntp来搭建自己的时间服务器。
【推荐课程:Linux系统】
由于硬件的原因,机器或多或少的跟标准时间对不上,误差几秒到几分钟不等。对于服务器来说时间不准会有很多麻烦。例如支付的时候,无法下单,游戏无法登录等。接下来将在文章中为大家分享Linux系统中如何实现时间同步,希望对大家有所帮助
方法一:用ntpdate从时间服务器更新时间
如果linux系统没有ntpdate这个命令,可以输入以下代码进行安装
安装完了之后,你不要做什么配置,也不需要,直接测试一下
1 2 3 4 5 | vi /etc/ntp.conf
add below:
server 1.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.org
|
restart service and sync the time
1 2 | [root@localhost ~]# ntpdate time.nist.gov
22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec
|
如果显示上面的内容说明同步成功了,然后在crontab里面加上以下内容。
1 | */10 * * * * ntpdate time.nist.gov #域名或IP
|
每隔十分钟同步一次。推荐几个时间服务器。
time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org
方法二、用ntp搭建自己的时间服务器
上面是利用别人的时间服务器来同步时间,这些时间服务器都是比较权威的。当我们自己搭建时间服务器就不用crontab来定时去跑。
1、安装时间服务器ntp
2、配置ntp
1 2 3 4 5 6 7 8 9 10 11 12 | [root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
restrict default ignore //默认不允许修改或者查询ntp,并且不接收特殊封包
restrict 127.0.0.1 //给于本机所有权限
restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify //给于局域网机的机器有同步时间的权限
server time.nist.gov prefer //设置时间服务器,加prefer表示优先
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
|
3、启动 ntp
阅读剩余部分
相关阅读 >>
Linux有哪些常用的版本?
Linux基础教程之Linux安装步骤详解
如何在Linux上为用户设置http代理
Linux如何查看防火墙状态
怎么查看Linux的硬盘
Linux中文件无法被root修改
Linux中vi查找关键字的方法
Linux怎么cd到桌面
ssh Linux是什么
在云服务器 ecs上如何删除一份自定义镜像
更多相关阅读请进入《Linux》频道 >>
转载请注明出处:木庄网络博客 » linux实现时间同步有哪几种方法