10

I'm using this to configure localtime:

dpkg-reconfigure tzdata 

It is ncurses interface. I'm looking for the way to programming this. Is there true way without user ncurses interface?

How to change localtime by one shell command?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
unixlinuxuser
  • 141
  • 1
  • 1
  • 6
  • This question has an [up-to-date answer on ServerFault](https://serverfault.com/questions/84521/automate-dpkg-reconfigure-tzdata/846989#846989). – Dan Dascalescu May 08 '19 at 03:09

5 Answers5

13

I'm assuming you want to change the timezone by one shell command.

Do you have timedatectl on your system?

If so:

timedatectl status will show your current settings.

timedatectl list-timezones shows available timezones.

timedatectl set-timezone Antarctica/Mawson sets it.

Note: If the RTC is configured to be in the local time, this will also update the RTC time.

Evgeny Vereshchagin
  • 5,286
  • 4
  • 35
  • 43
garethTheRed
  • 33,289
  • 4
  • 92
  • 101
7

Is there true way without user ncurses interface?

echo Antarctica/Mawson >/etc/timezone
dpkg-reconfigure -f noninteractive tzdata

Yes, you can create one shell command:

sh -c 'echo Antarctica/Mawson >/etc/timezone && dpkg-reconfigure -f noninteractive tzdata'

:)

There is a bug in tzdata: certain values get normalized by dpkg-reconfigure:

echo 'US/Central' >/etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Current default time zone: 'America/Chicago'

echo 'US/Eastern' >/etc/timezone
apt-get install --reinstall tzdata
# Current default time zone: 'America/New_York'
Evgeny Vereshchagin
  • 5,286
  • 4
  • 35
  • 43
  • 3
    In 16.04 you have to set `/etc/localtime`, `dpkg-reconfigure` then updates `/etc/timezone`: https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806 – Joó Ádám Jul 15 '16 at 12:13
  • this one works under docker, timedatectl does not. – YudhiWidyatama Nov 06 '16 at 15:35
  • I upvote this because the `apt install --reinstall tzdata` fixed my date problems. Even after adjusting `/etc/localtime` and `/etc/timezone` either by hand or using the various `tz*` commands (including `dpkg-reconfigure`) left the date in a weird state (showed as local time being the same as UTC but timezone was "America" not "PDT") – Dave T. Sep 17 '18 at 21:42
  • I also had to do `/etc/init.d/cron restart` in order for cron jobs to start running under the new timezone. – We Are All Monica Jul 10 '19 at 04:55
2

The way that I'm taking everytime:

# /bin/ln -fs /usr/share/zoneinfo/Asia/Novosibirsk /etc/localtime

It working pretty everywhere, from Linux to BSD family.

anonymous
  • 29
  • 1
  • `dpkg-reconfigure tzdata` updates `/etc/timezone`. `openjdk` uses `/etc/timezone` before `/etc/localtime` to guess the local timezone. see [The algorithm that OpenJDK uses to guess the local timezone ID on Linux](http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-August/020229.html). – Evgeny Vereshchagin Jul 12 '15 at 16:28
  • see also: http://unix.stackexchange.com/a/16294/120177 – Evgeny Vereshchagin Jul 12 '15 at 16:32
  • `cron` [uses](http://anonscm.debian.org/cgit/pkg-cron/pkg-cron.git/tree/debian/cron.init#n56) `/etc/timezone` for `TZ` setting. – Evgeny Vereshchagin Jul 12 '15 at 18:05
  • On Debian `dpkg-reconfigure` makes copy, `timedatectl` makes copy. See [timedated doesn't support split-usr, uses a symlink for /etc/localtime](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=726256), [Useless systemd-timedated /etc/localtime symlink complain in syslog](https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1409594) – Evgeny Vereshchagin Jul 12 '15 at 21:27
2

None of the answers here worked for me. What worked for me was running:

printf "%s\n"                                              \
       "tzdata  tzdata/Areas    select America"            \
       "tzdata  tzdata/Zones/America    select New_York"   \
       | debconf-set-selections

After doing this, when you can now trigger

DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata

and it will automatically use the correct time zone based on the pre-loaded settings.

user541686
  • 3,033
  • 5
  • 28
  • 43
2

When you have interactive commands it's always an option to use expect program:

#!/usr/bin/expect -f
set timeout 360
spawn dpkg-reconfigure tzdata

expect "Geographic area:"

send -- "2\r"

expect "city or region"

send -- "134\r"

This will look for the string in stdout "expect" and then send the string in "send" to stdin.

lucaswxp
  • 121
  • 2
  • 1
    It's worth noting that if your app doesn't use stdout/stdin then this solution won't help. However in the OPs actual use case this should do. – Patrick Taylor Aug 15 '21 at 18:18