Setting the Timezone with an automated script

18,089

Solution 1

You should be able to do this with

cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

Substitute the appropriate timezone in the above command.

Solution 2

Try this:

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

Solution 3

On Ubuntu 12.04 the following did the trick for me:

# http://manpages.ubuntu.com/manpages/precise/man7/debconf.7.html
export DEBCONF_NONINTERACTIVE_SEEN=true DEBIAN_FRONTEND=noninteractive
echo "Europe/Berlin" > /etc/timezone
dpkg-reconfigure tzdata

Solution 4

Update /etc/localtime symlink to point to correct timezone in /usr/share/zoneinfo/

ln -sf /usr/share/zoneinfo/$(tzselect) /etc/localtime

Solution 5

Ran into this myself. Here's a complete config for how to do this right (it's an aggregate of comments in here and this question):

#Set time zone and time
echo "tzdata tzdata/Areas select Europe" | debconf-set-selections
echo "tzdata tzdata/Zones/Europe select London" | debconf-set-selections
TIMEZONE="Europe/London"
echo $TIMEZONE > /etc/timezone
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
/usr/sbin/ntpdate pool.ntp.org
Share:
18,089

Related videos on Youtube

Tom
Author by

Tom

Updated on September 17, 2022

Comments

  • Tom
    Tom over 1 year

    I'm writing scripts to automate setting up new slicehost installations. In a perfect world, after I started the script, it would just run, with no attention from me. I have succeeded, with one exception.

    How do I set the timezone, in a permanent (survive reboot) and sane (adjust for standard and daylight savings time, so no just forcing the date) ... manner that doesn't require input from me?

    Currently, I'm using

     dpkg-reconfigure tzdata
    

    This doesn't seem to have any way to force parameters into it. It demands user input.


    EDIT: I'm editing here, rather than commenting, since comments don't seem to allow code blocks.

    Here's the actual code I ended up with, based on Rudedog's comment below. I also noticed that this doesn't update /etc/timezone. I'm not certain who uses that, but in case anybody does, I'm setting that too.

    TIMEZONE="America/Los_Angeles"      
    echo $TIMEZONE > /etc/timezone                     
    cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime   # This sets the time
    
  • Zypher
    Zypher over 14 years
    you should be symlinking that not copying over
  • Rudedog
    Rudedog over 14 years
    He's using Debian. dpkg-reconfigure tzdata makes a copy, not a symlink. I believe the rationale is because /usr/share might not be mounted at boot time.
  • brent
    brent over 14 years
    According to the technical section of the tzconfig manpage for Debian: >The work done by tzconfig is actually pretty simple. It just updates the link /etc/localtime to point to the correct timezone installed in /usr/share/zoneinfo/.
  • Rudedog
    Rudedog over 14 years
    tzconfig is deprecated on lenny in favor of dpkg-reconfigure tzdata and won't actually do anything. You can verify that it copies instead of symlinks by running dpkg-reconfigure tzdata and look at the result.
  • peterh
    peterh about 9 years
    Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long but it is expected.
  • Peter Gibson
    Peter Gibson over 7 years
    This method doesn't appear to work anymore bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806
  • Peter Gibson
    Peter Gibson over 7 years
    This method doesn't appear to work anymore bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806
  • SuB
    SuB over 6 years
    Works on Ubuntu 14.04.