Icono del sitio Binario 0

Automatización de la configuración de sincronización horaria en sistemas Linux con NTP o Chrony

Artículos Guías Manuales Sistemas Linux Windows Redes MySql Binario 0 Binario Cero

Artículos Guías Manuales Sistemas Linux Windows Redes MySql Binario 0 Binario Cero

En entornos empresariales, la correcta sincronización horaria de los sistemas es crucial para mantener la coherencia en registros, autenticaciones, y procesos distribuidos. Este artículo presenta dos scripts automatizados, uno para sistemas Debian/Ubuntu y otro para CentOS/RHEL, que detectan si el sistema usa NTP o Chrony y configuran el servicio adecuadamente utilizando un servidor de tiempo genérico (time.example.com).

¿Qué hacen los scripts?

Ambos scripts:

Script para Debian/Ubuntu

#!/bin/bash

DC_SERVER="time.example.com"
NTP_CONF="/etc/ntp.conf"
CHRONY_CONF="/etc/chrony/chrony.conf"

check_installed() {
dpkg -l | grep -qw "$1"
}

configure_ntp() {
echo "Configurando NTP..."
sed -i 's/^server/#server/g' $NTP_CONF
echo "server $DC_SERVER iburst" >> $NTP_CONF
cat <<EOF >> $NTP_CONF

restrict default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
EOF
systemctl enable ntp
systemctl restart ntp
}

configure_chrony() {
echo "Configurando Chrony..."
sed -i 's/^pool/#pool/g' $CHRONY_CONF
sed -i 's/^server/#server/g' $CHRONY_CONF
echo "server $DC_SERVER iburst" >> $CHRONY_CONF
systemctl enable chrony
systemctl restart chrony
}

echo "Comprobando si NTP o Chrony están instalados..."

if check_installed ntp; then
echo "NTP ya está instalado."
configure_ntp
elif check_installed chrony; then
echo "Chrony ya está instalado."
configure_chrony
else
echo "No hay NTP ni Chrony. Instalando NTP..."
apt update && apt install -y ntp
configure_ntp
fi

echo "Verificando sincronización..."
timedatectl status
ntpq -p || chronyc sources -v

Script para CentOS/RHEL

#!/bin/bash

DC_SERVER="time.example.com"
NTP_CONF="/etc/ntp.conf"
CHRONY_CONF="/etc/chrony.conf"

check_installed() {
rpm -q "$1" &>/dev/null
}

configure_ntp() {
echo "Configurando NTP..."
sed -i 's/^server/#server/g' $NTP_CONF
echo "server $DC_SERVER iburst" >> $NTP_CONF
cat <<EOF >> $NTP_CONF

restrict default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
EOF
systemctl enable ntpd
systemctl restart ntpd
}

configure_chrony() {
echo "Configurando Chrony..."
sed -i 's/^pool/#pool/g' $CHRONY_CONF
sed -i 's/^server/#server/g' $CHRONY_CONF
echo "server $DC_SERVER iburst" >> $CHRONY_CONF
systemctl enable chronyd
systemctl restart chronyd
}

echo "Comprobando si NTP o Chrony están instalados..."

if check_installed ntp; then
echo "NTP ya está instalado."
configure_ntp
elif check_installed chrony; then
echo "Chrony ya está instalado."
configure_chrony
else
echo "No hay NTP ni Chrony. Instalando NTP..."
yum install -y ntp
configure_ntp
fi

echo "Verificando sincronización..."
timedatectl status
ntpq -p || chronyc sources -v

Consideraciones finales

Estos scripts son útiles en despliegues automatizados, entornos de configuración masiva o como parte de sistemas de provisión como Ansible o scripts de inicio.

Salir de la versión móvil