Linux – Setting the Timezone with an automated script

bashdebianlinuxscriptingtimezone

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

Best Answer

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.

Related Topic