1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

NTP server settings

Discussion in 'HyperWRT Firmware' started by slinde, Dec 31, 2005.

  1. slinde

    slinde Network Guru Member

    I am new at Linksys routers but have UNIX/network experience.
    I have a WRT54G v 2.2 running HyperWRT 2.1b1 +tofu10.

    It is not connected to anything on the WAN port. I use it for a wds link to another WRT54.

    On the local network I have a NTP server working. I want the WRT54 to synchronize time with the NTP server. I have set the nvram settings as below, but it does not work. What am i missing?

    # nvram show | grep ntp
  2. slinde

    slinde Network Guru Member

    Here's an update:

    I found out that I can sync to the NTP server by issuing following command:
    # usr/sbin/ntpclient
    mini ntpclient
    trying []
    Time updated.

    Which raises some more questions...
    1. I guess this is a one-time sync? I would prefer a continous syncing to the NTP server.
    2. I have some timezone issue. The time displayed is one hour off from local time. My timezone and daylight settings seems to be OK. Does anybody know the exact syntax of the "time_zone" nvram variable?
    3. How about the nvram variables, if I change them do I need to reboot the router for them to take effect?

    I still would be interested to know if there is a "recommended" way to do the NTP settings... ;-)

    Happy New Year to all of you Linksys hackers out there!
  3. mattiL

    mattiL Network Guru Member

    A tip from another beginner

    Perhaps a cron task would do the trick?
    Add a file in /tmp/cron.d/ named for example "ntptime" containing something like below:

    */2 * * * * root /usr/sbin/ntpclient .

    But you need to change the interval (that's the stars in the line).
    So check out the cron manpages in linux or the web.

    http://www.adminschoice.com/docs/crontab.htm#Crontab file

  4. slinde

    slinde Network Guru Member

    Thanks for your tip!
    I have been thinking about using cron but have not studied the details, now you gave them to me. :) Thank you!

    But I still would like to know how this is supposed to work. Since there is an entry in the web setup GUI for timezone, there must be some mechanism which is supposed to set the time?
    The best I think would be to use that mechanism with as few modifications as possible. Otherwise it feels like inventing the wheel all over again...

  5. tofu

    tofu Network Guru Member

    Time not updating if WAN=disabled is a bug that has been fixed, but not yet released.

    ntpclient simply gets the UTC time and exits. It doesn't support the odd time_zone format used by Linksys. You may be able to get around it if you set time_zone to TZ format (try googling gnu tz). For example: time_zone=PST8 for California. time_zone for some reason sets the TZ env even though it's not the same, but you may be able to use it for good here. :) You may need to reboot after setting it...
  6. mattiL

    mattiL Network Guru Member

    A quick look in the code (part of /sbin/rc ) reveals the following:

    -ntp is called from ntp_main and process_monitor_main once per hour.
    -I works on WAN (check_wan_link)

    ntp.c (HyperWRT 201205)

    int do_ntp(void) // called from ntp_main and process_monitor_main; called every hour
    struct timeval tv;
    float fofs;
    int dst, i;
    char *servers;

    if (!nvram_match("ntp_enable", "1")) return 0;

    if (sscanf(nvram_safe_get("time_zone"), "%f %*d %d", &fofs, &dst) != 2) return 1; // OFS[.5] UNK DSTIDX
    if (((i = atoi(nvram_safe_get("dstcode"))) > 0) && (i <= 5)) dst = i;
    if (!nvram_match("daylight_time", "1")) dst = 0;

    if ((!nvram_match("ntp_mode", "manual")) || ((servers = nvram_get("ntp_server")) == NULL) || (*servers == 0))
    servers = " pool.ntp.org";

    char *argv[] = {"ntpclient", servers, NULL};
    if (_eval(argv, NULL, 20, NULL) != 0) {
    nvram_set("timer_interval", NTP_N_TIMER);
    return 1;

    // -- probably should move to ntpclient

    gettimeofday(&tv, NULL);
    tv.tv_sec += (int)(fofs * 3600); // <-- cast it or this will be off (?)

    if ((dst >= 1) && (dst <= 5)) {
    struct tm *tm = localtime(&tv.tv_sec);
    int mon = tm->tm_mon + 1;
    int day = tm->tm_mday;
    int yi = tm->tm_year + 1900 - 2002; // dst table starts at 2002
    int mbeg = dstEntry[dst].startMonth;
    int mend = dstEntry[dst].endMonth;
    int dbeg = dstEntry[dst].startDay[yi];
    int dend = dstEntry[dst].endDay[yi];

    if (((mon == mbeg) && (day >= dbeg)) ||
    ((mon == mend) && (day <= dend)) ||
    ((mbeg < mend) && (mon > mbeg) && (mon < mend)) ||
    ((mbeg > mend) && ((mon > mbeg) || (mon < mend)))) {
    tv.tv_sec += dstEntry[dst].dstBias;

    settimeofday(&tv, NULL);

    nvram_set("timer_interval", NTP_M_TIMER); // are these used??
    return 0;

    void ntp_main(timer_t t, int arg)
    if (check_action() != ACT_IDLE) return; // don't execute while upgrading
    if (!check_wan_link(0)||nvram_match("wan_proto","disabled")) return; // don't execute if not online

    if ((do_ntp() == 0) && (arg == FIRST)) timer_cancel(t);

    int stop_ntp(void)
    eval("killall","-9", "ntpclient");
    return 0;
  7. slinde

    slinde Network Guru Member

    Thanks for really good answers tofu and mattiL! :clap:

    Now I know that time will not update if WAN=disabled.
    I think I will get around it for now by creating a cron-job that runs ntpclient say four times a day.

    And later on the issue might be fixed in a future release of HyperWRT-tofu... :rockon:

    I'm happy!
    Thank you
  8. kevinsd

    kevinsd Guest

    Assuming your timezone is GMT+8, then try

    nvram set time_zone=UTC-8

    Dont take into account DST. That should be set seperately.

Share This Page