Hello! These days I was having some problems synchronizing some services that I’m preparing for this lab and it took me a long time to figure out what it was. I’ve always paid very little attention to adjusting the times on my VMs/Containers that I use for testing, labs or training, but this time the difference in time/timezone between some Containers, VMs and my host made me come to this topic.
Where I live (Portugal) we have DST (Daylight Saving Time) which made this time difference worse.
There are 2 points we need to see, the first is the timezones, this will define your time and if there is a DST. The second point is NTP (Network Time Protocol), which, quite simply, is a service that uses the NTP protocol to synchronise your host’s clock with a time server, which are normally extremely accurate.
In VMs, it is advisable to have both configurations, if you need services with combined times for synchronization, as the clocks are completely isolated from the Host. In Docker, only the timezone is enough, the clock is used from the Host, and in my case, my notebook (Debian 12, which works as a docker server and VM) already has NTP configured.
I will first show the timezone and NTP settings for VMs in Ubuntu, AlmaLinux and Alpine and then how to do it in Docker.
The settings are very similar, especially the timezone, for all the OS below and for Docker.
First step, install TimeZones:
apt update
apt install -y tzdata
Now that you have the timezones installed, find your timezone, there is some reference on the websites below:
https://www.iana.org/time-zones
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
But I will show you how to do it only in Linux, the timezones are divided into a first large group, which are not continents but are not countries and cities either. It can also be set to ETC/GMT, for example ETC/GMT-3.
I’m in Portugal, so I know my timezone is Europe/Lisbon.
ls /usr/share/zoneinfo
Africa Atlantic Canada EST5EDT Factory GMT-0 Iceland Japan MST7MDT PRC ROC US Zulu zone.tab
America Australia Chile Egypt GB GMT0 Indian Kwajalein Mexico PST8PDT ROK UTC iso3166.tab zone1970.tab
Antarctica Brazil Cuba Eire GB-Eire Greenwich Iran Libya NZ Pacific Singapore Universal leap-seconds.list
Arctic CET EET Etc GMT HST Israel MET NZ-CHAT Poland Turkey W-SU posixrules
Asia CST6CDT EST Europe GMT+0 Hongkong Jamaica MST Navajo Portugal UCT WET right
ls /usr/share/zoneinfo/Europe
Amsterdam Athens Berlin Bucharest Chisinau Gibraltar Isle_of_Man Kaliningrad Kyiv London Malta Monaco Oslo Prague Samara Saratov Sofia Tirane Uzhgorod Vienna Warsaw Zurich
Andorra Belfast Bratislava Budapest Copenhagen Guernsey Istanbul Kiev Lisbon Luxembourg Mariehamn Moscow Paris Riga San_Marino Simferopol Stockholm Tiraspol Vaduz Vilnius Zagreb
Astrakhan Belgrade Brussels Busingen Dublin Helsinki Jersey Kirov Ljubljana Madrid Minsk Nicosia Podgorica Rome Sarajevo Skopje Tallinn Ulyanovsk Vatican Volgograd Zaporozhye
Once the timezone has been identified, we will create links to the localtime and timezone files and export the TZ environment variable, which can also be placed in your .bashrc .
rm -rf /etc/localtime
rm -rf /etc/timezone
ln -s /usr/share/zoneinfo/Europe/Lisbon /etc/localtime
echo "Europe/Lisbon" > /etc/timezone
export TZ=Europe/Lisbon
At this point, your time should be ok or closer to the real one and this may be enough here, but to make it more accurate, the ideal is to update the clock with an NTP server:
apt -y install chrony
systemctl enable chronyd
systemctl start chronyd
# date
Fri Apr 19 08:08:57 PM WEST 2024
# timedatectl
Local time: Fri 2024-04-19 20:08:59 WEST
Universal time: Fri 2024-04-19 19:08:59 UTC
RTC time: Fri 2024-04-19 19:09:00
Time zone: Europe/Lisbon (WEST, +0100)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# cat /etc/timezone
Europe/Lisbon
Perfect!
First step, install TimeZones:
dnf install -y tzdata
Now that you have the timezones installed, find your timezone, there is some reference on the websites below:
https://www.iana.org/time-zones
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
But I will show you how to do it only in Linux, the timezones are divided into a first large group, which are not continents but are not countries and cities either. It can also be set to Etc/GMT??, for example Etc/GMT-3.
I’m in Portugal, so I know my timezone is Europe/Lisbon.
ls /usr/share/zoneinfo
Africa Arctic Australia CST6CDT Cuba EST5EDT Etc GB-Eire GMT-0 HST Indian Jamaica Libya MST7MDT NZ-CHAT PST8PDT Portugal Singapore US W-SU iso3166.tab posix tzdata.zi
America Asia Brazil Canada EET Egypt Europe GMT GMT0 Hongkong Iran Japan MET Mexico Navajo Pacific ROC Turkey UTC WET leap-seconds.list posixrules zone.tab
Antarctica Atlantic CET Chile EST Eire GB GMT+0 Greenwich Iceland Israel Kwajalein MST NZ PRC Poland ROK UCT Universal Zulu leapseconds right zone1970.tab
ls /usr/share/zoneinfo/Europe
Amsterdam Athens Berlin Bucharest Chisinau Gibraltar Isle_of_Man Kaliningrad Kyiv London Malta Monaco Oslo Prague Samara Saratov Sofia Tirane Uzhgorod Vienna Warsaw Zurich
Andorra Belfast Bratislava Budapest Copenhagen Guernsey Istanbul Kiev Lisbon Luxembourg Mariehamn Moscow Paris Riga San_Marino Simferopol Stockholm Tiraspol Vaduz Vilnius Zagreb
Astrakhan Belgrade Brussels Busingen Dublin Helsinki Jersey Kirov Ljubljana Madrid Minsk Nicosia Podgorica Rome Sarajevo Skopje Tallinn Ulyanovsk Vatican Volgograd Zaporozhye
Once the timezone has been identified, we will create links to the localtime and timezone files and export the TZ environment variable, which can also be placed in your .bashrc .
rm -rf /etc/localtime
rm -rf /etc/timezone
ln -s /usr/share/zoneinfo/Europe/Lisbon /etc/localtime
echo "Europe/Lisbon" > /etc/timezone
export TZ=Europe/Lisbon
At this point, your time should be ok or closer to the real one and this may be enough here, but to make it more accurate, the ideal is to update the clock with an NTP server:
dnf install -y chrony
systemctl enable chronyd
systemctl start chronyd
# date
Fri Apr 19 08:08:57 PM WEST 2024
# timedatectl
Local time: Fri 2024-04-19 20:08:59 WEST
Universal time: Fri 2024-04-19 19:08:59 UTC
RTC time: Fri 2024-04-19 19:09:00
Time zone: Europe/Lisbon (WEST, +0100)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# cat /etc/timezone
Europe/Lisbon
Perfect!
First step, install TimeZones:
apk add tzdata
Now that you have the timezones installed, find your timezone, there is some reference on the websites below:
https://www.iana.org/time-zones
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
But I will show you how to do it only in Linux, the timezones are divided into a first large group, which are not continents but are not countries and cities either. It can also be set to Etc/GMT, for example Etc/GMT-3.
I’m in Portugal, so I know my timezone is Europe/Lisbon.
ls /usr/share/zoneinfo
Africa Atlantic Canada EST5EDT Factory GMT-0 Iceland Japan MST7MDT PRC ROC US Zulu zone.tab
America Australia Chile Egypt GB GMT0 Indian Kwajalein Mexico PST8PDT ROK UTC iso3166.tab zone1970.tab
Antarctica Brazil Cuba Eire GB-Eire Greenwich Iran Libya NZ Pacific Singapore Universal leap-seconds.list
Arctic CET EET Etc GMT HST Israel MET NZ-CHAT Poland Turkey W-SU posixrules
Asia CST6CDT EST Europe GMT+0 Hongkong Jamaica MST Navajo Portugal UCT WET right
ls /usr/share/zoneinfo/Europe
Amsterdam Athens Berlin Bucharest Chisinau Gibraltar Isle_of_Man Kaliningrad Kyiv London Malta Monaco Oslo Prague Samara Saratov Sofia Tirane Uzhgorod Vienna Warsaw Zurich
Andorra Belfast Bratislava Budapest Copenhagen Guernsey Istanbul Kiev Lisbon Luxembourg Mariehamn Moscow Paris Riga San_Marino Simferopol Stockholm Tiraspol Vaduz Vilnius Zagreb
Astrakhan Belgrade Brussels Busingen Dublin Helsinki Jersey Kirov Ljubljana Madrid Minsk Nicosia Podgorica Rome Sarajevo Skopje Tallinn Ulyanovsk Vatican Volgograd Zaporozhye
Once the timezone has been identified, we will create links to the localtime and timezone files and export the TZ environment variable, which can also be placed in your .bashrc .
rm -rf /etc/localtime
rm -rf /etc/timezone
ln -s /usr/share/zoneinfo/Europe/Lisbon /etc/localtime
echo "Europe/Lisbon" > /etc/timezone
export TZ=Europe/Lisbon
At this point, your time should be ok or closer to the real one and this may be enough here, but to make it more accurate, the ideal is to update the clock with an NTP server:
apk add --no-cache chrony
rc-update add chronyd
rc-service chronyd start
# date
Fri Apr 19 08:08:57 PM WEST 2024
# cat /etc/timezone
Europe/Lisbon
Perfect!
The default chrony settings are enough, but if you want to change them in /etc/chrony/chrony.conf. To find the NTP servers closest to where you are, there is the search option on this website: https://www.ntppool.org/en/ e https://www.ntppool.org/en/zone
“In most cases it’s best to use pool.ntp.org to find an NTP server (or 0.pool.ntp.org, 1.pool.ntp.org, etc if you need multiple server names). The system will try finding the closest available servers for you.“
# chronyc -a online
200 OK
# chronyc tracking
Reference ID : 5BD1104E (smtp-in1.aqea.net)
Stratum : 3
Ref time (UTC) : Fri Apr 19 11:42:24 2024
System time : 0.000089235 seconds fast of NTP time
Last offset : +0.000019724 seconds
RMS offset : 0.000067112 seconds
Frequency : 7.818 ppm slow
Residual freq : -0.000 ppm
Skew : 0.010 ppm
Root delay : 0.047378536 seconds
Root dispersion : 0.001409075 seconds
Update interval : 1037.7 seconds
Leap status : Normal
# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ntp2.tecnico.ulisboa.pt 2 10 377 529 +104us[ +125us] +/- 28ms
^+ time.cloudflare.com 3 10 377 46 +237us[ +237us] +/- 24ms
^+ time.cloudflare.com 3 10 377 546 +178us[ +198us] +/- 24ms
^* smtp-in1.aqea.net 2 10 377 145 +460us[ +479us] +/- 25ms
In Docker, we won’t have NTP, only the timezone, but once again, there are several ways to do it in Docker.
The way I find easiest is, if you are on a Linux host, to map your timezone files to the container and set a TZ environment variable, since it is unlikely that a base image comes with tzdata installed. But this doesn’t work on Mac, for example.
docker run --rm -ti \
--network local-bridge \
--env TZ=Europe/Lisbon \
-v /etc/timezone:/etc/timezone \
-v /etc/localtime:/etc/localtime \
--name test-timezone ubuntu:22.04
Another point is to create an image with tzdata already installed or just the timezone files, as it is lighter. I will probably use this solution in the images I will create, as it is independent of the host’s operating system, it could be Linux or Mac.
Leave a Reply