Linux 服務器修改時間與時間同步

設置時間

date --set '2015-11-23 0:10:40'  # 方法一,通用
timedatectl set-time '2015-11-23 08:10:40'  # 容器內可能不支持

設置時區

# 方法一,通用
# 對容器而言,也能夠直接將宿主機的下面倆文件映射到容器內
# 或者有的容器支持使用環境變量
/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone

# 方法二,容器內可能不支持
timedatectl set-timezone UTC

同步時間

Alpine Linux: ntpd -d -q -n -p ntp3.aliyun.com,當即同步時間,不限制同步間隔shell

CentOS/Ubuntu: sntp -S ntp3.aliyun.com,這句我還沒跑成功過。sntp 須要先安裝再使用服務器

國內公共 ntp 服務器:測試

  1. 國家授時中心:ntp.ntsc.ac.cn
  2. 阿里雲:ntp3.aliyun.com

至於爲啥要在容器內修改時間/同步時間,由於公司要測試有關時間的產品邏輯,要提供這方面的 API阿里雲

P.S. ntpdate 已經被標記爲 Deprecated,儘可能不要用它了code

相關文章
相關標籤/搜索