Skip to content

Instantly share code, notes, and snippets.

@mayankchhabra
Last active April 21, 2024 14:32
Show Gist options
  • Save mayankchhabra/a42999f49d7b9712c6c5100965fa05bb to your computer and use it in GitHub Desktop.
Save mayankchhabra/a42999f49d7b9712c6c5100965fa05bb to your computer and use it in GitHub Desktop.
Use Cloudflare NTP server on umbrelOS
#!/bin/bash
# Ensure the script is run as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Current date is: $(date -R)"
# Update packages
echo "Updating repo..."
apt-get update -qq > /dev/null 2>&1
echo "Removing ntp..."
apt-get --yes -qq remove ntp > /dev/null 2>&1
# Install systemd-timesyncd
echo "Installing systemd-timesync..."
if ! apt-get --yes -qq install systemd-timesyncd > /dev/null 2>&1; then
echo "Failed to install systemd-timesyncd, reinstalling ntp"
apt-get --yes qq install ntp
exit
fi
echo "Enabling timesyncd..."
timedatectl set-ntp true
systemctl enable --now systemd-timesyncd
# Configure systemd-timesyncd to use time.cloudflare.com
echo "Setting default NTP server..."
# Clean up existing NTP and FallbackNTP entries
sed -i '/^NTP/d' /etc/systemd/timesyncd.conf
sed -i '/^FallbackNTP/d' /etc/systemd/timesyncd.conf
sed -i '/^ConnectionRetrySec/d' /etc/systemd/timesyncd.conf
echo "NTP=time.cloudflare.com" >> /etc/systemd/timesyncd.conf
echo "FallbackNTP=time.google.com 0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org" >> /etc/systemd/timesyncd.conf
echo "ConnectionRetrySec=10" >> /etc/systemd/timesyncd.conf
# Restart timesyncd to apply changes
echo "Restarting timesyncd..."
systemctl restart systemd-timesyncd
# Reboot the system
# echo "Rebooting the system now..."
# reboot
echo "Date is: $(date -R)"
timedatectl status
echo "Connecting to the new NTP server..."
sleep 5s
echo "Date is: $(date -R)"
timedatectl timesync-status
echo "Refetching status..."
sleep 5s
echo "Date is: $(date -R)"
timedatectl timesync-status
echo "Successfully switched the NTP server."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment