Files
ntp-client-ubuntu/ntp-client-install
2025-10-23 15:41:31 +02:00

86 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Author : Allan Christensen
# First Created : 05012022 (DD-MM-YYYY)
# Description : Installs Ntp client on Ubuntu 24.04
# License : MIT License (see LICENSE file for details)
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo ""; exit 1; fi
#
# Install and enable NTP
#
apt install -y ntp ntpdate && systemctl enable ntp
#
# Disable systemd-timesyncd NTP
#
timedatectl set-ntp off
#
# Sync time once
#
ntpdate -u 0.pool.ntp.org
#
# Determine which NTP config file to use
#
# Default NTP config file
conf_file="/etc/ntp.conf"
#
# If ntpsec is installed, override with ntpsec's config file
#
if [ -f /etc/ntpsec/ntp.conf ]; then conf_file="/etc/ntpsec/ntp.conf" ; fi
#
# Backup existing config file
#
backup_file="${conf_file}.bak"; cp "$conf_file" "$backup_file"
#
# Define NTP servers
#
ntp_servers="server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org"
#
# Comment out existing pool servers
#
sed -i '/pool.ntp.org/ s/^/# /g' "$conf_file"
#
# Add the new defined servers
tmp_file="/tmp/ntpconf" ; printf "%s\n" "$ntp_servers" > "$tmp_file"
# Insert after a line containing "Specify one" if it exists else append at the end
if grep -q "Specify one" "$conf_file"; then
sed -i "/Specify one/r $tmp_file" "$conf_file"
else
cat "$tmp_file" >> "$conf_file"
fi
#
# Restart NTP service
#
systemctl restart ntp
#
# Clean up
#
rm -f "$tmp_file"
#
# All done
#
printf "\nAll Done...\n"
#
# End of script
#