When we install any Linux distribution, we can provide a timezone & time will be served based on that timezone. But we can also change the timezone once we have installed the operating system. In this tutorial, we are going to discuss how to change the timezone in Linux using command line.
Recommended Read: Introduction to Bash Scripting Tutorial
Also Read: Important PostgreSQL commands that every beginner should know
There are different methods to change timezone in Linux based Linux distribution being used. Let’s start the tutorial with CentOS/RHEL.
Change timezone in Linux (CentOS/RHEL)
Timezone in CentOS, Redhat & other RHEL based OS like amazon Linux, Oracle Linux, Scientific Linux is maintained by the file ‘/etc/localtime’. Files for all timezones are stored at location ‘/usr/share/zoneinfo/’.
So in order to change timezone in Linux, we first need to remove the file ‘/etc/localtime’ & create a soft link to the timezone file in ‘/usr/share/zoneinfo/’. To see the current timezone file associated with /etc/localtime, use the following command,
# ls -l /etc/localtime
Next, we can check all the timezone available to use,
# ls /usr/share/zoneinfo/
Let’s check all the timezones that are available in Asia,
# ls /usr/share/zoneinfo/Asia
Of these mentioned timezones, we will be switching from Jakarta to Kolkata. So we will start by removing the current localtime file,
# rm /etc/localtime
Next, we need to create a soft link to required time zone i.e. Asia/Kolkata,
# ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
That’s it, we can now check the time on the timezone using ‘date’ command.
Change timezone in Linux (Ubuntu/Debian)
For Debian & Ubuntu-based Linux system, the process is a bit different than the RHEL based systems. For Ubuntu & Debian, timezone is controlled by the file ‘/etc/timezone’. To check the current timezone, we can use the following command,
# cat /etc/timezone
Asia/Jakarta
Now to change the timezone we have to edit the file & mention the required timezone,
# vi /etc/timezone
Asia/Kolkata
That’s it, our timezone will be updated. We can also set the timezone using the environment variable named ‘TZ’,
# export TZ=Asia/Kolkata
We now end this tutorial on how to change timezone in Linux. Please do send us your suggestions, queries, questions using the comment box below.