Gestion de l’heure système

La synchronisation de l’horloge système est essentielle pour l’analyse des logs. Le protocole NTP est utilisé pour fournir un horodatage correct.

Commande timedatectl

La commande timedatectl affiche les paramètres d’horodatage :

# timedatectl
Local time: Sun 2024-09-22 03:46:12 EDT
Universal time: Sun 2024-09-22 07:46:12 UTC
RTC time: Sun 2024-09-22 07:46:12
Time zone: America/Toronto (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

Afficher la BDD des fuseaux horaires :

# timedatectl list-timezones
[...]
Europe/Rome
Europe/Samara
Europe/Saratov
Europe/Simferopol
Europe/Sofia
Europe/Stockholm
[...]

Appliquer un fuseau horaire :

# timedatectl set-timezone Europe/Stockholm

Changer l’heure :

# timedatectl set-time 13:37:00

Désactiver la synchronisation NTP (en cas d’echec pour changer manuellement l’heure) :

# timedatectl set-ntp false

Parametrage interactif avec tzselect

Configurer une time-zone avec tzselect :

# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa 5) Atlantic Ocean 9) Pacific Ocean
2) Americas 6) Australia 10) coord - I want to use geographical coordinates.
3) Antarctica 7) Europe 11) TZ - I want to specify the timezone using the Posix TZ format.
4) Asia 8) Indian Ocean
#? 7
Please select a country whose clocks agree with yours.
1) Albania 8) Bulgaria 15) Germany 22) Italy 29) Moldova 36) Portugal 43) Spain 50) Åland Islands
2) Andorra 9) Croatia 16) Gibraltar 23) Jersey 30) Monaco 37) Romania 44) Svalbard & Jan Mayen
3) Austria 10) Czech Republic 17) Greece 24) Latvia 31) Montenegro 38) Russia 45) Sweden
4) Belarus 11) Denmark 18) Guernsey 25) Liechtenstein 32) Netherlands 39) San Marino 46) Switzerland
5) Belgium 12) Estonia 19) Hungary 26) Lithuania 33) North Macedonia 40) Serbia 47) Turkey
6) Bosnia & Herzegovina 13) Finland 20) Ireland 27) Luxembourg 34) Norway 41) Slovakia 48) Ukraine
7) Britain (UK) 14) France 21) Isle of Man 28) Malta 35) Poland 42) Slovenia 49) Vatican City
#? 14
The following information has been given:

France

Therefore TZ='Europe/Paris' will be used.
Selected time is now: Sun Sep 22 09:49:22 CEST 2024.
Universal Time is now: Sun Sep 22 07:49:22 UTC 2024.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Europe/Paris'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Europe/Paris

service chronyd

Le service chronyd se base sur une horloge temps réel (RTC) locale en la synchronisant avec des serveurs NTP configurés dans /etc/chrony.conf.

L’option iburst permet de réduire le décalage de synchromisation entre les horloges.

Fichier de configuration /etc/chrony.conf :

server my.NTP.server.org iburst

Redémarrez le service après avoir modifier l’adresse du serveur NTP :

# systemctl restart chronyd

Pour vérifier que le système utilise le bon serveur NTP pour synchroniser l’horloge système :

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ time.cloudflare.com 3 10 377 239 -305us[ -305us] +/- 17ms
^+ 10-42-42-22.cpe.distr> 1 10 377 275 +348us[ +348us] +/- 7268us
^* 10-42-42-22.cpe.distr> 1 10 377 1029 -223us[ -322us] +/- 7161us
^- ntp1.yow.accuris.ca 2 10 377 721 +7001us[+7001us] +/- 45ms

/!\ Résumé des commandes

Commandes :

# timedatectl                               // Afficher les information de l'horodatage
# timedatectl list-timezones // Lister les fuseaux
# timedatectl set-timezone Europe/Stockholm // Changer le fuseau
# timedatectl set-time 13:37:00 // Changer l'heure
# timedatectl set-ntp false // En cas d'echec pour changer l'heure

# systemctl restart chronyd // Charger la config
# chronyc sources -v // Verifier la config

# tzselect // Configurer le fuseau horaire

Fichiers :

/etc/chrony.conf

server my.NTP.server.org iburst // parametrage d'un serveur NTP dans le fichier /etc/chrony.conf

Cas Pratiques

Fuseau horaire

Sélection du fuseau horaire approprié pour la base Antarctique Troll.

[root@SERVER01 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa 5) Atlantic Ocean 9) Pacific Ocean
2) Americas 6) Australia 10) coord - I want to use geographical coordinates.
3) Antarctica 7) Europe 11) TZ - I want to specify the timezone using the Posix TZ format.
4) Asia 8) Indian Ocean
#? 3

Please select a country whose clocks agree with yours.
1) Antarctica
2) Australia
#? 1

Please select one of the following timezones.
1) Casey 5) Rothera 9) Papua New Guinea (most areas), Chuuk, Yap, Dumont d'Urville
2) Davis 6) Troll 10) Syowa
3) Mawson 7) Vostok
4) Palmer 8) New Zealand time
#? 6

The following information has been given:

Antarctica
Troll

Therefore TZ='Antarctica/Troll' will be used.
Selected time is now: Sun Sep 22 09:57:55 +02 2024.
Universal Time is now: Sun Sep 22 07:57:55 UTC 2024.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
TZ='Antarctica/Troll'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Antarctica/Troll

Mise à jour du fuseau horaire sur Antarctica/Troll :

[root@SERVER01 ~]# timedatectl set-timezone Antarctica/Troll

Vérification :

[root@Redhat1-Test1 ~]# timedatectl
Local time: Sun 2024-09-22 10:00:21 +02
Universal time: Sun 2024-09-22 08:00:21 UTC
RTC time: Sun 2024-09-22 08:00:21
Time zone: Antarctica/Troll (+02, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

Configurer chronyd

Configuration de chronyd en éditant le fichier /etc/chrony.conf pour synchroniser l’heure du système avec le serveur NTP atomic-clock.org :

[root@SERVER01 ~]# vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.

server atomic-clock.org iburst
[...]

Activer la synchronisation :

[root@SERVER01 ~]# sudo timedatectl set-ntp true

Vérification :

[root@SERVER01 ~]# timedatectl
               Local time: Sun 2024-09-22 10:53:28 +02
Universal time: Sun 2024-09-22 08:53:28 UTC
RTC time: Sun 2024-09-22 08:53:28
Time zone: Antarctica/Troll (+02, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

Vérification que le serveur est synchronisé, l’astérisque (*) indique que l’heure du système local est correctement synchronisée avec la source de temps NTP :

[root@SERVER01 ~]# chronyc sources -v
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172-xxx-yyy-42.cpe.distr> 1 10 377 43 +188us[ +209us] +/- 7291us

Documentation

MAN timedatectl(1)
MAN tzselect(8)
MAN chronyd(8)
MAN chrony.conf(5)
MAN chronyc(1)

http://www.ntppool.org/
http://www.iana.org/time-zones

> Partager <