Setup time sync at startup as a service / script on Raspberry Pi running Ubuntu Server



If you have a Raspberry Pi and you have installed Ubuntu Server on it you will notice that unless you’re forcing a manual NTP sync on the device it will never get the correct time. Not having the correct time on a server can cause all sorts of issues with making secure connections to running jobs at the wrong time etc. In this guide I will show you how to setup a startup script if you are running 20.04 or later, as with later releases of Ubuntu rc.local has been deprecated and not recommended to use.


Depending on your setup you may need an IP address for the NTP server.

Check if you need to do this

  1. Open a connection to the RasPi just after a reboot
  2. run a ping command to see if you get an IP address:
    ping pool.ntp.org
  3. If you get an IP address then great you do not need to follow this guide, otherwise run the same ping on an internet connected computer. Make sure you select an NTP server in your area that you know has a static IP address, or get a few of them using the ping command above
Now that we have our list of IP addresses that we can query we can resume looking setting up the startup script.

Install chrony

  1. Run this command to install chrony
    sudo apt install chrony
  2. Restart the chrony services: 
    sudo systemctl restart chrony

Create Script and Test

  1. Check with one of your NTP IP addresses if the service is working (replace the [] with the ip address of your server): 
    sudo chronyd -q 'server [your.ntp.ip.address] iburst'
  2. Create the service file: 
    sudo nano /etc/systemd/system/sync-time-startup-ip.service
  3. Paste this text into the service file: 
    [Unit]
    After=network.service

    [Service]
    ExecStart=/usr/local/bin/sync-time-startup-ip.sh

    [Install]
    WantedBy=default.target
  4. Change the file permissions on the service: 
    sudo chmod 664 /etc/systemd/system/sync-time-startup-ip.service
  5. Create the script file: 
    sudo nano /usr/local/bin/sync-time-startup-ip.sh
  6. Paste this text into the script, replace the [] with the ip address of your server, if you have multiple in the list put them one after the other: 
    #!/bin/bash

    chronyd -q 'server [your.ntp.ip.address] iburst'
    chronyd -q 'server [your.ntp.ip.address] iburst'
  7. Change the file permissions on the script: 
    sudo chmod 744 /usr/local/bin/sync-time-startup-ip.sh
  8. Test the script: 
    sudo /usr/local/bin/sync-time-startup-ip.sh
  9. I did not put any logic in there to check if one server is down, use the other server, which is something if you feel like doing you can research. You can configure your NTP servers to obtain time from other sources periodically, this script is just to get you going so you have the correct time initially
  10. Activate the service: 
    sudo systemctl daemon-reload: 
    sudo systemctl enable sync-time-startup-ip.service
  11. Once you are ready to test run a reboot and reconnect to your box when it comes back up: 
    sudo reboot now
  12. You can check the time with this command to verify that it is now getting the correct time, it make take a few seconds to start showing as your script will be running in the background: 
    date

Congratulations you should now have a mini server that behaves as you would expect! Feel free to submit a feature request to Ubuntu to have them set an option to sync time at startup.


Sources:
Linuxconfig.org
opensource.com

Comments