更改時區
在給國外伙伴部署機器的時候,經常會碰到需要給Linux修改時區的情況:
這里例舉幾個方式:
1:覆蓋/etc/localtime文件內容
/usr/share/zoneinfo/ 下面都是對應的洲以及其對應的時區,如果我們需要修改時區為Shanghai的話,執行下面的操作:
[root@VM-75-60 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y
[root@VM-75-60 ~]#
檢測是否生效:
[root@VM-75-60 ~]# date -R
Fri, 24 Jul 2020 22:59:39 +0800
最后的參數‘+0800’就表示東八區!
2:tzselect指令
[root@VM-75-60 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
通過交互式,利用首位的數字去選擇你想要選擇的時區。
但這里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.
你看,他讓你編輯.profile,設置個TZ變量。。。
第一種方法直接cp覆蓋不香嗎?
注意:升級了glibc之后的機器,crond和rsyslog等程序的讀取時間都會變成新的glibc下的localtime,因此需要重新做軟連接,把/etc/localtime軟連接到新的glibc下的localtime