Monday, July 18, 2011

Set local time in DD-WRT

***This has been fixed in newer builds of DD-WRT - Check yours before continuing***

This never looks to be a problem since DD-WRT provides such extensive options. Unfortunately, unlike Tomato and OpenWrt, which do a good job, DD-WRT's approach is way off and completely wrong.

A Linux box can keep its time in either local time or UTC -- both are fine as long as the corresponding time zone info is presented. However, in DD-WRT, the time zone info is completely missing and instead of standard implementing used in almost all Linux distros, DD-WRT  coins a weird scheme to setup(and keep) the time.

For example, 8AM PST(GMT-8) should be 4PM GMT, but a DD-WRT router would "think" the current time is 8AM GMT. Now if you change the time zone to EST(GMT-5), what will the router think? 11AM GMT!! Yes, that's after you've set all the "time zone" info in DD-WRT web management page. This is usually not a problem if you just want to use it as a router but for a Linux server, you definitely don't want to deal with any file created in future.

Now here is the fix:

  1. Disable DD-WRT's own NTP clientFrom DD-WRT's web interface, Setup->Basic Setup, at the bottom of the page, disable the whole NTP part.


    Then click "Apply Settings"

  2. Install our own NTP client
    Use PuTTY to login the router, then run

    opkg install ntpclient
    Then we need to get the newly installed ntpclient running as a service. To do so, use vi or nano and create a file named /opt/etc/init.d/ntpclient

    source /mnt/root/.profile
    /opt/usr/sbin/ntpclient -i
    600 -D -s -l -h -p 123
    #600 means it will sync every 10 minutes(600 seconds) is the NTP server to use

    Give it executable permission and then set it to run on start up.

    chmod a+x /opt/etc/init.d/ntpclient
    ln -s /opt/etc/init.d/ntpclient /opt/etc/init.d/S20ntpclient

  3. Set time zone info for DD-WRT
    First, we need to create the /etc/TZ file with proper time zone info

    echo PST8PDT,M3.2.0,M11.1.0 > /etc/TZ
    The red part an example(US Pacific) and the full list can be found here(the time zone table). Replace it with yours accordingly. And, don't worry about daylight saving time, it's already taken care of.

    We also need to set the TZ variable by adding a line in /mnt/root/.profile

    export TZ=$(cat /etc/TZ)
    Finally, there is a hidden nvram value used by DD-WRT internally, it's also needed to be fixed or the web page will display a wrong time. From PuTTY, run:

    nvram set time_zone=PST8PDT,M3.2.0,M11.1.0
    nvram commit

    #replace the red part with yours.
It's done. Reboot the router and login with PuTTY, type date to check the output. DD-WRT should start using local time now.


"Install our own NTP client" >>> where do I find this client?

You need to install opkg then follow the instructions in the 'install our own ntp client' section. The ntp client is downloaded and installed by opkg. How to for installing opkg is here:

Post a Comment

Note: Only a member of this blog may post a comment.