23

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
    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 '11 at 9:30
  • Here is one that explains how to set it to time.nist.gov: askubuntu.com/questions/972799/… – SDsolar Nov 4 '17 at 8:05
32

This is done with NTP, for which instructions are available. Basically, you'll need to install an NTP daemon. There seem to be 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.

EDIT: I missed "at startup." It's generally recommended to use ntpd to get continuous synchronization between your system and the server, but if you really only want to synchronize once, mfisch's answer seems to be what you're looking for.

  • 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 '10 at 1:02
  • I'm okay with it running continuously. – Nathan Osman Jul 29 '10 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 '10 at 2:01
  • For Ubuntu 18.04 the advice to install ntpd longer applies. By default systemd-timesyncd.service will synchronize the time. – ernstkl Mar 21 at 14:01
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 :)

  • 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. – Little Jawa Jul 29 '10 at 8:50
  • Isn't it labeled "Time and Date"? – Firefeather Nov 8 '10 at 0:00
  • @Firefeather: it may well be. I don't have an english interface, so I can't tell ;) – Little Jawa Nov 8 '10 at 6:41
  • Ah, silly me, forgetting about the internationalization. My en-us system labels it "Time and Date". – Firefeather Nov 9 '10 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
  • 1
    iirc You can't set suid on .sh scripts. – Broam Jul 29 '10 at 1:03
  • Can you do the suid on ntpdate directly then? That would probably accomplish the same thing. – mfisch Jul 29 '10 at 2:00
  • 2
    Why use at? And if you're going to automatically run ntpdate, why not just install the ntp service? – Reinier Post Aug 27 '14 at 12:07
  • 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. – Corey Goldberg Jun 10 '17 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.