How to Change Timezone in Ubuntu Command Line
Your remote Ubuntu server uses UTC and you have a difficult time figuring out the time difference? Change the timezone and live in peace.
A timezone is a set standard time followed along a particular region. Setting the correct timezone can be crucial for your sanity. "Why is this 07:00 cron job running at 01:30?"
The easiest way to change timezone in Ubuntu is using the timedatectl
command.
The syntax is as given below:
sudo timedatectl set-timezone TIMEZONE
Running this command with a provided timezone will change the local time of your computer.
Sounds good? Let's see it in detail.
Check the current timezone
Before you change your timezone willy-nilly, you should check if you are already in the correct timezone or not.
To do so, simply tun the timedatectl
command:
$ timedatectl
Local time: Tue 2022-03-01 18:00:00 UTC
Universal time: Tue 2022-03-01 18:00:00 UTC
RTC time: n/a
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
The line to focus on is the one that has 'Local time' in it. That is the time that your system follows. For example, the local time is shown when you run the date
command, it is also used for scheduling your (and every other user's) cron jobs.
As you can see, my computer's local time follows the 'Etc/UTC' timezone. The UTC timezone is essentially Greenwich Mean Time.
Show all available timezones
If you wish to change the timezone, you must know what are the available options.
To get a list of all available timezones, run the following command:
timedatectl list-timezones
This will present you with a scrollable list.
Once you find the most appropriate timezone, make a note of it.
Since I am living in India, I will use the IST timezone – which is 'Asia/Kolkata'.
Method 1: Use timedatectl command to change timezone (recommended)
The timezone can be changed using the timedatectl
command.
Below is the syntax for changing the timezone:
sudo timedatectl set-timezone TIMEZONE
Replace TIMEZONE with the timezone you made a note of earlier.
For me, it was 'Asia/Kolkata'. So let us try changing the timezone to it.
$ sudo timedatectl set-timezone Asia/Kolkata
$ timedatectl
Local time: Tue 2022-03-01 23:30:00 IST
Universal time: Tue 2022-03-01 18:00:00 UTC
RTC time: n/a
Time zone: Asia/Kolkata (IST, +0530)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
You can see now, my local time changed from UTC to IST.
Method 2: Make a symbolic link to change timezone (not recommended)
Take a look at the contents of the directory /usr/share/zoneinfo
. If you look closely, you will notice that there are directories which are named after all the continents.
There are 'Africa', 'America', 'Asia', 'Europe' and more. If you look into the contents of these directories, you will see the actual timezones.
For example, if I look at the contents of /usr/share/zoneinfo/Asia
, I will see the cities of each different timezone. There is Kolkata (IST), Bangkok (ICT), Hong Kong (HKT), Kathmandu (NPT), Taipei (CST) and more.
Select the closest/most appropriate city that follows your timezone and create a symbolic link to /etc/localtime
.
Below is an example of setting 'Asia/Kolkata' as my timezone using a symbolic link:
$ sudo ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
$ date
Tue 01 Mar 23:30:00 IST 2022
Ah, perfect! My computer now has the correct timezone.
You just learned to change timezone on Ubuntu using the terminal. Enjoy :)