26

I would like Ubuntu to automatically synchronize my system clock with a timeserver at startup.

However, my PC isn't connected to the Internet until after I've logged in (plus 5 - 10 seconds for good measure).

How can I set it to do this?

2
  • 2
    There are already excellent answers here that explain how to install ntpd so that your computer continuously keeps itself in sync with internet time servers, but I just wanted to add that by default Ubuntu will synchronize time with ntp.ubuntu.com whenever your network interface comes "up" (e.g. when you start your computer). This does not appear be happening in your case because of the way you connect to the internet, but other readers should not assume that Ubuntu does not sync time without ntpd being installed.
    – user8979
    Feb 27, 2011 at 9:30
  • Here is one that explains how to set it to time.nist.gov: askubuntu.com/questions/972799/…
    – SDsolar
    Nov 4, 2017 at 8:05

4 Answers 4

34

It's generally recommended to run a service that uses NTP (Network Time Protocol) to regularly synchronize your computer's clock with a server. In recent versions of Ubuntu (at least since 18.10, or possibly earlier but I'm not sure), this is taken care of by the systemd-timesyncd service, which is installed and enabled by default, so there's no need to do anything special. If the service is available and active, running

timedatectl status

should tell you so.

For older versions of Ubuntu, you can follow instructions to set up an NTP daemon. There are several choices available but the "standard" one is in the package ntp. According to the instructions at the linked page,

sudo apt-get install ntp

will get everything set up to synchronize with Ubuntu's NTP server.

If you really do only want to synchronize the time once at startup and never again (until the next startup), see e.g. mfisch's answer. But again, this is not recommended and there's rarely any reason it would be beneficial.

7
  • 3
    I think in general he'd be better off just having ntpd always running and just having it fail to connect when he was not online, that is by no means harmful. With that said, my method should also work ;)
    – mfisch
    Jul 29, 2010 at 1:02
  • I'm okay with it running continuously. Jul 29, 2010 at 1:35
  • 1
    George, just do that. I was hoping to answer your "real" question since the most annoying thing in the world is asking a question like this and having all the responses be "why do you want to do that?"
    – mfisch
    Jul 29, 2010 at 2:01
  • 1
    For Ubuntu 18.04 the advice to install ntpd longer applies. By default systemd-timesyncd.service will synchronize the time.
    – ernstkl
    Mar 21, 2019 at 14:01
  • 1
    "should tell you so" what does this mean? I am having trouble with the wrong time (Universal time is of by 2 hours) and don't know how to get it to sync. For me it reads "System clock synchronized: no \n NTP service: active " (Ubuntu 20.04 LTS)
    – Kvothe
    Apr 1, 2021 at 13:39
9

If you go to "System->Administration->Time and Date", you will get a GUI to set the date/time.

An option is provided for using time servers. If you check it and NTP is not installed, it will ask if you want to install it. Just click "yes", and let it do its job :)

4
  • Forgot the "startup" thing. NTP does its job regularly, which means that you should get synchronized whenever you are online... But the only way to make sure is to use some kind of script as provided by mfisch. Now to be honest : once synchronized, there's little chance that your computer gets messed up on its clock. So if you use the default time synchronization, and let it do it in background, you should be good to go most of the time, without having to do anything more. Jul 29, 2010 at 8:50
  • Isn't it labeled "Time and Date"? Nov 8, 2010 at 0:00
  • @Firefeather: it may well be. I don't have an english interface, so I can't tell ;) Nov 8, 2010 at 6:41
  • Ah, silly me, forgetting about the internationalization. My en-us system labels it "Time and Date". Nov 9, 2010 at 4:39
6

You can do this using at and ntpdate. at is probably already installed, but ntpdate may not be. (apt-get install ntpdate).

First create a small script that runs ntpdate, lets call it update_time.sh.

#!/bin/bash
ntpdate pool.ntp.org

In your .bash_login file (which you may need to create) add this:

at -f ~/update_time.sh now + 1 minute

That should do what you want. You can change the delay that at uses to be 5 minutes, 10 minutes etc.

EDIT: I just realized that you'll need to be root to run ntpdate. You'll need to set the SUID bit on the update_time.sh script that I mentioned. You can do that by running this from the command (only needs to be run once):

sudo chmod 4711 update_time.sh
sudo chown root update_time.sh
4
  • 1
    iirc You can't set suid on .sh scripts.
    – Broam
    Jul 29, 2010 at 1:03
  • Can you do the suid on ntpdate directly then? That would probably accomplish the same thing.
    – mfisch
    Jul 29, 2010 at 2:00
  • 2
    Why use at? And if you're going to automatically run ntpdate, why not just install the ntp service? Aug 27, 2014 at 12:07
  • 1
    note: ntpdate is no longer installed by default on Ubuntu (16.04+) and was deprecated in favor of timedatectl. timedatectl controls systemd's timesyncd service, which is used by default for time synchronization. Jun 10, 2017 at 23:42
0

Since Ubuntu 16.04 timedatectl / timesyncd (which are part of systemd) replace most of ntpdate / ntp. See Time Synchronization.

You might need to activate time synchronization using:

$ sudo timedatectl set-ntp on

If you want to use chrony:

  • If you require a one-shot sync use: $ chronyd -q

  • If you require a one-shot time check, without setting the time use: $ chronyd -Q

  • For continuous syncing, the recommended solution is chrony:

chrony(d)

The NTP daemon chronyd calculates the drift and offset of your system clock and continuously adjusts it, so there are no large corrections that could lead to inconsistent logs for instance. The cost is a little processing power and memory, but for a modern server this is usually negligible. Installation

To install chrony, from a terminal prompt enter:

$ sudo apt install chrony

You might also need to activate

sudo timedatectl set-ntp on

Update: Another method if the above doesn't work is to set a cron job to run $ chronyd -q

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .