购买的vps时间和本地的对不上.这是一件非常凡人的小事,在以往的操作中往往需要复杂的操作来修改时区,但是在最新的操作系统中提供了内置的一键修改时区的方法,本站会以传统方案以及新方案两个角度来简述如何修改时区

传统方案

首先使用下面这个命令来选择目标时区

tzselect

以中国为例,应该选择的分支为Asia/Shanghai

接着直接替换系统的时间文件

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
  • 传统的方式到这里就为止了

新方案

  • timedatectl是目前控制时间的新服务

首先使用如下命令查看当前系统支持的时间分区

timedatectl list-timezones

以中国为例,应该选择的分支为Asia/Shanghai

接下来直接使用timedatectl来设置中国分区时间

timedatectl set-timezone Asia/Shanghai

同步时间

  • 上面只是介绍了一些修改时区的方法
  • 很多时候(特别是虚拟机)正确时区和正确的时间之间不是相关的,还有很多因素干扰
  • 这里介绍几个可能会用到的指令

启用ntp同步时间

ntpdate time.windows.com
#备用服务器:time.nist.gov

将时间写入硬件

hwclock -w

The time of the purchased vps and the local one can not be correct. This is a very mundane and trivial thing, in the past operation often requires complex operations to modify the time zone, but in the latest operating system provides a built-in one-click method to modify the time zone, this site will be the traditional program and the new program to briefly explain how to modify the time zone from two perspectives

Traditional solution

First, use the following command to select the target time zone

tzselect

In the case of China, the branch that should be selected is `Asia/Shanghai''

Then replace the system time file directly

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  • This is the end of the traditional approach

New solution

  • timedatectl is the new service that currently controls time

First use the following command to see which time zones are currently supported on your system

timedatectl list-timezones

Using China as an example, the branch you should select is Asia/Shanghai.

Next, use timedatectl to set the time of the China branch directly

timedatectl set-timezone Asia/Shanghai

Synchronize the time

  • The above just describes some ways to modify the time zone
  • Many times (especially for virtual machines) the correct time zone and the correct time are not correlated, and many factors interfere
  • Here are a few commands that may be used

Enable ntp synchronization time

ntpdate time.windows.com
# Alternate server: time.nist.gov

Write time to hardware

hwclock -w
Last modification:February 5, 2021
如果觉得我的文章对你有用,请随意赞赏