27

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

70

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.

  • 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 Apr 21 '13 at 17:56
  • 1
    This is good stuff, but if you need a non-interactive (and therefore scriptable) option, see my answer below. – mc0e Jul 21 '15 at 5:33
13

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.

  • 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 Jul 23 '18 at 7:15
9

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.

7

Update:

The canonical answer (for which Ynhockey deserves credit) appears to be:

dpkg-reconfigure tzdata

My original answer is below. I would simply have deleted this answer entirely but superuser.com doesn't allow accepted answers to be deleted.

http://www.debian-administration.org/articles/213 says

the simple way to edit your timezone is to use the /usr/bin/tzselect command.

...

Once you've set your timezone you might wish to change the UTC setting, upon Debian machines you can find that in the file /etc/default/rcS. Although changing this will require a reboot.

  • This not correct. Changing the UTC setting in /etc/default/rcS changes the timezone of the hardware clock - which is a totally different thing, and for most linux users should stay as UTC. (You only need local time if dual booting with windows.) @Ynhockey has the correct answer. – Ariel Jul 11 '13 at 9:59
  • @Ariel: Thanks. Answer updated as it can't be deleted. – RedGrittyBrick Jul 11 '13 at 10:09
2

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.

  • 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..."? – amn Apr 9 '18 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 Apr 10 '18 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... – Matija Nalis Sep 27 '18 at 12:41
  • @MatijaNalis Yes, you're right. Thanks for correction! – Tombart Sep 28 '18 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.