Configure localtime. dpkg-reconfigure tzdata
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.
unixlinuxuser
Updated on September 18, 2022Comments
-
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?
-
Admin almost 5 yearsThis question has an up-to-date answer on ServerFault.
-
-
Gilles 'SO- stop being evil' almost 10 yearsWhere's
timedatectl
from?apt-file search timedatectl
returns nothing on Debian stable. -
garethTheRed almost 10 years
timedatectl
is available from thesystemd-services
package on Ubuntu and fromsystemd
on Fedora -
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 almost 9 yearssee also: unix.stackexchange.com/a/16294/120177
-
Evgeny Vereshchagin almost 9 years
-
Evgeny Vereshchagin almost 9 yearsOn Debian
dpkg-reconfigure
makes copy,timedatectl
makes copy. See timedated doesn't support split-usr, uses a symlink for /etc/localtime, Useless systemd-timedated /etc/localtime symlink complain in syslog -
Joó Ádám almost 8 yearsIn 16.04 you have to set
/etc/localtime
,dpkg-reconfigure
then updates/etc/timezone
: bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806 -
YudhiWidyatama over 7 yearsthis one works under docker, timedatectl does not.
-
Hakanai over 5 yearsHmm.
Failed to create bus connection: No such file or directory
-
Dave T. over 5 yearsI 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 varioustz*
commands (includingdpkg-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 almost 5 yearsI also had to do
/etc/init.d/cron restart
in order for cron jobs to start running under the new timezone. -
Patrick Taylor over 2 yearsIt'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.