Timezone setting in Linux

707,894

Solution 1

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.

Solution 2

This is how I do it in Ubuntu. Just replace Asia/Tokyo with your own timezone.

echo 'Asia/Tokyo' | sudo tee /etc/timezone

sudo dpkg-reconfigure -f noninteractive tzdata

There is a bug in tzdata: certain values get normalized by dpkg-reconfigure:

echo 'US/Central' >/etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Current default time zone: 'America/Chicago'

echo 'US/Eastern' >/etc/timezone
apt-get install --reinstall tzdata
# Current default time zone: 'America/New_York'

Solution 3

tzselect command is made to do what you want.

Share:
707,894

Related videos on Youtube

Rahul Dhobi
Author by

Rahul Dhobi

Updated on September 18, 2022

Comments

  • Rahul Dhobi
    Rahul Dhobi over 1 year

    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
    
    • Admin
      Admin over 10 years
      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).
    • slm
      slm over 10 years
      What's the distro? Different distros handle this differently!
    • Rahul Dhobi
      Rahul Dhobi over 10 years
      @slm:I'm using ubuntu 12.04
    • slm
      slm over 10 years
      You likely did not know this policy but you're not suppose to cross post the same Q's on the different SE sites. stackoverflow.com/questions/21282367/timezone-setting-in-lin‌​ux
    • Peter Mortensen
      Peter Mortensen over 9 years
      Also cross posted to Super User: Timezone setting in Linux
    • ctrl-alt-delor
      ctrl-alt-delor almost 8 years
      But the example shows that the timezone did change to gmt+6. Though the sign looks wrong.
    • Ajedi32
      Ajedi32 almost 8 years
      "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?
    • slm
      slm over 7 years
      @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.
    • Elliott Slaughter
      Elliott Slaughter over 5 years
      @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".
    • slm
      slm over 5 years
      @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.
    • Elliott Slaughter
      Elliott Slaughter over 5 years
      @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.
  • Rahul Dhobi
    Rahul Dhobi over 10 years
  • slm
    slm over 10 years
    @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.
  • slm
    slm over 10 years
    @user3184706 - to try and clean this up I included Matt's A in mine to make it complete.
  • JRichardsz
    JRichardsz over 7 years
    Thanks man, this "sudo dpkg-reconfigure tzdata" works in my Debian!!!
  • Dan Nissenbaum
    Dan Nissenbaum over 7 years
    For command-line setting of time zone in Ubuntu, the following link provides the trivial 1-line command: askubuntu.com/a/524362/182454
  • slm
    slm over 7 years
    @DanNissenbaum - thanks I've incorporated that into the answer. Note that this answer pre-dates 14.04's existence.
  • Andreas Hartmann
    Andreas Hartmann over 6 years
    This also works, but in Debian/Ubuntu, slm's answer is probably easier and less risky (no chance of typos)
  • Elliott Slaughter
    Elliott Slaughter over 5 years
    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.
  • Elliott Slaughter
    Elliott Slaughter over 5 years
    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.
  • Alexander Malakhov
    Alexander Malakhov almost 5 years
    timedatectl works for SLES 12 too.