In this article, we’ll explain how you can set the time zone on an Ubuntu system running 16.04 version of the operating system.
Generally, we set the time zone for the computer during installation but it can be changed after the operating system has been installed.
We may use a GUI tool to modify the time zone but in this article, we’ll be using command line utilities to accomplish this task.
Check current time zone in Linux
Step 1: We’ll use the date command with the %z and %Z options to print only the time zone information.
root@linuxnix:~# date '+%z %Z' -0700 PDT
%z displays the time in numeric notation and %Z displays time zone in alphabetic notation.
Another way to check your time zone is to read the /etc/timezone file as shown here.
root@linuxnix:~# cat /etc/timezone America/Los_Angeles
So, it appears that our system is set to America/Los_Angeles timezone but since I’m in India, we’ll need to change this.
How to change timezone in Linux using tzselect/tzdata
Step 2: Historically one would use the tzconfig command to change the time zone but that has been deprecated now.
root@linuxnix:~# tzconfig WARNING: the tzconfig command is deprecated, please use: dpkg-reconfigure tzdata root@linuxnix:~#
Different Linux distributions have come up with different utilities to modify simplify the task of modifying the time zone.
In Ubuntu, we could use dpkg-reconfigure tzdata to update the time zone.
This launches a TUI menu which presents an easy to use interface to take us through the process and does most of the heavy lifting like copy and modifying files etc.
Given below is a screenshot of the TUI which launches via the dpkg-reconfigure tzdata command.
But we don’t use the menu. We will do it manually to see what you may understand the steps involved in the process.
Now, we’ll use the tzselect command which asks a couple of questions and based on our responses it tells us the time zone data file we need to use to modify the time zone to our required settings.
root@linuxnix:~# tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent, ocean, "coord", or "TZ". 1) Africa 2) Americas 3) Antarctica 4) Asia 5) Atlantic Ocean 6) Australia 7) Europe 8) Indian Ocean 9) Pacific Ocean 10) coord - I want to use geographical coordinates. 11) TZ - I want to specify the time zone using the Posix TZ format. #? 4 Please select a country whose clocks agree with yours. 1) Afghanistan 18) Israel 35) Palestine 2) Armenia 19) Japan 36) Philippines 3) Azerbaijan 20) Jordan 37) Qatar 4) Bahrain 21) Kazakhstan 38) Russia 5) Bangladesh 22) Korea (North) 39) Saudi Arabia 6) Bhutan 23) Korea (South) 40) Singapore 7) Brunei 24) Kuwait 41) Sri Lanka 8) Cambodia 25) Kyrgyzstan 42) Syria 9) China 26) Laos 43) Taiwan 10) Cyprus 27) Lebanon 44) Tajikistan 11) East Timor 28) Macau 45) Thailand 12) Georgia 29) Malaysia 46) Turkmenistan 13) Hong Kong 30) Mongolia 47) United Arab Emirates 14) India 31) Myanmar (Burma) 48) Uzbekistan 15) Indonesia 32) Nepal 49) Vietnam 16) Iran 33) Oman 50) Yemen 17) Iraq 34) Pakistan #? 14 The following information has been given: India Therefore TZ='Asia/Kolkata' will be used. Local time is now: Wed Nov 1 15:57:29 IST 2017. Universal Time is now: Wed Nov 1 10:27:29 UTC 2017. Is the above information OK? 1) Yes 2) No #? 1 You can make this change permanent for yourself by appending the line TZ='Asia/Kolkata'; 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/Kolkata
Based on my responses the tzselect command displays that I should use the Asia/Kolkata timezone data file to set my system time zone to IST.
The output also mentions that I could change the time zone for my own use instead of the entire system by using the TZ environment variable.
In the entity Asia/Kolkata, Asia is actually a directory and Kolkata is a file inside that directory.
The directory Asia is in folder /usr/share/zoneinfo/ along with time zone data files for other time zones.
root@linuxnix:~# ls /usr/share/zoneinfo/ Africa Asia Canada Cuba EST Factory GMT0 Hongkong Iran Japan localtime MST7MDT Pacific posixrules ROC Turkey UTC zone.tab America Atlantic CET EET EST5EDT GB GMT-0 HST iso3166.tab Kwajalein MET Navajo Poland PRC ROK UCT WET Zulu Antarctica Australia Chile Egypt Etc GB-Eire GMT+0 Iceland Israel leap-seconds.list Mexico NZ Portugal PST8PDT Singapore Universal W-SU Arctic Brazil CST6CDT Eire Europe GMT Greenwich Indian Jamaica Libya MST NZ-CHAT posix right SystemV US zone1970.tab
The file /usr/share/zoneinfo/Asia/Kolkata is actually a binary file which contains all the information required by the system to set the corresponding time zone.
root@linuxnix:~# ls -l /usr/share/zoneinfo/Asia/Kolkata lrwxrwxrwx 1 root root 8 Oct 16 06:07 /usr/share/zoneinfo/Asia/Kolkata -> Calcutta root@linuxnix:~# ls -l /usr/share/zoneinfo/Asia/Calcutta -rw-r--r-- 1 root root 291 Jul 5 2016 /usr/share/zoneinfo/Asia/Calcutta
As shown above the file Kolkata turned out to be a symbolic link to Calcutta.
Change the time zone through config files
To change the system time persistently we need to modify two files.
The first file is /etc/timezone.
The current /etc/timezone file on system has the entry America/Los_Angeles for the PDT time zone.
root@linuxnix:~# cat /etc/timezone America/Los_Angeles
We’ll modify that to Asia/Kolkata now.
root@linuxnix:~# cat /etc/timezone Asia/Kolkata
The second file we need to modify is /etc/localtime.
root@linuxnix:~# ls -l /etc/localtime lrwxrwxrwx 1 root root 39 Nov 1 03:24 /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles
Notice that the /etc/localtime file is soft linked to the binary time zone data file for the America/Los_Angeles timezone represented in alphabetic notation by PDT.
To modify this file, we’ll first remove the soft link with the unlink command and then create a new soft link to the required file.
root@linuxnix:~# unlink /etc/localtime root@linuxnix:~# root@linuxnix:~# ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime root@linuxnix:~# root@linuxnix:~# ls -l /etc/localtime lrwxrwxrwx 1 root root 32 Nov 1 16:14 /etc/localtime -> /usr/share/zoneinfo/Asia/Kolkata root@linuxnix:~# root@linuxnix:~# date Wed Nov 1 16:14:37 IST 2017
Notice that the change is instantaneous. The update will persist across reboots.
In this article, we explained how we could modify the time zone of our system and the files that would be modified in doing so.
In a typical networked production environment, the time zone information would be synced via NTP.
Latest posts by Sahil Suri (see all)
- Ansible install on RHEL 8 explained - August 22, 2019
- RHEL 8 server registration with Red Hat subscription Management - August 20, 2019
- RHEL 8 installation step by step with screenshots - August 19, 2019
- 3 ways to obtain per process swap utilization in Linux - August 16, 2019
- Docker engine explained - August 15, 2019