Setting time and date without using NTP

11,839

If you don't wish to use the NTP command, see if this works as an alternative for you:

date -s "$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g') -0500"

**Note: The time pulled from Google, is in GMT, so the -0500 represents the numeric timezone you are in. For me I am in US/EST so that is -0500, please change yours to match to your respective timezone, and it should fix any 'local time offsets' you might be experiencing.

If it isn't sticking every time you log out and log back in, you can try setting this in your .bash_profile or /etc/profile so that it runs first every time you log in, it isn't a 'fix' but more of a 'hack'.

Share:
11,839

Related videos on Youtube

Sharista
Author by

Sharista

Updated on September 18, 2022

Comments

  • Sharista
    Sharista over 1 year

    I have a "windriver" linux based Wimax ASN, and when ever I login, I find the date and time is wrong (example: "15 jul 2010").

    Whenever I set both the date and time with the date command, or the hwclock, it returns back to old state "15 jul 2010" right after I log out and in again, without even restarting the device.

    Is there any way to permanently set them both without using NTP?

  • Jeff Schaller
    Jeff Schaller over 8 years
    If you want the exact time, you should use a time service, like NTP.
  • Sharista
    Sharista over 8 years
    Hi Jeff, i mean setting it with the same command that Dev sent if possible, because all fine now regarding date and time , both been saved well , but i have a drift in some hours:minuts cause i did not enter time in Dev's command.
  • devnull
    devnull over 8 years
    @Sharista, do you just not want to use the ntpd but are ok with ntpdate or do you not wish to use either? Because if you are ok with using ntpdate, you can just cron something like 0 0 * * * /path/to/ntpdate <ntpserver>
  • Sharista
    Sharista over 8 years
    @DevNull i do not want to use ntpd at all, i mean setting date and time manually, the date now is good thanks to u, but with wrong time(shifted some hours cause when i ran ur line , i was almost 9 hours past 00:00:00).
  • devnull
    devnull over 8 years
    @Sharista, I updated my post, see if that helps. If I understand you correctly, the drift you are experiencing might be caused by the fact that the time being pulled from the google headers is in GMT and not in your local time zone, so by adding your GMT offset to the command you should get the correct local time. Hope that helps.
  • Ulises Sánchez
    Ulises Sánchez over 8 years
    rdate. time honored method of syncing system time. here's what i do at reboot, but you need to choose your own sensible time server: sudo rdate time-c.timefreq.bldrdoc.gov && sudo hwclock -w