NTP configuration with Elemental

Overview

The default OS channel shipped with Elemental provides NTP support via systemd-timesyncd.

This page covers configuring systemd-timesyncd with the provided SLE Micro images, which is pre-configured with some default (fallback) NTP servers: ([0-3].suse.pool.ntp.org).

The easiest way to configure a specific NTP server, is to drop a configuration file in the /etc/systemd/timesyncd.conf.d directory. The directory and the configuration file should be accessible by the systemd-timesync user.

Configure a static NTP server

The NTP configuration can be provided via a cloud-config snippet added to the MachineRegistration configuration.

We will need to:

  • ensure the timesyncd.conf.d directory can be read by the systemd-timesync user
  • write the custom config file in the timesyncd.conf.d directory
  • restart the systemd-timesyncd service to use the new configuration

As an example, let’s see how to configure ntp.ripe.net as the primary NTP server (lines 6-14):

  1. config:
  2. cloud-config:
  3. users:
  4. - name: root
  5. passwd: root
  6. write_files:
  7. - content: |
  8. [Time]
  9. NTP=ntp.ripe.net
  10. path: /etc/systemd/timesyncd.conf.d/custom-ntp.conf
  11. permissions: 644
  12. runcmd:
  13. - chmod 755 /etc/systemd/timesyncd.conf.d
  14. - systemctl restart systemd-timesyncd
  15. elemental:
  16. install:
  17. device: /dev/vda
  18. reboot: true
  19. machineInventoryLabels:
  20. element: fire

Configure NTP from DHCP

In order to get the NTP server from the network via the NTP DHCP option, we need a NetworkManager dispatcher script to reconfigure dynamically the systemd-timesync service when needed.

We will have both to:

  • provide the dispatcher script which creates and deletes the systemd-timesyncd config files
  • enable the NetworkManager-dispatcher service

See lines 6-34 in the following MachineRegistration configuration example:

  1. config:
  2. cloud-config:
  3. users:
  4. - name: root
  5. passwd: root
  6. write_files:
  7. - content: |
  8. #! /usr/bin/bash
  9. [ -n "$CONNECTION_UUID" ] || exit
  10. INTERFACE=$1
  11. ACTION=$2
  12. case $ACTION in
  13. up | dhcp4-change | dhcp6-change)
  14. [ -n "$DHCP4_NTP_SERVERS" ] || exit
  15. mkdir -p /etc/systemd/timesyncd.conf.d/
  16. cat<<EOF > /etc/systemd/timesyncd.conf.d/$CONNECTION_UUID.conf
  17. [Time]
  18. NTP=$DHCP4_NTP_SERVERS
  19. RootDistanceMaxSec=15
  20. EOF
  21. systemctl restart systemd-timesyncd
  22. ;;
  23. down)
  24. rm -f /etc/systemd/timesyncd.conf.d/$CONNECTION_UUID.conf
  25. systemctl restart systemd-timesyncd
  26. ;;
  27. esac
  28. path: /etc/NetworkManager/dispatcher.d/10-update-timesyncd
  29. permissions: 700
  30. runcmd:
  31. - systemctl enable NetworkManager-dispatcher
  32. elemental:
  33. install:
  34. device: /dev/vda
  35. reboot: true
  36. machineInventoryLabels:
  37. element: fire