Linux 时间同步

作者: 舌尖上的大胖 | 来源:发表于2019-07-30 02:27 被阅读0次

Linux 服务器可以通过命令来进行时间同步。

一、ntpdate 同步网络时间

# 安装 ntpdate
$ sudo yum install -y ntpdate

# 执行时间同步
$ sudo ntpdate -u ntp.api.bz

NTP 常用服务器:

210.72.145.44 ................ 中国国家授时中心
ntp.api.bz ................... NTP服务器(上海) 
time.nist.gov  ............... 美国
ntp.fudan.edu.cn  ............ 复旦
time.windows.com  ............ 微软公司授时主机(美国) 
asia.pool.ntp.org ............ 台警大授时中心(台湾)

经测试中国国家授时中心与 NTP 上海服务器可以正常同步时间,注意需要加上 -u 参数。

注意:若不加上 -u 参数,会提示:no server suitable for synchronization found;
-u:从 man ntpdate 中可以看出 -u 参数可以越过防火墙与主机同步。

二、通过 Web 服务器同步

通过 HTTP 的 HEAD 方法访问 Web 服务器可以获取到头信息:

$ curl -I baidu.com

返回结果:

HTTP/1.1 200 OK
Date: Mon, 29 Jul 2019 17:37:34 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Tue, 30 Jul 2019 17:37:34 GMT
Connection: Keep-Alive
Content-Type: text/html

其中,cURL 可以:

  • curl -x $proxy 指定代理服务器
  • curl -H 'Cache-Control: no-cache' 避免缓存

可以通过解析 Date 字段值来设置时间。由于 Date 字段值是 GMT 时间,所以需要按 UTC 时间处理。

(一)显示时间

1、根据当前区域设置显示时间,后面传入的时间需要以最后的 Z 来区分传入的时间是否为 UTC 时间:

$ date -d "$(curl -H 'Cache-Control: no-cache' -sI baidu.com | grep '^Date:' | cut -d' ' -f3-6)Z"

显示为 CST 时间:

2019年 07月 30日 星期二 08:00:00 CST

2、指定显示 UTC 时间,后面传入的时间不管最后是否带有 Z 都被理解为 UTC 时间

$ date -ud "$(curl -H 'Cache-Control: no-cache' -sI baidu.com | grep '^Date:' | cut -d' ' -f3-6)"

显示为 UTC 时间:

2019年 07月 29日 星期一 18:14:36 UTC

(二)设置时间

1、在时间后跟 Z 表示传入的时间按 UTC 时间处理

$ sudo date -s "$(curl -H 'Cache-Control: no-cache' -sI baidu.com | grep '^Date:' | cut -d' ' -f3-6)Z"

设置后回显 CST 时间

2019年 07月 30日 星期二 02:19:16 CST

2、通过 -u 指定为 UTC 时间,是否加 Z 都可以

$ sudo date -us "$(curl -H 'Cache-Control: no-cache' -sI baidu.com | grep '^Date:' | cut -d' ' -f3-6)"

设置后回显 UTC 时间

2019年 07月 29日 星期一 18:19:53 UTC

参数说明:

-s, --set=STRING
       set time described by STRING

-u, --utc, --universal
       print or set Coordinated Universal Time (UTC)

三、手工同步

1、使用 date 命令查看当前时间

结果如下:

Tue Jul 30 01:20:43 CST 2019

2、使用 date -s 设置当前时间

$ sudo date -s '16:30:00'

结果如下:

Wed Feb 4 16:30:00 CST 2015

3、使用 date -s "YYYY-MM-DD hh:mm[:ss]"

如:

$ date -s "2015-02-04 16:30:00"
# 将时间写入 BIOS 避免重启失效
$ hwclock -w

四、参考资料

(完)

相关文章

  • linux ntp 同步时间

    文章来自:linux ntp 同步时间,linux ntp时间同步的两种方法[https://blog.csdn....

  • linux时间同步

    时间同步法二: 网址 反应时间 服务器地址

  • linux时间同步

    安装ntp 同步时间 写入硬件

  • linux 同步时间

    CentOS系统时间同步的步骤如下: 1、下载ntpdate注:有些版本是没有自带ntpdate,因此需要下载 y...

  • Linux时间同步

    转载自(http://www.cnblogs.com/zhi-leaf/p/6281549.html)1、安装nt...

  • 同步Linux时间

    有时候, 因为某些原因, 我们设置错了时区或时间, 或者系统当前时间不正确的时候, 我们该如何做? 修改系统时区(...

  • Linux时间同步

    原创作品,允许转载,转载时请务必以超链接形式标明原始出处、作者信息和本声明,否则后果自负。如果你觉得这篇文章对你有...

  • Linux 时间同步

    Linux 服务器可以通过命令来进行时间同步。 一、ntpdate 同步网络时间 NTP 常用服务器: 经测试中国...

  • Linux时间同步

    查看时间date 简单设置时间date -s 00:00:01 查看ntp的安装包:yum list|grep n...

  • Linux时间同步

    发现科学上网不能用,看一下系统时间错了,更新了下时间就能上网了。

网友评论

    本文标题:Linux 时间同步

    本文链接:https://www.haomeiwen.com/subject/oivwrctx.html