使用chrony修改时区并同步时间

快速配置命令:

CentOS7/8:

date -R
yum install -y chrony
sed -i '/server 0.centos.pool.ntp.org iburst/i\server cn.ntp.org.cn iburst\' /etc/chrony.conf
sed -i '/server 0.centos.pool.ntp.org iburst/i\server ntp.aliyun.com iburst\' /etc/chrony.conf
sed -i '/server 0.centos.pool.ntp.org iburst/i\#server 0.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 1.centos.pool.ntp.org iburst/i\#server 1.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 2.centos.pool.ntp.org iburst/i\#server 2.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 3.centos.pool.ntp.org iburst/i\#server 3.centos.pool.ntp.org iburst\' /etc/chrony.conf
systemctl start chronyd && systemctl enable chronyd
echo "TZ='Asia/Shanghai'" >> /etc/profile && source /etc/profile
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
chronyc makestep
date -R

 CentOS6:

date -R
yum install -y chrony
sed -i '/server 0.centos.pool.ntp.org iburst/i\server cn.ntp.org.cn iburst\' /etc/chrony.conf
sed -i '/server 0.centos.pool.ntp.org iburst/i\server ntp.aliyun.com iburst\' /etc/chrony.conf
sed -i '/server 0.centos.pool.ntp.org iburst/i\#server 0.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 1.centos.pool.ntp.org iburst/i\#server 1.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 2.centos.pool.ntp.org iburst/i\#server 2.centos.pool.ntp.org iburst\' /etc/chrony.conf
sed -i '/server 3.centos.pool.ntp.org iburst/i\#server 3.centos.pool.ntp.org iburst\' /etc/chrony.conf
/etc/init.d/chronyd start  &&  chkconfig –add chronyd
echo "TZ='Asia/Shanghai'" >> /etc/profile && source /etc/profile
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
chronyc makestep
date -R

配置参数详细说明:

推荐使用chrony程序同步时间原因,ntpdate不支持CentOS8版本,而chrony支持CentOS6、7、8版本。

Chrony由两个程序组成:

  • chronyc – chrony的命令行界面
  • chronyd –可以在启动时启动的守护程序

在Linux中安装Chrony

#[在 CentOS/RHEL 上]
 yum -y install chrony 
#[在 Debian/Ubuntu 上]
 apt install chrony 
#[在 Fedora 22+ 上]
 dnf -y install chrony 

检查chronyd的状态

#[在SystemD上 ]
 systemctl status chronyd 
#[在Init上 ]
 /etc/init.d/chronyd status 

设置chrony开机自启

#[在SystemD上 ]
 systemctl enable chronyd 
#[在Init上 ]
 chkconfig –add chronyd

修改更换NTP服务器:

使用#注释掉以下内容:

#server 0.centos.pool.ntp.org iburst 
#server 1.centos.pool.ntp.org iburst 
#server 2.centos.pool.ntp.org iburst 
#server 3.centos.pool.ntp.org iburst

在注释掉后面添加新的NTP服务器:(可自行选择授时中心,包含但不限于以下公网提供授时中心)

#中科院授时中心(延时最小)
server ntp.ntsc.ac.cn iburst 
# 中国授时中心
server cn.ntp.org.cn  
# 国际通用服务器
server cn.pool.ntp.org
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
# 阿里 ntp1到7
server ntp.aliyun.com
# 腾讯 ntp1到5或者time1到5
server ntp.tencent.com
server time.cloud.tencent.com
# 苹果
server time.asia.apple.com
# 微软
server time.windows.com

更多设置内容参考:

阿里 https://help.aliyun.com/document_detail/92704.html

腾讯 https://cloud.tencent.com/document/product/213/30392

其他:https://dns.icoa.cn/ntp/

常用设置命令

#检查时间同步
chronyc tracking
#检查chrony来源的信息
chronyc sources
#立即步进系统时钟,而忽略当前正在进行的任何调整
chronyc makestep
#停止chrony
#[在SystemD上 ]
 systemctl stop chrony 
#[在 Init上]
 /etc/init.d/chronyd stop 

时区设置

对于部署在海外的Linux服务器来说,拿到的机器时区和本地并不一样,导致运行在上面的应用也面临时区问题。所以有必要修改系统本地时区。

查看时区

# date -R
Tue, 17 Jan 2017 21:36:23 +0800
最后的+0800,即东8区。

设置时区

1、然并卵用的tzselect命令

看起来很像一个时区选择的工具,但并非如此。事实上tzselect仅仅是一个查看时区表示方式的『向导』程序而已。通过依次询问大洲→国家→城市,最后告诉你如何TZ变量的写法,比如北京时间是:Asia/Shanghai

2、TZ变量

可以通过修改TZ变量,直接修改时区信息,

#临时修改
 export  TZ='Asia/Shanghai'
#永久修改
echo "export  TZ='Asia/Shanghai'" >> /etc/profile
source /etc/profile

3、修改/etc/localtime文件

默认情况下情况下,TZ属性是空,这时候是靠/etc/localtime文件来确定的时区。而此文件通常又是一个到/usr/share/zoneinfo/下各种时区文件的软连接。通过修改/etc/localtime指向的软连接,进而修改系统的时区。比如下面的方法,将localtime文件设置为了北京时间

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

时区修改总结:

  1. tzselect命令无法修改时区,仅给出时区的城市表示法
  2. TZ变量和/etc/localtime文件会影响时区,并建议直接修改/etc/localtime文件。
  3. 如果在shell中临时需要变更时区信息,可以修改TZ变量实现。
  4. 在profile文件里设置变量TZ,达到和修改/etc/localtime类似的效果。

参考文章:https://www.linuxmi.com/linux-chrony.html

版权声明:
作者:WaterBear
链接:https://l-t.top/2203.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录