{{tag>SystemD Réseau Temps NTP UDP}} # NTP temps horloge heure ## Temps horloge Comment vérifier que NTP fonctionne Voir : * Film [Quels temps font-ils ?](http://www.canal-u.tv/video/cerimes/quels_temps_font_ils_une_introduction_au_temps_des_physiciens.7605) * https://wiki.debian.org/fr/NTP * http://www.ntp.org/ntpfaq/NTP-s-trbl-general.htm * Si pb de flux réseau voir [[tunnel_ssh_udp_forward]] * https://www.vmware.com/docs/vmware_timekeeping * /etc/adjtime ''man 5 adjtime_config'' * ''man 4 rtc'' (Real-Time Clock) * https://kb.meinbergglobal.com/kb/time_sync/ntp/ntp_basics ## Configurer ### Configurer via Systemd ''/etc/systemd/timesyncd.conf'' ~~~ini [Time] Servers=0.debian.pool.ntp.org 1.debian.pool.ntp.org ~~~ ~~~bash timedatectl set-ntp true ~~~ Diag ~~~bash systemctl restart systemd-timesyncd systemctl status systemd-timesyncd ~~~ ### Configurer via Chrony TODO ### Configurer via NTP ~~~bash aptitude install ntp ~~~ On configure le serveur de temps via le fichier Notamment les lignes suivantes: ''/etc/ntp.conf'' ~~~ # le serveur doit lui aussi se synchroniser server 0.fr.pool.ntp.org # ou debian.pool.ntp.org server 1.fr.pool.ntp.org # ou debian.pool.ntp.org server 2.fr.pool.ntp.org # ou debian.pool.ntp.org server 3.fr.pool.ntp.org # ou debian.pool.ntp.org # Autoriser les autres ordinateurs à ce connecter: restrict 0.0.0.0 mask 0.0.0.0 # Limiter les ordinateurs pouvant se connecter à notre réseau personnel: broadcast 192.168.0.255 ~~~ #### The systems were using only two NTP servers Four NTP servers are recommended. See http://support.ntp.org/bin/view/Support/SelectingOffsiteNTPServers "With two, it is impossible to tell which one is better, because you don't have any other references to compare them with. This is actually the worst possible configuration -- you'd be better off using just one upstream time server and letting the clocks run free if that upstream were to die or become unreachable." "Use at least 4 NTP servers" #### Pour éviter la dérive sur les VM ** Ou passer à Chrony ** ~~~ tinker panic 0 ~~~ "The tinker panic value of 0 tells NTP that no matter what the time offset is, not to panic and exit. This is recommended for virtual machines because virtual machines have no physical clock and can be paused at anytime and started back up hours later." Source : https://www.redhat.com/en/blog/avoiding-clock-drift-vms Voir : * https://documentation.avaya.com/fr-CA/bundle/DeployingSystemManagerinVE_r10.1/page/Timekeeping__NTP_.html 1 - Add the following line to the beginning of the ''/etc/ntp.conf'' file ~~~bash tinker panic 0 ~~~ 2 - Remove or comment the "driftfile" ~~~bash #driftfile /var/lib/ntp/drift/ntp.drift # path for drift file ~~~ 3 - Restart the ntpd.service ~~~bash /bin/systemctl restart ntpd.service ~~~ Voir https://www.suse.com/fr-fr/support/kb/doc/?id=000021102 ### Configurer via ntpdate ''ntpdate -B'' ajustement progressif ''ntpdate -b'' ajustement brutal ~~~bash aptitude install ntpdate ~~~ Utilisation manuel ~~~bash /usr/sbin/ntpdate -dvu 192.168.2.11 ~~~ Automatisation ~~~bash crontab -e ~~~ Ajouter la ligne suivante: ~~~ 0 3 * * * root /usr/sbin/ntpdate -dv ntp.serveur.org > /dev/null ~~~ Tester / simuler / lecture seul / dry-run ~~~bash ntpdate -q pool.ntp.org ~~~ #### Autres ''/etc/sysconfig/ntp'' ~~~bash SYNC_HWCLOCK=yes ~~~ This is an option to ntpdate ### Horloge matérielle / BIOS / hwclock Afficher la date et l’heure ~~~bash date ~~~ Si la date est incorrecte, utiliser date MMDDmm Afficher l’heure du BIOS ~~~bash hwclock ~~~ Commande pour que le BIOS puisse utiliser l’heure du système ~~~bash hwclock --systohc --utc ~~~ Comparaison du temps système et le BIOS ~~~bash date hwclock ~~~ Vérification dans le fichier ''/etc/sysconfig/clock'' ~~~bash UTC=true ~~~ ## Vérifier Source: https://www.cyberciti.biz/faq/linux-unix-bsd-is-ntp-client-working/ Commandes pour vérifier * ntpq – standard NTP query program * ntpstat – show network time synchronisation status * timedatectl – show or set info about ntp using systemd * chronyc tracking ### ntpstat ~~~bash ntpstat ~~~ | **Exit status** | **Error** | | --------------- | ----------------------------------------------------------------------- | | 0 | Clock is synchronised. | | 1 | Clock is not synchronised. | | 2 | If clock state is indeterminant, for example if ntpd is not contactable | ### ntpq ~~~bash ntpq -pn ~~~ Source : https://www.system-linux.eu/index.php?post/2010/01/05/Mettre-vos-serveurs-%C3%A0-la-bonne-heure-avec-NTP ~~~ # ntpq -p remote refid st t when poll reach delay offset jitter =========================================== europium.canoni 193.79.237.14 2 u 15 64 1 28.682 -3.649 0.015 horlogegps.rese 10.3.128.189 2 u 14 64 1 29.002 -11.238 0.015 kontir.mkc.fr 213.251.128.249 2 u 13 64 1 76.501 18.866 0.015 dnscache-london 140.203.16.5 2 u 12 64 1 58.209 4.266 0.015 ~~~ Une fois la synchronisation faite des *, + et - apparaissent devant les noms de serveur. Le serveur précédé d’une astérisque (*) est celui utilisé, ceux précédés d’un moins (-) sont actuellement éliminés par l’algorithme de choix des serveurs, ceux précédés d’un plus (+) sont des candidats potentiels à la synchronisation et ceux précédés d’un espace sont soit inaccessibles, soit trop éloignés. Une synchronisation correcte implique une valeur de « delay » et de « offset » non nulle, et de « jitter » inférieure à 100 Resynchroniser ~~~bash sudo service ntp stop sudo ntpd -gq sudo service ntp start ~~~ ### SystemD - timedatectl ~~~bash timedatectl status |grep NTP ~~~ ~~~ NTP enabled: yes NTP synchronized: yes ~~~ ### Chrony - chronyc ~~~bash chronyc tracking chronyc sources chronyc sourcestats ~~~ ## Autres ### Timesyncd ''/lib/systemd/system/systemd-timesyncd.service.d/wsl.conf'' ~~~ini # Enable timesyncd on WSL machines # so WSL clock is synced on resume from suspend of the host. [Unit] ConditionVirtualization= ConditionVirtualization=|!container ConditionVirtualization=|wsl ~~~ ### Adjtimex ~~~bash apt-get install adjtimex ~~~ Avant de faire une manip ~~~bash adjtimex -u -l=/var/log/clocks.log ~~~ Voir http://www.linux-france.org/article/sys/heure/ch3.html ### clocksource Oracle recommande de changer le "clock source" à ''tsc'' pour les VM Linux x86-64. ~~~ # cat /sys/devices/system/clocksource/clocksource0/available_clocksource xen tsc acpi_pm ~~~ If the tsc clock source is available, then set tsc as the current clock source. ~~~bash echo "tsc" > /sys/devices/system/clocksource/clocksource0/current_clocksource ~~~ Using any text editor, append the clocksource directive to the GRUB_CMDLINE_LINUX line in the /etc/default/grub file to retain this clock source setting even after a reboot. ''/etc/default/grub'' ~~~bash GRUB_CMDLINE_LINUX="rd.lvm.lv=ol/root rd.lvm.lv=ol/swap rhgb quiet numa=off transparent_hugepage=never clocksource=tsc" ~~~ ## Falsifier la date et l'heure avec faketime ~~~bash faketime 2016-01-01 gpg --list-keys ~~~ ## Pb ### Pb fichier de conf ignoré à cause d'une conf via DHCP Voir aussi : https://coreos.com/os/docs/latest/configuring-date-and-timezone.html ''/var/lib/ntp/ntp.conf.dhcp'' ~~~bash ps -ef |grep ntp ~~~ ~~~ ntp 9777 1 0 08:04 ? 00:00:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -c /var/lib/ntp/ntp.conf.dhcp -u 108:113 ~~~ ~~~bash rm /var/lib/ntp/ntp.conf.dhcp systemctl restart ntp ~~~ ou sur Redhat ~~~bash rm /var/lib/dhclient/chrony.servers.eth0 systemctl restart chronyd.service ~~~ ### Pb - the NTP socket is in use, exiting #### Solution 1 ~~~bash ntpdate -u pool.ntp.org ~~~ #### Solution 2 Arrêt du service (pour libérer le socket réseau UDP) puis ~~~bash ntpdate -u pool.ntp.org ~~~ http://superuser.com/questions/639202/updating-time-ntpdate3108-the-ntp-socket-is-in-use-exiting ntptrace ### Pb avec VirtualBox ''ConditionFileIsExecutable=!/usr/sbin/VBoxService was not met'' ~~~ # systemctl status systemd-timesyncd ● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/systemd-timesyncd.service.d └─disable-with-time-daemon.conf Active: inactive (dead) Condition: start condition failed at Fri 2019-10-11 11:37:16 CEST; 5min ago └─ ConditionFileIsExecutable=!/usr/sbin/VBoxService was not met Docs: man:systemd-timesyncd.service(8) ~~~ #### Solution ''/lib/systemd/system/systemd-timesyncd.service.d/disable-with-time-daemon.conf'' ~~~ini [Unit] # do not run timesyncd if we have another NTP daemon installed #ConditionFileIsExecutable=!/usr/sbin/VBoxService ~~~ ~~~bash systemctl daemon-reload systemctl restart systemd-timesyncd ~~~