Skip to content
Go back

Set the Timezone in Ubuntu with timedatectl

· Updated:
By SumGuy 6 min read
Set the Timezone in Ubuntu with timedatectl

The timedatectl command is a powerful tool that can be used to view and change the system’s time and date settings in Ubuntu. It is a part of the systemd system and service manager, and it is a replacement for the old date command.

To use timedatectl, you need to be logged in as a root user or have sudo privileges. Once you are logged in, open a terminal window and type the following command:

timedatectl

This will display the current time and date settings for your system, including the timezone.

To list all of the available timezones, you can use the following command:

timedatectl list-timezones

You can use grep to find your timezone.

timedatectl list-timezones | grep -i <city name>

results for timedatectl list-timezones | grep -i paris:

Europe/Paris

To set the timezone, you can use the following command:

timedatectl set-timezone <timezone>

For example, to set the timezone to America/New York, you would use the following command:

timedatectl set-timezone America/New_York

Once you have set the timezone, you can use the following command to verify that the change has been made:

timedatectl

The output of this command should now show the new timezone. e.g. before:

Local time: Wed 2023-09-13 00:01:57 UTC
Universal time: Wed 2023-09-13 00:01:57 UTC
RTC time: Wed 2023-09-13 00:01:57
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

After:

Local time: Tue 2023-09-12 20:02:10 EDT
Universal time: Wed 2023-09-13 00:02:10 UTC
RTC time: Wed 2023-09-13 00:02:10
Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

In addition to setting the timezone, you can also use timedatectl to enable or disable NTP updates. NTP (Network Time Protocol) is a protocol used to synchronize the clocks of computers over a network. To enable NTP updates, you can use the following command:

timedatectl set-ntp true

To disable NTP updates, you can use the following command:

timedatectl set-ntp false

To verify that NTP updates are enabled or disabled, you can use the following command:

timedatectl status

The output of this command should show the status of NTP updates.

Here are some additional things to keep in mind when using timedatectl:

I hope this article has been helpful in explaining how to use timedatectl to set the timezone in Ubuntu and enable NTP updates. If you have any questions, please feel free to ask in the comments :).

What Actually Breaks When the Timezone Is Wrong

“Immediate effect” is mostly true — but there’s a graveyard of things that silently keep the old timezone in memory until you poke them. Here’s what bites people:

Cron jobs. cron reads the timezone at startup. If you set the timezone and don’t restart the cron daemon, your 0 9 * * * job still fires at 9 AM UTC — or whatever the old zone was. Fix it:

Terminal window
sudo systemctl restart cron

Docker containers. Containers inherit /etc/localtime from the host at start time. Running containers won’t notice the change. You need to restart them, or better yet, pass the timezone explicitly so you’re not relying on host inheritance at all:

Terminal window
docker run -e TZ=America/New_York your-image

Postgres and MySQL. Both cache timezone info when the service starts. Logs, NOW(), scheduled jobs — all potentially wrong until you restart the database service. This is the one that gets you at 2 AM when logs from “last night” appear to be from the future.

Your SSH session. If you’re logged in over SSH and you change the timezone, your current shell still has the old $TZ (or none) in its environment. Open a new session and it’ll pick up the system zone correctly.

The NTP Gotcha Nobody Warns You About

If timedatectl shows System clock synchronized: no and NTP service: active, the usual culprit is that systemd-timesyncd is fighting with another NTP client — chrony or ntpd — that got installed by something else. You can’t run two NTP daemons on the same host; they’ll both lose their minds.

Check what’s running:

Terminal window
systemctl status chronyd ntpd systemd-timesyncd 2>/dev/null | grep -E "Active:|●"

Pick one, disable the other. For most Ubuntu servers systemd-timesyncd is fine unless you need high-precision sync — in which case, use chrony and disable timesyncd:

Terminal window
sudo systemctl disable --now systemd-timesyncd
sudo apt install chrony
sudo systemctl enable --now chrony

Then timedatectl show-timesync will report chrony as the sync source and synchronized: yes should come back within a minute or two.

Scripting Timezone Changes Across a Fleet

If you’re doing this on more than one box, you don’t want to type it interactively. Here’s the one-liner that works in Bash, Ansible shell tasks, cloud-init, or wherever you need it:

Terminal window
sudo timedatectl set-timezone "$(curl -sf https://ipapi.co/timezone)"

That auto-detects the correct timezone from the server’s public IP — handy for freshly provisioned VMs where you know the geographic region but don’t want to hardcode it. Obviously don’t use this if the server is behind a VPN or the IP is misleading. And yes, check what data that endpoint returns before running it in prod. Trust but verify.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Self-Hosted Email Is Probably a Bad Idea
Next Post
Socat: The Swiss Army Knife of Networking

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts