On Ubuntu, the system timezone is set during the installation process, but can be changed later.
在Ubuntu上,系统时区是在安装过程中设置的,但以后可以更改。
Configuring the appropriate timezone is important for a lot of us, so let us find out how we can change the timezone on our Ubuntu 18.04 system.
对我们中的大多数人而言,配置适当的时区很重要,因此让我们了解如何在Ubuntu 18.04系统上更改时区。
We can use the timedatectl
command to display and set the current system’s time and timezone.
我们可以使用timedatectl
命令来显示和设置当前系统的时间和时区。
timedatectl
As you can observe, here, the timezone is currently set to UTC.
如您所见,此处的时区当前设置为UTC 。
Let’s get this changed to IST (GST +5:30) using a few steps!
让我们通过几个步骤将其更改为IST (GST +5:30)!
We need to find out the format for the timezone you want to use. The timezone naming convention usually uses a “Region/City” format.
我们需要找出您要使用的时区的格式。 时区命名约定通常使用“ Region / City ”格式。
To list all available time zones, we can use timedatectl
along with the list-timezones option.
要列出所有可用时区,我们可以使用timedatectl
以及list-timezones选项。
timedatectl list-timezones
We will get an output like this, showing the long name format for all available timezones.
我们将得到这样的输出,显示所有可用时区的长名称格式。
Let us find out the required format for our necessary timezone, which we want to find; IST, in my case. It is in the Kolkata zone, so let’s grep for Kolkata.
让我们找出我们想要的时区所需的格式; IST ,就我而言。 它是在加尔各答区,让我们对加尔各答的grep。
timedatectl list-timezones | grep Kolkata
Now we have identified our timezone format. We just need to set it now!
现在,我们确定了时区格式。 我们只需要立即设置!
Use the below command to change the timezones, as root.
使用以下命令以root身份更改时区。
sudo timedatectl set-timezone your_time_zone
In my case, it is Asia/Kolkata, so the command will be
在我的情况下,它是Asia / Kolkata ,所以命令将是
sudo timedatectl set-timezone Asia/Kolkata
Alternatively, we can also directly set the timezone to its abbreviated format (IST, in my case)
另外,我们也可以将时区直接设置为缩写格式(在我的情况下为IST )
sudo timedatectl set-timezone IST
If this is successful, you will see the system time in terms of your timezone, and the timezone has been successfully updated!
如果成功,则将以时区显示系统时间,并且时区已成功更新!
But even if the above method didn’t work, we have some alternatives. So if you’ve configured timezone successfully using timedatectl
, the rest of the article is not required. But otherwise, let’s keep reading.
但是,即使上述方法无效,我们也有其他选择。 因此,如果您已经成功地使用timedatectl
配置了时区,则本文的其余部分不是必需的。 但是,否则,让我们继续阅读。
Suppose the above method didn’t work, we can use dpkg
to reconfigure our timezone data and set the desired timezone.
假设上述方法无效,我们可以使用dpkg
重新配置时区数据并设置所需的时区。
sudo dpkg-reconfigure tzdata
We will now get a menu that allows us to reconfiguring our timezone.
现在,我们将获得一个菜单,该菜单使我们可以重新配置时区。
Now, you can use the graphical menu to successfully update your timezone!
现在,您可以使用图形菜单成功更新您的时区!
Another way of changing the timezone would be to edit the localtime
file manually.
更改时区的另一种方法是手动编辑localtime
文件。
Information about the timezone is located at /etc/localtime
. We can manually modify that file to our desired timezone, which is located at /usr/share/zoneinfo/
.
有关时区的信息位于/etc/localtime
。 我们可以将该文件手动修改为所需的时区,该时区位于/usr/share/zoneinfo/
。
The zoneinfo directory contains a list of Regions of interest. Select your region. (For IST, the region is Asia)
zoneinfo目录包含感兴趣的区域列表。 选择您的地区。 (对于IST,该地区是亚洲 )
Now, simply copy the Kolkata timezone file to /etc/localtime
.
现在,只需将加尔各答时区文件复制到/etc/localtime
。
sudo cp /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
Restart the machine to apply your changes.
重新启动机器以应用您的更改。
With this, we have now updated our timezone successfully!
这样,我们现在已经成功更新了时区!
We can also directly perform a symbolic link between the details of the Timezone file and /etc/localtime
, located at /usr/share/zoneinfo/TIMEZONE
, where TIMEZONE is your required timezone. In my case, it is IST.
我们还可以直接在位于/usr/share/zoneinfo/TIMEZONE
的Timezone文件的详细信息和/etc/localtime
之间执行符号链接,其中TIMEZONE是您所需的时区。 就我而言,它是IST 。
sudo ln -sf /usr/share/zoneinfo/IST /etc/localtime
Again, restart your machine to apply the changes.
再次,重新启动计算机以应用更改。
Now, we have shown you different methods for updating your timezone!
现在,我们向您展示了更新时区的不同方法!
In this tutorial, we showed how you can change the timezone of your Ubuntu 18.04 system using various methods.
在本教程中,我们展示了如何使用各种方法来更改Ubuntu 18.04系统的时区。
While we have shown different approaches, usually, the recommended method depends on your background:
尽管我们展示了不同的方法,但是通常推荐的方法取决于您的背景:
dpkg
, since it is user-friendly. 如果您是Windows的Ubuntu新用户,建议您使用dpkg
来更改时区的GUI模式,因为它是用户友好的。 /etc/localtime
. 另一方面,您以前有使用Linux的经验,但是没有使用过Ubuntu,可以手动更改/etc/localtime
。 timedatectl
is your best bet! 否则,如果您是Ubuntu的日常用户,那么timedatectl
是您的最佳选择! Hope you found your answer by reading this tutorial!
希望您通过阅读本教程找到答案!
翻译自: https://www.journaldev.com/33989/change-timezone-in-ubuntu