Configurar ntp
El NTP es un servicio que si bien en muchas máquinas no es imprescindible, si que es conveniente que en una red, todas las máquinas usen la misma base de tiempo y esté bien sincronizada. No sólo es imprescindible en clusters, también para la revisión de logs, y saber lo que está pasando en nuestra red, es muy conveniente tener la misma hora en todas.
Instalamos y configuramos el servicio NTP
apt-get -y install ntp apt-get -y install ntpdate service ntp restart systemctl enable ntp
Ahora configuramos la zona horaria
Esto podemos realizarlo de dos formas:
cp /usr/share/zoneinfo/Europe/Madrid /etc/localtime
O bien con el comando
dpkg-reconfigure tzdata
Ahora podemos editar el fichero /etc/ntp.conf, para comprobar la configuración
nano /etc/ntp.conf
El fichero tiene el siguiente contenido. La parte #server ntp.your-provider.example es en la que añadimos a la configuración en nuestro caso dos servidores hora.roa.es y hora.rediris.es Los pools, (# pool: <http://www.pool.ntp.org/join.html>) los podemos modificar a nuestra voluntad, bien con servidores o con pools de NTP
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help driftfile /var/lib/ntp/ntp.drift # Enable this if you want statistics to be logged. #statsdir /var/log/ntpstats/ statistics loopstats peerstats clockstats filegen loopstats file loopstats type day enable filegen peerstats file peerstats type day enable filegen clockstats file clockstats type day enable # You do need to talk to an NTP server or two (or three). #server ntp.your-provider.example ################################################################## ## Agregamos nuestros servidores NTP Por lo general es recomendable usar los de RedIris ## hora.roa.es y hora.rediris.es ################################################################## server hora.roa.es server hora.rediris.es # pool.ntp.org maps to about 1000 low-stratum NTP servers. Your server will # pick a different set every time it starts up. Please consider joining the # pool: <http://www.pool.ntp.org/join.html> pool 0.debian.pool.ntp.org iburst pool 1.debian.pool.ntp.org iburst pool 2.debian.pool.ntp.org iburst pool 3.debian.pool.ntp.org iburst # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery limited restrict -6 default kod notrap nomodify nopeer noquery limited # Local users may interrogate the ntp server more closely. restrict 127.0.0.1
Iniciamos el servicio NTP
service ntp restart
Y configuramos el servicio para que se inicie al arrancar la máquina
systemctl enable ntp
Verificar la sincronización del servidor
Para verificar si tras el reinicicio del proceso ntp nuestro servidor NTP está sincronizando su hora local con el pool de servidores NTP de Internet, ejecutaremos el siguiente comando:
ntpq -p
Nos dará un resultado parecido a este:
root@mail:~# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000 hora.roa.es .INIT. 16 u - 64 0 0.000 0.000 0.000 hora.rediris.es .INIT. 16 u - 64 0 0.000 0.000 0.000
Las columnas indican lo siguiente:
- remote - Indica los servidores de ntp definidos en el fichero ntp.conf. '*' indica que es el servidor actual y el mejor origen de sincronización
- '+' indica que son servidores disponibles de NTP. Los orígenes con - son considerados como no usables.
- refid - La dirección IP del servidor NTP del cual se obtiene la hora.
- st - Stratum
- t - Tipo. 'u' para unicast. Existen otros valores como por ejemplo local, multicast, broadcast...
- when - Tiempo transcurrido (en segundos) desde el último contacto con el servidor NTP.
- poll - Frecuencia de sondeo con el servidor en segundos.
- reach - Un valor en octal que indica cuando hay algún tipo de error en la comunicación con el servidor. El valor 377 indica 100% de éxitos.
- delay - El 'round trip' entre nuestro servidor y el servidor remoto.
- offset - La diferencia de tiempo entre nuestro equipo local y el equipo remoto en milisegundos.
- jitter - La media de tiempos en milisegundos entre dos muestras.
Problemas con el servicio NTP
A veces hay problemas con el inicio del servicio NTP por ejemplo ene Ubuntu 18.04 LTS. Está habilitado, pero inactivo, así que hay que iniciarlo manualmente después de cada reinicio.
systemctl status ntp.service ● ntp.service - Network Time Service Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: man:ntpd(8)
La razón está en un conflicto con el servicio systemd-timesyncd.service, que es parte de systemd. Después de deshabilitarlo, ntp.service arrancará sin problemas.
systemctl show ntp.service | grep Conflicts Conflicts=shutdown.target systemd-timesyncd.service
systemctl status systemd-timesyncd.service ● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-07-12 05:17:21 UTC; 18min ago
Para deshabilitarlo bastará con ejecutar
systemctl disable systemd-timesyncd.service