30

Dreamhost seems to think that we like PDT, despite being in the Midwest. How do I change the timezone?

4 Answers 4

78

A tool I found to be easier to use is dpkg-reconfigure. Use:

sudo dpkg-reconfigure tzdata

You will be given a multitude of choices, including some that are not included in tzselect.

2
  • A benefit of doing it this way is that you are doing it in a reproduceable way and limit the chances for doing something wrong like putting in a timezone that does not exist.
    – Mogget
    Commented Apr 21, 2013 at 17:56
  • 1
    This is good stuff, but if you need a non-interactive (and therefore scriptable) option, see my answer below.
    – mc0e
    Commented Jul 21, 2015 at 5:33
15

This is presumably a bit late for the OP, and more intended for other searchers who arrive here.

If you need a non-interactive solution, try this solution from changing timezone with dpkg-reconfigure tzdata and debconf-set-selections

echo "Europe/Zurich" > /etc/timezone 
dpkg-reconfigure -f noninteractive tzdata

You can figure out your timezone by poking around in /usr/share/zoneinfo. In fact the dpkg-reconfigure command above copies the appropriate file from there to /etc/localtime. I used to just do that manually, and it seems to work fine.

Don't be too surprised if long running processes don't pick up the change. eg Apache log timestamps come to mind. Consider whether that matters, and hence whether you need to restart running processes or even reboot.

1
  • 3
    Beware that for this to work, it's a requirement that /etc/localtime does not already exist. If it exists and is a softlink to /usr/share/zoneinfo/whatever, it's taken as base and it is /etc/localtime that is updated accordingly, overwriting your changes.
    – Multisync
    Commented Jul 23, 2018 at 7:15
13

To edit it non interactively on recent Debian and Ubuntu versions (Stretch/Xenial etc.), you need to remove /etc/localtime as well, otherwise /etc/timezone will be overwritten.

echo 'Europe/Zurich' > /etc/timezone 
rm /etc/localtime
dpkg-reconfigure -f noninteractive tzdata

This answer is based on mc0e' answer. I can not add a comment with my reputation.

3

TL;DR

echo "TZ=Etc/UTC" >> /etc/environment
tzconfig

where Etc/UTC is your preferred timezone you found using tzselect.

Longer version

When you're using

dpkg-reconfigure tzdata

the timezone information is not kept after reboot. You can verify your settings with

diff -s /etc/localtime /usr/share/zoneinfo/`cat /etc/timezone`

If you're only updating /etc/timezone, the update won't be consistent with /etc/localtime, thus it's better to execute

tzconfig

Use tzselect to find out which time zones actually exists (or simply browse /usr/share/zoneinfo directory).

In POSIX systems the TZ should take precedence before /etc/localtime:

export TZ='Europe/Berlin'
$ date
Tue Apr 10 08:51:03 CEST 2018
export TZ='Pacific/Efate'
$ date
Tue Apr 10 18:28:33 +11 2018

Using directly TZ can save you many system calls as frequently used date/time related functions has to access filesystem each time some program asks for current date. It's a micro-optimization, but quite simple one.

4
  • Are you positive exporting TZ in your profile file creates a symlink? That does not sound true at all in my experience. Perhaps you meant "Also make sure to create the symlink..."? Commented Apr 9, 2018 at 16:37
  • 1
    @amn No, that won't create a symlink, I've tried to clarify that. But $TZ variable is read by glibc functions: gnu.org/software/libc/manual/html_node/TZ-Variable.html
    – Tombart
    Commented Apr 10, 2018 at 7:01
  • shouldn't it be echo "TZ=Etc/UTC" >> /etc/environment ? you're missing TZ= part... (if case someone is still running ancient debian versions). Also, noninteractive stuff like daemons would still use /etc/localtime version instead of one set in TZ... Commented Sep 27, 2018 at 12:41
  • @MatijaNalis Yes, you're right. Thanks for correction!
    – Tombart
    Commented Sep 28, 2018 at 9:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .