Linux 查看和修改时区
查看时区
date -R # Thu, 20 Jan 2022 10:20:28 +0800
末尾的 +0800 表示时区
查看 date 命令手册:
man date
date 格式化输出:
date +"%Z %z" # CST +0800
%Z 表示字符形式的时区缩写,%z 表示数字形式的时区
设置时区
使用 tzselect 命令选择时区。
tzselect
输入命令后根据提示选择地理区域
选择结束确认后出现如下提示:
You can make this change permanent for yourself by appending the line
	TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
注意: 根据提示得知,tzselect 并不会永久性修改时区。如果想要永久性修改时区可以按照提示修改环境变量 TZ。
vi ~/.profile # 追加如下行 TZ='Asia/Shanghai'; export TZ
/etc/localtime
环境变量 TZ 默认为空,此时系统默认使用 /etc/localtime 文件来确认时区。而 /etc/localtime 是指向 /usr/share/zoneinfo/ 文件夹下时区文件的软链接
~ ll /etc/localtime 
lrwxrwxrwx 1 root root 33 May 27  2020 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
可通过修改 /etc/localtime 的软链地址来设置时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime