很多人在进行时间的同步时,仅仅只是用ntpdate修改时间,但这并不是真正的修改了服务器的时间,只是修改了当前date看到的时间,也就是系统时间。在Linux服务器中,掌管时间的一共有三个,分别是时区、系统时间、硬件时间(CMOS)。

同步时间,应该将这三者都进行正确的修改

时区

查看系统所在时区

$ date -R
Sun, 07 Mar 2021 08:20:34 -0500
# 这里的-0500就是所在时区,在网上一查就知道这个是纽约时区,而北京应该是+0800

修改时区

$ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
$ date -R
Sun, 07 Mar 2021 21:24:16 +0800

系统对时

网络对时

ntpdate ntp.ntsc.ac.cn
# ntp.ntsc.ac.cn是国家授时服务器地址
# 或者使用阿里云的时间服务器ntp1.aliyun.com
# 如果公司有ntp服务器,使用ntp服务器的ip也可以

有的因为网络限制不能进行网络对时,我们可以手动去修改时间

手动对时

方法一

timedatectl set-time "xxxx-xx-xx 00:00:00"
# 也可以只修改年月日,或者只修改时间
timedatectl set-time "xxxx-xx-xx"
timedatectl set-time "00:00:00"

如果手动修改时间报错Automatic time synchronization is enabled,是因为自动同步时间的开启,关闭即可,关闭命令timedatectl set-ntp no,再次开启timedatectl set-ntp yes

方法二

date -s "xxxx-xx-xx 00:00:00"
date -s "xxxx-xx-xx"
date -s "00:00:00"

硬件时间

将修改好的系统时间同步到硬件上

硬件时间是12小时制,通过AM(上午)和PM(下午/晚上)来区分的

hwclock --systohc

评论




正在载入...
PoweredHexo
HostedAliyun
DNSAliyun
ThemeVolantis
UV
PV
BY-NC-SA 4.0