Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
1  
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

3 Answers 3

up vote 19 down vote accepted

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.

share|improve this answer
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
    
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

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 :)

share|improve this answer
    
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
    
Thanks. I updated it :-) –  Little Jawa Nov 10 '10 at 6:48

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
share|improve this answer
    
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
    
Why use at? And if you're going to automatically run ntpdate, why not just install the ntp service? –  Reinier Post Aug 27 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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