[Linux] How to modify the time zone of Linux and correctly synchronize the Beijing time of the Linux system


TZ It is the abbreviation of time zone and is an environment variable on the Linux system. This variable determines which time zone is used. This article describes how to correctly modify the time zone of the Linux system and synchronize the correct Beijing time.

1. Check the current time zone of Linux

You can use the following command to easily check the current time zone of your Linux system:

# 查看当前时间是否正确
date
# 查看当前的时区是否是北京
echo $TZ
Asia/Tokyo			# 如这个就是东京时区,很明显不是北京
# 或者用date -R查看是不是+0800
date -R
Wed, 28 Jun 2023 08:13:04 +0900		# 这个一看就不是北京时间,不是+0800

2. Get the value of the time zone environment variable TZ

Note: In fact, all operations in this step are to obtain the correct TZ value. If you have the correct value, you can directly configure it in the configuration file /etc/profile.

To change the Linux system time zone, you must first know the TZ value of your local time zone. You tzselectcan use the command to view the correct TZ value.

  • execute tzselectcommand
# 选择查找时区的字符串 TZ 值
tzselect

  • Select continent, Asia

  • Select country, China

  • Select time zone, Beijing

  • Finally, the TZ value that needs to be configured is:TZ='Asia/Shanghai'

3. Configure the value of the environment variable TZ

Each Linux system user will read /etc/profilethe file when logging in, so choose to add the TZ environment variable at the end of the file.

Note: If you know the correct TZ value, there is no need to obtain the TZ value.

# 配置TZ值
vim /etc/profile

Add the content " " in the last line of the configuration file TZ='Asia/Shanghai'; export TZ, as shown in the figure below:

4. Reload the configuration and verify whether it takes effect

The TZ value configured in the above steps only takes effect for a new login session. If we want this session to take effect, we need to reload the configuration file, as follows:

# 重新加载配置
source /etc/profile
# 检验时间是否正确
date
date -R
echo $TZ
# 如果时间还是不正确,有网络的可以同步一下北京时间
ntpdate ntp.aliyun.com

Portal: Nanny-style Spring5 source code analysis

Welcome to exchange technology and work life with the author

Contact the author

Guess you like

Origin blog.csdn.net/yuchangyuan5237/article/details/131428812