Configure localtime. dpkg-reconfigure tzdata

37,220

Solution 1

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.

Solution 2

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'

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
37,220
unixlinuxuser
Author by

unixlinuxuser

Updated on September 18, 2022

Comments

  • unixlinuxuser
    unixlinuxuser over 1 year

    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'
    Gilles 'SO- stop being evil' almost 10 years
    Where's timedatectl from? apt-file search timedatectl returns nothing on Debian stable.
  • garethTheRed
    garethTheRed almost 10 years
    timedatectl is available from the systemd-services package on Ubuntu and from systemd on Fedora
  • Evgeny Vereshchagin
    Evgeny Vereshchagin almost 9 years
    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.
  • Evgeny Vereshchagin
    Evgeny Vereshchagin almost 9 years
  • Evgeny Vereshchagin
    Evgeny Vereshchagin almost 9 years
    cron uses /etc/timezone for TZ setting.
  • Evgeny Vereshchagin
    Evgeny Vereshchagin almost 9 years
  • Joó Ádám
    Joó Ádám almost 8 years
    In 16.04 you have to set /etc/localtime, dpkg-reconfigure then updates /etc/timezone: bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806
  • YudhiWidyatama
    YudhiWidyatama over 7 years
    this one works under docker, timedatectl does not.
  • Hakanai
    Hakanai over 5 years
    Hmm. Failed to create bus connection: No such file or directory
  • Dave T.
    Dave T. over 5 years
    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")
  • We Are All Monica
    We Are All Monica almost 5 years
    I also had to do /etc/init.d/cron restart in order for cron jobs to start running under the new timezone.
  • Patrick Taylor
    Patrick Taylor over 2 years
    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.