Thursday, June 24, 2010

Set DD-WRT to Use Local Time

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 client
    From 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.


Eddo said...

Is this still necessary? I'm using build 14815 at the moment. You can set the timezone from the webinterface.

Anonymous said...

Thanks. Before I did this all files I created on my Samba-share where two hours off (CEST time).
Now they are correctly timed.

Anonymous said...

Fantastic information - thank you! The received date as calculated by my mail clients has been off my 4 or 5 hours since I set up MTA, MDA, and anti-spam services on my dd-wrt router earlier this year. Your fix has now resolved this!

I have my router set to reboot once per day to ensure uptime, plus CRON jobs that run every 20 minutes to ensure key services remain up, so I have taken a simplistic approach and simply run the following command in the router's startup script:

/opt/bin/ntpclient -s -h -p 123

I used the current (12/6/2010) "ntpclient" package that was available via Optware.

To answer Eddo's question...
No - this has not been resolved in dd-wrt builds. The timezone that can be set in the web interface when using the ntp client included with dd-wrt is not interpreted correctly at the command prompt nor by services running on the router. The way dd-wrt's build-in ntp client works, you are always in GMT (+0000) and the timezone specified in the web interface is used to set the current time. It should not work that way, but it does.

Mark said...

If I check the time via ssh, it has the correct time without all this hassle... am I missing something?

PhYziCiAN said...

On WNDR-3700 (dd-wrt build 15962) local time works fine.
Time zone - UTC+3 (in my case)
Summer Time (DST) - sun/mar and sun/oct
Server IP/Name -

No need in article above!!

Máximo EA1DDO & HK1H said...

Is the DD-WRT NTP option just used for router timing?
Or is that NTP time information "sent" out by the DHCP protocol to any computer connected to that router?

Post a Comment