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?
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?
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.
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'
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.
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.
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.