Fixing clock skew problems in GNU/Linux

I ran into a bit of trouble recently on my new Gentoo GNU/Linux laptop because I accidentally set the date a whole month in the future, and then proceeded to install lots of packages before realizing my mistake. I corrected the date, but then I was left with a slew of clock skew problems. The problems occur during boot init scripts, which complain that configuration files were created or modified in the future. This is little more than an annoyance, but the clock skew most definitely did mess up my attempted installation of VMware. Make uses files’ modified times to determine if they should be recompiled; obviously, any file seeming to date from the future won’t be recompiled. This will affect package managers like portage or apt-get.

The simplest solution would be to wait out the extra time of the skew so that it doesn’t appear that files were created in the future. This is okay when you’re dealing with broken timezone settings, which can’t make the files appear to be from more than a day in the future, but it’s not good for dealing with skews of a month. I simply can’t wait that long to install new software. So I came up with a better solution. This fix should work for any GNU/Linux system:

(as root)

$ cd /
$ touch currtime
$ find . -cnewer /currtime -exec touch {} \;

The first two steps involve going to the root of the filesystem and creating a dummy file called currtime. This file is used solely for its timestamp, which will effectively represent the present. Any file with a modified time after currtime appears to be modified in the future, which the third line then corrects. The program find has a bunch of parameters that can be used to specify how recent a file is (e.g. “modified within last X hours”), but they can’t seem to take a negative number as a parameter (which would indicate modification in the future) because the negative sign is interpreted as a parameter signal. Hence why I’m bothering with the dummy file currtime; it’s simply easiest to do the time calculations by comparing modified times between files.

One last thing to explain: the program touch has two separate functionalities. If called with a filename that doesn’t yet exist, it will create that file will null contents, which is what we use to create currtime. And if the file does exist, it will “touch” the file, meaning that the modified time is updated to the present but no changes are made to the actual contents of the file. So the full script works like this: make a dummy file, search the entire filesystem for anything that appears to have been modified more recently than the dummy file, and touch all these files to make them appear to have been modified in the present.

You will get some errors from non-touchable parts of your system, like devices, pseudo files in /proc, etc. Don’t worry about them. Their modified times can’t be set, so you won’t do any harm, and they don’t need any fixing. The last thing to do is to cleanup your root directory by removing the dummy file currtime. And that’s it — the clock skew is totally corrected.

14 Responses to “Fixing clock skew problems in GNU/Linux”

  1. Nick Says:

    Wow, thanks, this solution is simple and ideally fit my situation!

  2. Cyde Weys Says:

    Glad it helped someone! That’s why I put it online after I spent the time figuring it out myself. May I ask how you found this? Google?

  3. Andres101 Says:

    I found this via google search terms: touch files modified in future

  4. mordjah Says:

    thanks for an elegant solution. the google string i used was “gentoo fixing clock skew” and this was number 3.

  5. drew Says:

    Very creative and VERY helpful :]

  6. Jeff Says:

    This wouldn’t have happened using *BSD! Just kidding. Great tip.

    Maybe post on the Gentoo forums?

    :-)

    Cheers, Happy New Year!

  7. Not-So-Anonymous Coward Says:

    This saved my life.

    P.S. This was the first result when I searched for “fix clock skew” in google.

  8. gothmgr Says:

    sweet xD worked like a charm! thanks alot
    google ftw

  9. math3z Says:

    Hi Cyde Weys, first of all I must admit that I’ve found your website really accidentaly (through links browser)… and second you saved me before going mad =) As I am only unexperienced GNU/Linux user it was my first kernel & Gentoo compilation and my nerves almost collapsed when I encountered so many serious errors… totally broken terminal window?! Oh s**t what’s up.. Finally I managed to repair that with “reset” command. And after that it appeared.. that filthy-little annoying clock skew notification. So once again many thanks and sorry for a bit off-topic post.

  10. nico Says:

    Thanks, you saved me from headache :) Simple and efficient, i like it.

  11. Kjartan Says:

    THANKS! Indeed worth the caps-lock!

    from #gentoo @ freenode

  12. Barutan Seijin Says:

    Nice. One note: I had to use newer rather than cnewer, i.e.,

    find . -newer ./file_with_correct_date -exec touch {} \;

  13. Brent Says:

    Thankyou thankyou thankyou thankyou thankyou…. THANKYOU!!!!!!

  14. north face jackets for men Says:

    Hey there I am so thrilled I found your blog page, I really found you by accident, while I was browsing on Yahoo for something else, Anyways I am here now and would just like to say many thanks for a tremendous post and a all round exciting blog (I also love the theme/design), I don? have time to look over it all at the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job.