When you run a server in the cloud, there's one thing that might catch you off guard: The timezone!
Imagine you inspect who logged in through SSH today. And you see this:

Note the time: 17:20 (and 10 seconds). But your own clock shows it's 13:20. So it's like this log event happened in the future!
Of course, it's nothing that magical. It's just that the timezone of the Ubuntu server is "ahead" of your own, local timezone. When it's 13:20 for you, it's 17:20 on that server. Which can be quite confusing when you try to make sense of logs. And see what happened, and at what time.
If you leave the timezone as it is, you will constantly have to adjust in your head what something like 10:54 on the server means in your own timezone. So a good habit to have, when you create servers in the cloud, is to set the timezone on the server to the same timezone you're in (local timezone). At least, if you're the only one managing those servers.
How to Find the Correct Timezone for the timedatectl Command
First, check the current timezone set on the Ubuntu machine:
timedatectlYou'll see output like this:

Running this first is a good way to get a reminder on the syntax of timezones to pass to the timedatectl command. In this case, the timezone is set to Europe/Bucharest. Most timezones work this way, in a form like:
Continent_Name/Capital_CityBut, of course, there are exceptions. For example, Australia has just one capital city, but multiple timezones.
So what names do you pass to the timedatectl command if you live in a place which has multiple timezones? Let's say you're in the Western part of Australia.
Well, here's one solution. You can take a look at this timezones map, and check what timezone you're in.

You'll notice a city called Perth in the Western area.
So now you can tell timedatectl to list the timezones it has available:
timedatectl list-timezonesAnd then use the PAGE UP and PAGE DOWN keys to scroll around until you see the city that represents your own timezone:

Press q to exit from this list.
A faster way to look for your timezone is to tell timedatectl to list the zones, and then send its output to the grep command. Which, in turn, will filter out only content that matches with Perth in this case.
timedatectl list-timezones | grep PerthReplace Perth with the city that applies to you. If you're in a small country, just pass the name of your capital city.

So now you get the timezone you need, Australia/Perth, in one single line. This is what you'll use when changing the timezone with a timedatectl set-timezone command.
Perth, not perth, as searches in Linux are case sensitive. Looking for perth, with a lowercase p would return zero results.If, for some reason, you cannot find the correct city in that map, then you can simply google something like "australia timezones," or whatever area you live in. You'll get a list like this:

And you can see the timezone locations you can use in the timedatectl command. So if you'd need to use something like Christmas Island from this list, then you'd pass it to the same timedatectl list-timezones | grep command discussed earlier.
But this is another interesting case. You have this location with a name made out of two words: Christmas Island. Well, in the grep command you should pass just the first word of that name. So just Christmas:
timedatectl list-timezones | grep Christmas
Notice how the label for this timezone is Indian/Christmas. So two things are interesting here:
- If you'd look for
Christmas Islandyou wouldn't find that timezone. As intimedatectlit's justChristmas. - It's not grouped under
Australia. From the previous examples you'd expect something likeAustralia/Christmashere, since that island belongs to them. But no, the timezone is labeled under Indian Ocean, not Australia. So it'sIndian/Christmasinstead ofAustralia/Christmas.
There are a few potential gotchas like that, depending on where you live. But for most cases, setting the timezone will be simple. And with the steps above, you should be able to identify any timezone.
How to Change the Timezone on Ubuntu
Once you have the correct timezone you want to pass to the timedatectl command, you're good to go.
To change the timezone on Ubuntu, run this command, replacing Australia/Perth with the timezone that applies to you:
sudo timedatectl set-timezone Australia/PerthNext, check if the timezone has been changed correctly:
timedatectl
The current time on your server (used in logs, and other such events) is what you see in the first line of text, Local time:.
Learn More
If you want to learn even more Linux commands that are useful in system administration, check out this learning path:

Thank you for reading this blog, and see you in the next one!

Discussion