Timezone setting in Linux

  • I'm setting the timezone to GMT+6 on my Linux machine by copying the zoneinfo file to /etc/localtime, but the date command is still showing the time as UTCtime-6. Can any one explain to me this behavior?

    I'm assuming the date command should display UTCtime+6 time. Here are steps I'm following:

    date
    Wed Jan 22 17:29:01 IST 2014
    
    date -u
    Wed Jan 22 11:59:01 UTC 2014
    
    cp /usr/share/zoneinfo/Etc/GMT+6 /etc/localtime
    
    date
    Wed Jan 22 05:59:21 GMT+6 2014
    
    date -u
    Wed Jan 22 11:59:01 UTC 2014
    

    Has TZ environment variable been set (does `echo $TZ` produce something but an empty line)? Also it might be a better idea to create a symlink to `/etc/localtime` instead of copying the actual file (that way you'll be safe, in case zoneinfo data ever changes which it most likely never will).

    What's the distro? Different distros handle this differently!

    @slm:I'm using ubuntu 12.04

    You likely did not know this policy but you're not suppose to cross post the same Q's on the different SE sites. http://stackoverflow.com/questions/21282367/timezone-setting-in-linux

    Also cross posted to Super User: *Timezone setting in Linux*

    But the example shows that the timezone **did** change to gmt+6. Though the sign looks wrong.

    "This question has been posted on multiple sites" Huh, that's a new one for me. Never encountered a SE site with close reason like that. Anyway, this is the first question that comes up on Google when searching for how to change a time zone in Linux, and it's the most highly-voted question of all the ones I've seen, so maybe it should be reopened?

    @Ajedi32 - I reviewed the other questions on the other SE sites and they're still there. The content is valid in this Q but we are pretty strict on the cross-posting so I'm leaving it closed.

    @slm Could we close the linked question and make this one the official one? This question has far more useful information for people coming here, and continues to be the most highly ranked SO site in Google for the search "set timezone linux".

    @ElliottSlaughter - this one got closed b/c the OP posted it to multiple sites. Our site has a policy of not allowing cross-posting to other SE sites.

    @slm: I understand that the different SE sites operate independently, but it still seems like you could go over to the other site, ask their moderators if they'd be willing to close their version of the question, and if they do, then reopen this one. That way you're maintaining the invariant that at most one version is open.

  • slm

    slm Correct answer

    7 years ago

    Take a look at this blog post titled: How To: 2 Methods To Change TimeZone in Linux.

    Red Hat distros

    If you're using a distribution such as Red Hat then your approach of copying the file would be mostly acceptable.

    NOTE: If you're looking for a distro-agnostic solution, this also works on Debian, though there are simpler approaches below if you only need to be concerned with Debian machines.

    $ ls /usr/share/zoneinfo/
    Africa/      CET          Etc/         Hongkong     Kwajalein    Pacific/     ROK          zone.tab
    America/     Chile/       Europe/      HST          Libya        Poland       Singapore    Zulu
    Antarctica/  CST6CDT      GB           Iceland      MET          Portugal     Turkey       
    Arctic/      Cuba         GB-Eire      Indian/      Mexico/      posix/       UCT          
    Asia/        EET          GMT          Iran         MST          posixrules   Universal    
    Atlantic/    Egypt        GMT0         iso3166.tab  MST7MDT      PRC          US/          
    Australia/   Eire         GMT-0        Israel       Navajo       PST8PDT      UTC          
    Brazil/      EST          GMT+0        Jamaica      NZ           right/       WET          
    Canada/      EST5EDT      Greenwich    Japan        NZ-CHAT      ROC          W-SU         
    

    I would recommend linking to it rather than copying however.

    $ sudo unlink /etc/localtime 
    $ sudo ln -s /usr/share/zoneinfo/Etc/GMT+6 /etc/localtime
    

    Now date shows the different timezone:

    $ date -u
    Thu Jan 23 05:40:31 UTC 2014
    
    $ date 
    Wed Jan 22 23:40:38 GMT+6 2014
    

    Ubuntu/Debian Distros

    To change the timezone on either of these distros you can use this command:

    $ sudo dpkg-reconfigure tzdata
    

        ss #1

    $ sudo dpkg-reconfigure tzdata
    
    Current default time zone: 'Etc/GMT-6'
    Local time is now:      Thu Jan 23 11:52:16 GMT-6 2014.
    Universal Time is now:  Thu Jan 23 05:52:16 UTC 2014.
    

    Now when we check it out:

    $ date -u
    Thu Jan 23 05:53:32 UTC 2014
    
    $ date 
    Thu Jan 23 11:53:33 GMT-6 2014
    

    NOTE: There's also this option in Ubuntu 14.04 and higher with a single command (source: Ask Ubuntu - setting timezone from terminal):

    $ sudo timedatectl set-timezone Etc/GMT-6
    

    On the use of "Etc/GMT+6"

    excerpt from @MattJohnson's answer on SO

    Zones like Etc/GMT+6 are intentionally reversed for backwards compatibility with POSIX standards. See the comments in this file.

    You should almost never need to use these zones. Instead you should be using a fully named time zone like America/New_York or Europe/London or whatever is appropriate for your location. Refer to the list here.

    @user3184706 - you've kind of made a mess here. Your Q here was how to change the timezone, which I answered. The bit about using GMT.. timezone or not that the SO A provided is accurate, but I neglected to include that here, since you weren't really asking about that, though I thought. I can either add that bit to this A or you can copy/paste that info here as your own A.

    @user3184706 - to try and clean this up I included Matt's A in mine to make it complete.

    Thanks man, this "sudo dpkg-reconfigure tzdata" works in my Debian!!!

    For command-line setting of time zone in Ubuntu, the following link provides the trivial 1-line command: http://askubuntu.com/a/524362/182454

    @DanNissenbaum - thanks I've incorporated that into the answer. Note that this answer pre-dates 14.04's existence.

    Just FYI, if you're looking for a distro-agnostic solution, use the first approach (the one labeled "Red Hat"). It works on Debian (even if it's not recommended) and doesn't rely on distro-specific commands or user interaction.

    Note though that if you're in a container, with Ubuntu you have to install the package `tzdata` or else the files in `/usr/share/zoneinfo` won't exist.

    `timedatectl` works for SLES 12 too.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM

Tags used