{"id":508691,"date":"2025-08-13T03:59:43","date_gmt":"2025-08-13T03:59:43","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=508691"},"modified":"2025-08-13T03:59:43","modified_gmt":"2025-08-13T03:59:43","slug":"scripts-shell-pour-synchroniser-lheure-dans-linux","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/fr\/script-hub\/scripts-shell-pour-synchroniser-lheure-dans-linux\/","title":{"rendered":"Comment utiliser les scripts Shell pour synchroniser l&rsquo;heure dans Linux"},"content":{"rendered":"<p>La synchronisation du temps est une des bases de l&rsquo;infrastructure informatique moderne. De l&rsquo;enregistrement pr\u00e9cis \u00e0 la coordination des syst\u00e8mes distribu\u00e9s, en passant par les communications s\u00e9curis\u00e9es, il est essentiel de disposer d&rsquo;une heure syst\u00e8me coh\u00e9rente entre les serveurs et les <a href=\"https:\/\/www.ninjaone.com\/fr\/blog\/qu-est-ce-qu-un-terminal-vue-d-ensemble-pour-les-pros-de-l-informatique\/\">terminaux<\/a>. Pour les professionnels de l&rsquo;informatique et les fournisseurs de services g\u00e9r\u00e9s (MSP), il est essentiel de disposer d&rsquo;une m\u00e9thode fiable et automatis\u00e9e pour <strong>synchroniser l&rsquo;heure dans Linux<\/strong>. Cet article pr\u00e9sente un script shell efficace con\u00e7u pour synchroniser l&rsquo;heure en utilisant <code>chronyd<\/code>ou <code>systemd-timesyncd<\/code>, deux services de protocole de temps r\u00e9seau (NTP) largement support\u00e9s sous Linux.<\/p>\n<h2>Contexte<\/h2>\n<p>De nombreux environnements informatiques, en particulier ceux qui s&rsquo;appuient sur des plateformes de gestion centralis\u00e9e et d&rsquo;automatisation comme NinjaOne, exigent une synchronisation temporelle stricte. Les corr\u00e9lations de journaux, les versions de fichiers, les jetons d&rsquo;authentification et les pistes d&rsquo;audit d\u00e9pendent tous d&rsquo;horloges syst\u00e8me pr\u00e9cises. Bien que les services NTP tels que <code>chronyd<\/code>et <code>systemd-timesyncd<\/code>s&rsquo;en chargent souvent en arri\u00e8re-plan, une mauvaise configuration, des pannes de service ou des incoh\u00e9rences dans l&rsquo;image du syst\u00e8me peuvent perturber la synchronisation.<\/p>\n<p>Ce script shell s&rsquo;attaque directement \u00e0 ces probl\u00e8mes en d\u00e9tectant automatiquement les services de synchronisation disponibles, en s&rsquo;assurant qu&rsquo;ils sont correctement configur\u00e9s, en les d\u00e9marrant temporairement si n\u00e9cessaire, en synchronisant l&rsquo;heure et en \u00e9tablissant des diagnostics d\u00e9taill\u00e9s. Il g\u00e8re \u00e9galement les services de mani\u00e8re fluide, en ramenant les syst\u00e8mes \u00e0 leur \u00e9tat ant\u00e9rieur si les services de synchronisation temporelle ont \u00e9t\u00e9 initialement d\u00e9sactiv\u00e9s.<\/p>\n<h2>Le script\u00a0:<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">#!\/usr\/bin\/env bash\r\n#\r\n# Description: Synchronize the time on Linux using the system's network time server. Expects chrony or systemd-timesyncd to be installed and running.\r\n# By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n# Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n# Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n# Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n# Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n# Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n# Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#\r\n# Minimum OS Architecture Supported: Debian 11 (Bullseye)+, Red Hat Enterprise Linux (RHEL) 8+\r\n#\r\n# Release Notes: Initial Release\r\n#\r\n\r\n# When run directly without testing, the \"__()\" function does nothing.\r\ntest || __() { :; }\r\n\r\ndie() {\r\n    local _ret=\"${2:-1}\"\r\n    echo \"$1\" &gt;&amp;2\r\n    exit \"${_ret}\"\r\n}\r\n\r\nfunction GetChronyConfigFile() {\r\n    local _configFile\r\n    # Check if there is a chrony config folder\r\n    if [[ -d \"\/etc\/chrony\" ]]; then\r\n        # Check if the config file exists\r\n        if [[ -f \"\/etc\/chrony\/chrony.conf\" ]]; then\r\n            _configFile=\"\/etc\/chrony\/chrony.conf\"\r\n        fi\r\n    fi\r\n    # Check if primary default config file exists, this takes precedence over the chrony folder\r\n    if [[ -f \"\/etc\/chrony.conf\" ]]; then\r\n        _configFile=\"\/etc\/chrony.conf\"\r\n    fi\r\n    # Get the system's default config file for chronyd\r\n    _chronySystemDefaultConfigFile=$(\r\n        # Get man page for chronyd\r\n        # Looking for a line containing this \"The compiled-in default value is \/etc\/chrony\/chrony.conf.\"\r\n        man chronyd |\r\n            # Get the default config file location\r\n            grep \/chrony.conf | grep default |\r\n            # Get the last item in the line\r\n            awk '{print $NF}' |\r\n            # Remove the trailing period\r\n            sed 's\/\\.$\/\/'\r\n    )\r\n    # Check if the config file exists\r\n    if [[ -f \"$_chronySystemDefaultConfigFile\" ]]; then\r\n        _configFile=\"$_chronySystemDefaultConfigFile\"\r\n    fi\r\n\r\n    # Validate that the config file exists\r\n    if [[ -n \"${_configFile}\" &amp;&amp; -f \"$_configFile\" ]]; then\r\n        echo \"$_configFile\"\r\n    else\r\n        echo \"[Error] No chrony config file found\"\r\n        exit 1\r\n    fi\r\n}\r\n\r\nfunction GetNtpPoolServers() {\r\n    local -a _configFiles\r\n    local -a _pools\r\n    local -a _servers\r\n    local -a _timedatectlConfigFiles\r\n    _timedatectlConfigFiles=(\r\n        # Last take precedence over previous ones\r\n        \"\/usr\/lib\/systemd\/timesyncd.conf.d\"\r\n        \"\/usr\/local\/lib\/systemd\/timesyncd.conf.d\"\r\n        \"\/run\/systemd\/timesyncd.conf.d\"\r\n        \"\/etc\/systemd\/timesyncd.conf.d\"\r\n        \"\/usr\/lib\/systemd\/timesyncd.conf\"\r\n        \"\/usr\/local\/lib\/systemd\/timesyncd.conf\"\r\n        \"\/run\/systemd\/timesyncd.conf\"\r\n        \"\/etc\/systemd\/timesyncd.conf\"\r\n    )\r\n    # Get the NTP pool servers for the specified region\r\n\r\n    echo \"[Info] Getting NTP pools and servers\"\r\n    if command -v chronyc &amp;&gt;\/dev\/null; then\r\n        # Get the NTP pool servers for chrony\r\n        if [[ -f \"${_chronyConfigFile}\" ]]; then\r\n            _pools+=$(grep -E \"^pool\" \"${_chronyConfigFile}\" | awk '{print $2}' | sort -u)\r\n            _servers+=$(grep -E \"^server\" \"${_chronyConfigFile}\" | awk '{print $2}' | sort -u)\r\n        fi\r\n    elif command -v timedatectl &amp;&gt;\/dev\/null; then\r\n        # Get the NTP pool servers for systemd-timesyncd\r\n        for configFile in \"${_timedatectlConfigFiles[@]}\"; do\r\n            if [[ -d \"$configFile\" ]]; then\r\n                # If the config file is a directory, check for .conf files inside it\r\n                for confFile in \"$configFile\"\/*.conf; do\r\n                    if [[ -f \"$confFile\" ]]; then\r\n                        # Append the contents of the .conf file to the list of config files\r\n                        _pools+=$(grep -E \"^NTP=\" \"$configFile\" | awk -F '=' '{print $2}' | sort -u)\r\n                        _pools+=$(grep -E \"^FallbackNTP=\" \"$configFile\" | awk -F '=' '{print $2}' | sort -u)\r\n                    fi\r\n                done\r\n            elif [[ -f \"$configFile\" ]]; then\r\n                _pools+=$(grep -E \"^NTP=\" \"$configFile\" | awk -F '=' '{print $2}' | sort -u)\r\n                _pools+=$(grep -E \"^FallbackNTP=\" \"$configFile\" | awk -F '=' '{print $2}' | sort -u)\r\n            fi\r\n        done\r\n    else\r\n        echo \"[Error] chronyc or timedatectl not found\"\r\n        exit 1\r\n    fi\r\n\r\n    # Return the list of NTP pools and servers\r\n    if [[ -n \"${_pools}\" ]]; then\r\n        echo \"[Info] NTP pools:\"\r\n        echo \"$_pools\"\r\n    fi\r\n    if [[ -n \"${_servers}\" ]]; then\r\n        echo \"[Info] NTP servers:\"\r\n        echo \"$_servers\"\r\n    fi\r\n    if [[ -z \"${_pools}\" &amp;&amp; -z \"${_servers}\" ]]; then\r\n        # If no pools or servers were found\r\n        echo \"[Info] Distribution's default NTP servers are being used.\"\r\n    fi\r\n}\r\n\r\nfunction GetNtpConfiguration() {\r\n\r\n    # Get the NTP configuration for the specified region\r\n\r\n    if command -v chronyc &amp;&gt;\/dev\/null; then\r\n        echo \"[Info] NTP configuration:\"\r\n        if ! [[ -f \"$_chronyConfigFile\" ]]; then\r\n            _chronyConfigFile=\"$(GetChronyConfigFile)\"\r\n        fi\r\n\r\n        # Get the NTP configuration for chrony\r\n        grep -E \"^server|^pool\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null\r\n        if [[ -f \"${_chronyConfigFile}\" ]]; then\r\n            grep -E \"^sourcedir\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null | awk '{print $2}' | while read -r _dir; do\r\n                if [[ -d \"$_dir\" ]]; then\r\n                    for confFile in \"$_dir\"\/*.conf; do\r\n                        # Check if the file exists before grepping\r\n                        if [[ -f \"$confFile\" ]]; then\r\n                            grep -E \"^server|^pool\" &lt;\"$confFile\" 2&gt;\/dev\/null\r\n                        fi\r\n                    done\r\n                fi\r\n            done\r\n            grep -E \"^confdir\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null | awk '{print $2}' | while read -r _dir; do\r\n                if [[ -d \"$_dir\" ]]; then\r\n                    for confFile in \"$_dir\"\/*.conf; do\r\n                        # Check if the file exists before grepping\r\n                        if [[ -f \"$confFile\" ]]; then\r\n                            grep -E \"^server|^pool\" &lt;\"$confFile\" 2&gt;\/dev\/null\r\n                        fi\r\n                    done\r\n                fi\r\n            done\r\n        fi\r\n    elif command -v timedatectl &amp;&gt;\/dev\/null; then\r\n        # Get the NTP configuration for systemd-timesyncd\r\n        if ((\"$(timedatectl --version | grep systemd | awk '{print $2}')\" &gt;= 239)); then # Try to get the NTP configuration\r\n            echo \"[Info] NTP configuration:\"\r\n            timedatectl show-timesync 2&gt;\/dev\/null\r\n        else\r\n            # If it fails then we are running and older version of systemd, then get the status\r\n            echo \"[Warn] systemd version is older than 239, show-timesync is not available.\"\r\n        fi\r\n    else\r\n        echo \"[Error] No NTP configuration found.\"\r\n        exit 1\r\n    fi\r\n}\r\n\r\nfunction IsChronySecurityConfigured() {\r\n    # Check if the keyfile is set in chrony.conf\r\n    if grep -q \"^keyfile\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null; then\r\n        # Check if commandkey is set in chrony.conf\r\n        if grep -q \"^commandkey\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null; then\r\n            echo \"[Info] Keyfile and commandkey are set in ${_chronyConfigFile}\"\r\n            _id=$(grep \"^keyfile\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null | awk '{print $2}')\r\n            _keyfile=$(grep \"^commandkey\" \"${_chronyConfigFile}\" 2&gt;\/dev\/null | awk '{print $2}')\r\n            # Check if the keyfile exists\r\n            if [[ -f \"$_keyfile\" ]]; then\r\n                # Check if commandkey exists in keyfile\r\n                if grep -q \"^$_id\" \"$_keyfile\"; then\r\n                    # Keyfile exists and commandkey is set\r\n                    return 0\r\n                else\r\n                    # Keyfile exists but commandkey is not set\r\n                    echo \"[Error] Keyfile $_keyfile exists but commandkey $_id is not set in it\"\r\n                    return 1\r\n                fi\r\n            else\r\n                # Keyfile does not exist\r\n                echo \"[Error] Keyfile $_keyfile does not exist\"\r\n                return 1\r\n            fi\r\n        else\r\n            # Keyfile is set but commandkey is not set\r\n            return 1\r\n        fi\r\n    else\r\n        # Keyfile is not set\r\n        return 1\r\n    fi\r\n}\r\n\r\n__ begin __\r\n\r\n# Check if the script is being run as root. If not, exit with an error message.\r\nif [[ $(id -u) -ne 0 ]]; then\r\n    die \"[Error] This script must be run with root permissions. Try running it with sudo or as the system\/root user.\" 1\r\nfi\r\n\r\n_is_enabled=0\r\n_is_active=0\r\n\r\n# Check if time synchronization services are installed\r\nif systemctl list-unit-files | grep -q -E \"(systemd-timesyncd|chronyd).service\"; then\r\n    echo \"[Info] Time synchronization service(s) found:\"\r\n    # Check if systemd-timesyncd or chronyd is running\r\n    while read -r service; do                                          # Read each service name\r\n        if systemctl is-enabled --quiet \"${service}\" 2&gt;\/dev\/null; then # Check if the service is enabled\r\n            echo \"[Info] ${service} is enabled\"\r\n            _is_enabled=1\r\n        else\r\n            echo \"[Info] ${service} is not enabled\"\r\n        fi\r\n        if systemctl is-active --quiet \"${service}\" 2&gt;\/dev\/null; then # Check if the service is active\/running\r\n            echo \"[Info] ${service} is running\"\r\n            _is_active=1\r\n        else\r\n            echo \"[Info] ${service} is not running\"\r\n        fi\r\n    done &lt; &lt;(\r\n        systemctl list-unit-files |                         # List all unit files\r\n            grep -E \"(systemd-timesyncd|chronyd).service\" | # Filter for systemd-timesyncd or chronyd\r\n            awk '{print $1}'                                # Get the first column (service name)\r\n    )\r\n    _service_name=$(systemctl list-unit-files | grep -E \"(systemd-timesyncd|chronyd).service\" | awk '{print $1}')\r\n    if [[ -z \"${_service_name}\" ]] &amp;&amp; [[ $_is_enabled -eq 0 ]] &amp;&amp; [[ \"${_is_active}\" -eq 0 ]]; then\r\n        die \"[Error] No time synchronization service found. Please install systemd-timesyncd, or chrony.\" 1\r\n    fi\r\n    if [[ \"${_is_active}\" -eq 0 ]]; then\r\n        echo \"[Info] Time synchronization service is not running. Will start it now, sync it, and stop it after.\"\r\n        # Start the service\r\n        if [[ \"${_service_name}\" == *\"chronyd\"* ]]; then\r\n            # If chronyd is available, start it as it takes precedence over systemd-timesyncd\r\n            # Start chronyd\r\n            echo \"[Info] Starting time synchronization service: chronyd\"\r\n            systemctl start chronyd.service 2&gt;\/dev\/null || die \"[Error] Failed to start chronyd service\" 1\r\n            sleep 2\r\n        elif [[ \"${_service_name}\" == *\"systemd-timesyncd\"* ]]; then\r\n            # chronyd is not available, so we will use systemd-timesyncd\r\n            # Start systemd-timesyncd\r\n            echo \"[Info] Starting time synchronization service: systemd-timesyncd\"\r\n            systemctl start systemd-timesyncd.service 2&gt;\/dev\/null || die \"[Error] Failed to start systemd-timesyncd service\" 1\r\n            sleep 2\r\n        else\r\n            # If neither chronyd nor systemd-timesyncd is available, exit with an error\r\n            die \"[Error] No time synchronization service found. Please install systemd-timesyncd, or chrony.\" 1\r\n        fi\r\n    fi\r\nelse\r\n    die \"[Error] No time synchronization service found. Please install systemd-timesyncd, or chrony.\" 1\r\nfi\r\n\r\n_shouldError=0\r\n\r\n# Sync time now\r\necho \"[Info] Syncing time now\"\r\nif command -v chronyc &amp;&gt;\/dev\/null; then\r\n    # Check if keyfile is set in chrony.conf\r\n    if IsChronySecurityConfigured; then\r\n        # Use chronyc to sync time\r\n        echo \"[Info] Syncing time using chronyc with keyfile in ${_chronyConfigFile}\"\r\n        if [[ \"$(chronyc -a makestep 2&gt;\/dev\/null)\" == *\"OK\"* ]]; then\r\n            echo \"[Info] Time syncing\"\r\n        else\r\n            echo \"[Error] Failed to sync time using chronyc\"\r\n            _shouldError=1\r\n        fi\r\n    else\r\n        # Sync time using chronyc\r\n        echo \"[Info] Syncing time using chronyc\"\r\n        if [[ \"$(chronyc makestep 2&gt;\/dev\/null)\" == *\"OK\"* ]]; then\r\n            echo \"[Info] Time syncing\"\r\n        else\r\n            echo \"[Error] Failed to sync time using chronyc\"\r\n            _shouldError=1\r\n        fi\r\n    fi\r\n\r\n    # Verify that time is synced if we had no errors\r\n    if ((_shouldError == 0)); then\r\n        # Sleep for 5 seconds to allow some time to sync after restarting chronyd\r\n        sleep 5\r\n\r\n        # Check if time is synced with chronyc tracking\r\n        case $(chronyc tracking 2&gt;\/dev\/null | grep \"Leap status\") in\r\n        *\"Leap Second\"* | *\"Normal\"*)\r\n            # If the leap status is \"Leap Second\" or \"Normal\", then time is synced\r\n            echo \"[Info] Time synced successfully\"\r\n            ;;\r\n        *\"Insert second\"*)\r\n            # If the leap status is \"Insert second\", then time is being synced\r\n            echo \"[Info] Chrony is in the process of inserting a second and will be synced over time to prevent time jumps.\"\r\n            ;;\r\n        *\"Delete second\"*)\r\n            # If the leap status is \"Delete second\", then time is being synced\r\n            echo \"[Info] Chrony is in the process of deleting a second and will be synced over time to prevent time jumps.\"\r\n            ;;\r\n        *\"Not synchronized\"*)\r\n            # If the leap status is \"Not synchronized\", then time is not synced\r\n            echo \"[Error] Chrony is not synchronized.\"\r\n            _shouldError=1\r\n            ;;\r\n        *\"Unknown\"*)\r\n            # If the leap status is \"Unknown\", then time is not synced\r\n            echo \"[Error] Unknown status from chronyc tracking.\"\r\n            _shouldError=1\r\n            ;;\r\n        *)\r\n            # If the leap status is not recognized, then time is not synced\r\n            echo \"[Error] Failed to get sync status from chronyc\"\r\n            _shouldError=1\r\n            ;;\r\n        esac\r\n\r\n        # Stop chronyd if it was not running before\r\n        if ((_is_active == 0)); then\r\n            sleep 5\r\n            # Get the service name\r\n            _service_name=$(systemctl list-unit-files | grep -E \"chronyd.service\" | awk '{print $1}')\r\n            echo \"[Info] Stopping time synchronization service: ${_service_name}\"\r\n            # Stop the service\r\n            systemctl stop \"$_service_name\" 2&gt;\/dev\/null\r\n        fi\r\n    fi\r\nelif command -v timedatectl &amp;&gt;\/dev\/null; then\r\n    # Sync time using timedatectl\r\n    echo \"[Info] Syncing time using timedatectl\"\r\n    # Restart systemd-timesyncd to force a sync\r\n    if systemctl restart systemd-timesyncd.service 2&gt;\/dev\/null; then\r\n        if timedatectl status 2&gt;\/dev\/null | grep \"synchronized\" | grep -q \"yes\"; then\r\n            echo \"[Info] Time synced successfully\"\r\n        else\r\n            echo \"[Error] Failed to sync time using timedatectl\"\r\n            _shouldError=1\r\n        fi\r\n    else\r\n        echo \"[Error] Failed to sync time using timedatectl\"\r\n        _shouldError=1\r\n    fi\r\n\r\n    # Stop systemd-timesyncd if it was not running before\r\n    if ((_is_active == 0)); then\r\n        sleep 5\r\n        # Get the service name\r\n        _service_name=$(systemctl list-unit-files | grep -E \"systemd-timesyncd.service\" | awk '{print $1}')\r\n        echo \"[Info] Stopping time synchronization service: ${_service_name}\"\r\n        # Stop the service\r\n        systemctl stop \"$_service_name\" 2&gt;\/dev\/null\r\n    fi\r\nelse\r\n    echo \"[Error] No time synchronization service found. Please install systemd-timesyncd, or chrony.\"\r\n    exit 1\r\nfi\r\n\r\necho \"\"\r\n\r\nGetNtpConfiguration\r\nGetNtpPoolServers\r\n\r\nif ((_shouldError == 1)); then\r\n    exit 1\r\nfi\r\n<\/pre>\n<p>&nbsp;<\/p>\n\n<h2>Description d\u00e9taill\u00e9e<\/h2>\n<p>Ce script est structur\u00e9 en plusieurs fonctions et sections pour un contr\u00f4le modulaire et une plus grande clart\u00e9. Voici un aper\u00e7u de ses principaux composants\u00a0:<\/p>\n<h3>1. Contr\u00f4les pr\u00e9alables<\/h3>\n<ul>\n<li><strong>Contr\u00f4le des autorisations de la racine<\/strong><strong><br \/>\n<\/strong>Veillez \u00e0 ce que le script soit ex\u00e9cut\u00e9 en tant que <code>racine<\/code>, car le contr\u00f4le du temps et des services n\u00e9cessite des privil\u00e8ges \u00e9lev\u00e9s.<\/li>\n<li><strong>D\u00e9couverte de services<\/strong><strong><br \/>\n<\/strong>Recherchez <code>chronyd<\/code>ou <code>systemd-timesyncd<\/code>. Si aucun des deux n&rsquo;est install\u00e9, le script se termine par un message d&rsquo;erreur clair.<\/li>\n<\/ul>\n<h3>2. Contr\u00f4le et synchronisation des services<\/h3>\n<ul>\n<li><strong>\u00c9valuation de l&rsquo;\u00e9tat des services<\/strong><strong><br \/>\n<\/strong>D\u00e9termine si chaque service est activ\u00e9 et\/ou en cours d&rsquo;ex\u00e9cution. S&rsquo;il n&rsquo;est pas en cours d&rsquo;ex\u00e9cution, il d\u00e9marre temporairement le programme appropri\u00e9.<\/li>\n<li><strong>Synchronisation temporelle<\/strong>\n<ul>\n<li>Si vous utilisez <code>chronyc<\/code>: Emet la commande <code>maketep<\/code>pour synchroniser imm\u00e9diatement l&rsquo;heure.<\/li>\n<li>Si vous utilisez <code>timedatectl<\/code>: Red\u00e9marre <code>systemd-timesyncd<\/code>et v\u00e9rifie l&rsquo;\u00e9tat de la synchronisation.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Restauration des services<\/strong><strong><br \/>\n<\/strong>Si un service \u00e9tait initialement inactif, le script l&rsquo;arr\u00eate \u00e0 nouveau apr\u00e8s la synchronisation.<\/li>\n<\/ul>\n<h3>3. Inspection de la configuration<\/h3>\n<ul>\n<li><strong><code>GetChronyConfigFile()<\/code><\/strong>: localise le fichier de configuration de chrony en se basant sur les conventions du syst\u00e8me ou les valeurs par d\u00e9faut des pages de manuel.<\/li>\n<li><strong><code>GetNtpPoolServers()<\/code><\/strong> : collecte les pools NTP et les configurations des serveurs \u00e0 partir des fichiers de configuration <code>chronyd<\/code>ou <code>timesyncd<\/code>.<\/li>\n<li><strong><code>GetNtpConfiguration()<\/code><\/strong> : affiche toutes les configurations du serveur, du pool et \u00e9ventuellement de la source pour aider \u00e0 valider les param\u00e8tres NTP.<\/li>\n<li><strong><code>IsChronySecurityConfigured()<\/code><\/strong> : v\u00e9rifie que les param\u00e8tres de s\u00e9curit\u00e9 de <code>chronyd<\/code>, tels que <code>keyfile<\/code>et <code>commandkey<\/code>, sont correctement d\u00e9finis.<\/li>\n<\/ul>\n<h2>Cas d&rsquo;utilisation potentiels<\/h2>\n<h3>\u00c9tude de cas\u00a0: gestion \u00e0 distance des correctifs<\/h3>\n<p>Un MSP g\u00e8re des syst\u00e8mes Linux sur plusieurs fuseaux horaires. Un correctif r\u00e9cent d\u00e9ploy\u00e9 via NinjaOne fait \u00e9tat d&rsquo;horodatages d&rsquo;installation incoh\u00e9rents. L&rsquo;administrateur d\u00e9ploie ce script en tant qu&rsquo;\u00e9tape de pr\u00e9-contr\u00f4le dans une strat\u00e9gie NinjaOne. Le script garantit que tous les syst\u00e8mes se synchronisent sur le m\u00eame pool NTP avant le d\u00e9but du processus de correction. Par cons\u00e9quent, les journaux s&rsquo;alignent correctement et les rapports sont exacts.<\/p>\n<h2>Comparaisons<\/h2>\n<table>\n<tbody>\n<tr>\n<td><strong>M\u00e9thode<\/strong><\/td>\n<td><strong>Avantages<\/strong><\/td>\n<td><strong>Limites<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>ntpdate<\/code> (ancien)<\/td>\n<td>Rapide, autonome<\/td>\n<td>Obsol\u00e8te, peu fiable<\/td>\n<\/tr>\n<tr>\n<td><code>chronyd\/systemd-timesyncd<\/code><\/td>\n<td>Moderne, s\u00fbr, persistant<\/td>\n<td>N\u00e9cessite une configuration ad\u00e9quate<\/td>\n<\/tr>\n<tr>\n<td><strong>Ce script<\/strong><\/td>\n<td>D\u00e9tection automatis\u00e9e, validation, contr\u00f4les de s\u00e9curit\u00e9<\/td>\n<td>Acc\u00e8s initial \u00e0 la racine n\u00e9cessaire<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Contrairement aux commandes ad hoc ou aux outils existants, ce script fournit une m\u00e9thode structur\u00e9e, tenant compte des erreurs et agnostique en mati\u00e8re de services, adapt\u00e9e \u00e0 un d\u00e9ploiement automatis\u00e9.<\/p>\n<h2>FAQ<\/h2>\n<p><strong>Q\u00a0: Que se passe-t-il si ni <\/strong><strong><code>chronyd<\/code><\/strong><strong>ni <\/strong><strong><code>systemd-timesyncd<\/code><\/strong><strong>n&rsquo;est install\u00e9\u00a0?<\/strong><strong><br \/>\n<\/strong>A\u00a0: Le script s&rsquo;arr\u00eate et conseille \u00e0 l&rsquo;utilisateur d&rsquo;installer l&rsquo;un des deux services.<\/p>\n<p><strong>Q\u00a0: Ce script peut-il \u00eatre programm\u00e9 via cron ou NinjaOne\u00a0?<\/strong><strong><br \/>\n<\/strong>A\u00a0: Oui, il est enti\u00e8rement compatible avec les plateformes d&rsquo;automatisation et les t\u00e2ches cron.<\/p>\n<p><strong>Q\u00a0: Cela va-t-il interf\u00e9rer avec ma configuration actuelle de synchronisation temporelle\u00a0?<\/strong><strong><br \/>\n<\/strong>A\u00a0: Non. Si un service de synchronisation temporelle \u00e9tait inactif avant l&rsquo;ex\u00e9cution du script, il sera \u00e0 nouveau arr\u00eat\u00e9 apr\u00e8s la synchronisation.<\/p>\n<p><strong>Q\u00a0: Peut-on l&rsquo;utiliser en toute s\u00e9curit\u00e9 sur des serveurs de production\u00a0?<\/strong><strong><br \/>\n<\/strong>A\u00a0: Oui, en supposant que le red\u00e9marrage des services n&rsquo;affecte pas les applications d\u00e9pendantes. Il est recommand\u00e9 de proc\u00e9der \u00e0 des tests en environnement de test.<\/p>\n<h2>Implications<\/h2>\n<p>Une mauvaise synchronisation de l&rsquo;heure peut entra\u00eener toute une s\u00e9rie de probl\u00e8mes\u00a0: \u00e9checs de connexion dus \u00e0 la non-concordance des jetons, <a href=\"https:\/\/www.ninjaone.com\/it-hub\/endpoint-security\/what-is-data-corruption\/\">journaux corrompus<\/a> et r\u00e9plication interrompue. En validant les fichiers de configuration, en v\u00e9rifiant la s\u00e9curit\u00e9 des services et en fournissant des journaux faciles \u00e0 auditer, ce script am\u00e9liore la fiabilit\u00e9 et la s\u00e9curit\u00e9 de l&rsquo;infrastructure. IL est particuli\u00e8rement important pour les syst\u00e8mes impliqu\u00e9s dans la gestion des identit\u00e9s, les communications s\u00e9curis\u00e9es et les environnements o\u00f9 la conformit\u00e9 est importante.<\/p>\n<h2>Recommandations<\/h2>\n<ul>\n<li><strong>Test de pr\u00e9-d\u00e9ploiement<\/strong>\u00a0: commencez toujours par ex\u00e9cuter le script dans un environnement de test.<\/li>\n<li><strong>Associez-le \u00e0 la surveillance<\/strong>\u00a0: utilisez NinjaOne ou des outils similaires pour contr\u00f4ler l&rsquo;\u00e9tat des services apr\u00e8s leur d\u00e9ploiement.<\/li>\n<li><strong>Incluez-le dans l&rsquo;image de base<\/strong>\u00a0: ajoutez ce script \u00e0 la configuration post-installation pour \u00e9viter toute d\u00e9rive future.<\/li>\n<li><strong>Sortie du journal<\/strong>\u00a0: redirigez la sortie du script vers un fichier journal \u00e0 des fins d&rsquo;audit (<code>. \/sync-time.sh &gt;&gt; \/var\/log\/time-sync.log<\/code>).<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>La synchronisation temporelle est essentielle \u00e0 l&rsquo;int\u00e9grit\u00e9 op\u00e9rationnelle \u00e0 tous les niveaux de l&rsquo;informatique. Ce script offre un moyen portable, conforme et \u00e0 toute \u00e9preuve de synchroniser l&rsquo;heure sous Linux \u00e0 l&rsquo;aide d&rsquo;un script shell. Int\u00e9gr\u00e9 \u00e0 des plateformes telles que NinjaOne, ce script permet aux professionnels de l&rsquo;informatique et aux MSP de maintenir l&rsquo;uniformit\u00e9 de leur flotte sans effort\u00a0: en am\u00e9liorant la pr\u00e9paration aux audits, la pr\u00e9cision des donn\u00e9es et la fiabilit\u00e9 du syst\u00e8me.<\/p>\n<p>Pour celles et ceux qui cherchent \u00e0 <strong>synchroniser l&rsquo;heure sur Linux \u00e0 l&rsquo;aide de scripts shell<\/strong>, cet outil est \u00e0 la fois robuste et facile \u00e0 automatiser. En l&rsquo;int\u00e9grant dans les processus du cycle de vie, vous pouvez garantir un horodatage coh\u00e9rent et s\u00e9curis\u00e9 dans l&rsquo;ensemble de votre infrastructure.<\/p>\n","protected":false},"author":35,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4211],"use_cases":[4281],"class_list":["post-508691","script_hub","type-script_hub","status-publish","hentry","script_hub_category-linux","use_cases-configuration-generale"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub\/508691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/comments?post=508691"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/media?parent=508691"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/operating_system?post=508691"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/use_cases?post=508691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}