Linux check time zone

How to check which timezone in Linux?

Usually, the TZ environment variable will tell you something useful. However, it is best to use functions such as mktime() and localtime() to convert between time_t and a local timezone representation. That is, don’t try to do the conversion yourself.

i have two instance of server , if i give «date» command in both server it shows different time. time zone for both server is IST, time of one server: Mon Jul 11 10:39:31 IST 2011 and other server Mon Jul 11 10:35:31 IST 2011 .. there is a difference of 4 minutes

@GregHewgill your answer is not helpful at first view. Consider editing your answer please. See the most voted answer below. I will downvote your answer for now. Also there is no TZ env on default on my machines.

If you mean from the console, just type:

And to set the timezone from the command line in Ubuntu, the following link provides the trivial, 1-line command to do so: askubuntu.com/a/524362/182454

If you want the numeric timezone:

date +’%:z %Z’

More precisely, this applies to GNU date . The BusyBox version prints something non-sensical, «%:z CEST» in my case.

I wanted to find the timezone in «US/Eastern» or «Europe/London» form instead. You can find this in:

  • /etc/timezone (present on Ubuntu and Red Hat? but not e.g. Amazon Linux)
  • (on Red Hat style systems) as ZONE=»US/Eastern» in /etc/sysconfig/clock
  • or you can try and match /etc/localtime to one of the files under /usr/share/zoneinfo; annoyingly this doesn’t seem to be a symlink, but you can e.g.

cd /usr/share/zoneinfo
find * -type f -exec sh -c «diff -q /etc/localtime ‘<>‘ > /dev/null && echo <>» \;

Sometimes you may be looking for the canonical timezone rather than the short form as produced by date %Z e.g. US/Eastern . On systems with timedatectl e.g. Fedora, timedatectl outputs lots of useful information, including the current zone:

# timedatectl Local time: Tue 2016-09-13 17:10:26 EDT Universal time: Tue 2016-09-13 21:10:26 UTC RTC time: Tue 2016-09-13 21:10:26 Time zone: US/Eastern (EDT, -0400) Network time on: yes NTP synchronized: yes RTC in local TZ: no 

Unfortunately, timedatectl takes set-timezone as a command, but has no corresponding get-timezone . Parse it as follows:

# timedatectl status | grep "zone" | sed -e 's/^[ ]*Time zone: \(.*\) (.*)$/\1/g'` US/Eastern 

For systemd >= 241, see @BurninateSE’s answer (TLDR: timedatectl show —va -p Timezone ).

For the time zone, you can use geolocation:

$ curl https://ipapi.co/timezone America/Chicago 
$ curl http://ip-api.com/line?fields=timezone America/Chicago 

Just want to point out that although this will likely return the correct timezone the server is located at it does not necessarily return the timezone the server is configured for.

All of the following solutions will give you an IANA compatible Olson timezone ID (e.g. America/Los_Angeles ). However, some may return extra whitespace at the beginning and/or end — trim if required.

Using the environment

The environment variable TZ (if set) specifies the timezone. Checking this first is essential.

I can personally confirm that this works on ALL of my modern Linux hosts.

Using Systemd version >= 241

This is the preferred method for all those who believe Systemd shall rule the world.

$ timedatectl show --va -p Timezone 

If you get an error similar to timedatectl: invalid option — ‘p’ that means your Systemd isn’t ready to rule the world. Instead.

Using Systemd and sed

Improvement upon Raman’s answer, but without the useless use of grep. This solution is optimal on systems utilizing older versions of Systemd.

$ timedatectl | sed -n 's/^\s*Time zone: \(.*\) (.*/\1/p' 

Using /etc/timezone

This is a simple readable file, but doesn’t seem to be available on as many distros anymore. IIRC, this is something those Debian devs came up with a while ago that fell out of favour.

Using GNU realpath because /etc/localtime is a symlink

This solution is optimal if you know that /etc/localtime is a symlink, which is something that both a lot of older software and guides accomplish and even modern SystemD promises; and you also have GNU realpath available, which should also be available on every GNU OS distro.

$ realpath --relative-to /usr/share/zoneinfo /etc/localtime 

If you find yourself with a non-symlink /etc/localtime (the command shown above will either error or provide a path that starts with ../ ), you can try to locate its md5 match in your local timezone database. This may return multiple matches as there are quite a few aliases in the Olson database.

$ find /usr/share/zoneinfo -type f -exec md5sum <> + |\ sed /$(printf $(md5sum /etc/localtime))/'!d;s/.\//;/^posix/d' 

If you’d only like a single timezone ID returned (whichever one is returned first by the system’s readdir()), you can use sed’s q command to quit after the first match. This is much more performant than starting up head for instance. However, this causes find to break the pipe with md5sum early, causing an error to print to fd 2. You may ignore this error by piping it to /dev/null as the following complete command demonstrates:

$ find /usr/share/zoneinfo -type f -exec md5sum <> + 2> /dev/null |\ sed /$(printf $(md5sum /etc/localtime))/'!d;s/.\//;/^posix/d;q' 

Using curl and geolocation

If for some reason you don’t want or don’t trust the host’s configuration (or just hate their guts) but you’re certain that it can connect to the wider world web, there are several gratis online services that will geolocate you (whilst they last). Below are some examples:

$ curl -Lsf https://ipapi.co/timezone # Defunct? $ curl -Lsf http://ip-api.com/line?fields=timezone $ curl -Lsf https://ipwhois.app/line/?objects=timezone $ curl -Lsf http://api.ipgeolocation.io/timezone -e ';auto' | jq -r .timezone 

First two services shamelessly stolen from Steven Penny’s answer. Additionally I implore the SE community to please keep this list up to date with working links thanks!

Finally, I beg of you to please don’t use this method to bind your users and steal their agency — only use this method if they ask you to do so!

Also note that while these services are gratis, please consider visiting their respective web sites to learn about and accept their terms and conditions before using.

Источник

How to Check Timezone in Linux

In this short article, we will walk newbies through the various simple ways of checking system timezone in Linux. Time management on a Linux machine especially a production server is always an important aspect of system administration.

There are a number of time management utilities available on Linux such as date and timedatectl commands to get the current timezone of system and synchronize with a remote NTP server to enable an automatic and more accurate system time handling.

Well, let us dive into the different ways of finding out our Linux system timezone.

1. We will start by using the traditional date command to find out present timezone as follows:

Alternatively, type the command below, where %Z format prints the alphabetic timezone and %z prints the numeric timezone:

Find Linux Timezone

Note: There are many formats in the date man page that you can make use of, to alter the output of the date command:

2. Next, you can likewise use timedatectl, when you run it without any options, the command displays an overview of the system including the timezone like so:

More so, try to employ a pipeline and grep command to only filter the timezone as below:

$ timedatectl | grep “Time zone”

Find Current Linux Timezone

3. In addition, users of Debian and its derivatives can display the content of the file /etc/timezone using cat utility to check your timezone:

Check Timezone of Linux

Important: For REHL/CentOS 7 and Fedora 25-22 users, the file /etc/localtime is a symbolic link to the timezone file under the directory /usr/share/zoneinfo/.

However, you can use date or timedatectl command to display the current time and timezone as well.

To change the timezone, create the symbolic link /etc/localtime to the appropriate timezone under /usr/share/zoneinfo/:

$ sudo ln -sf /usr/share/zoneinfo/zoneinfo /etc/localtime

The flag -s enables creation of a symbolic link, otherwise a hard link is created by default and -f removes an existing destination file, which in this case is /etc/localtime.

For example, to change the timezone to Africa/Nairobi, issue the command below:

$ sudo ln -sf /usr/share/zoneinfo/Africa/Nairobi /etc/localtime

That’s all! Do not forget to share you thoughts about the article by means of the feedback form below. Importantly, you should look through this time management guide for Linux to get more insight into handling time on your system, it has simple and easy-to-follow examples.

Lastly, always remember to stay tunned to Tecmint for the latest and interesting Linux stuff.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Parted Command in Linux

TLDR Man Pages for Linux Commands

apt-get Command Examples

Ubuntu apt-cache Commands

apt Command Examples

Funny Linux Commands

15 thoughts on “How to Check Timezone in Linux”

Any suggestion where should I check further? All settings seem correct? It looks like there is an extra character at the end of America/Chicago. ERROR:
tzone_read_system: no match found for America/Chicago^? Here are my current settings

[[email protected] etc]# timedatectl Local time: Thu 2020-04-23 11:28:51 CDT Universal time: Thu 2020-04-23 16:28:51 UTC RTC time: Thu 2020-04-23 16:28:51 Time zone: America/Chicago (CDT, -0500) NTP enabled: no NTP synchronized: yes RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2020-03-08 01:59:59 CST Sun 2020-03-08 03:00:00 CDT Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2020-11-01 01:59:59 CDT Sun 2020-11-01 01:00:00 CST [[email protected] etc]# [[email protected] etc]# ls -ltr /etc/localtime lrwxrwxrwx. 1 root root 37 Mar 26 12:20 /etc/localtime -> ../usr/share/zoneinfo/America/Chicago [[email protected] etc]# [[email protected] etc]# timedatectl | grep local RTC in local TZ: no [[email protected] etc]# [[email protected] etc]# timedatectl list-timezones | grep Chicago America/Chicago [[email protected] etc]# [[email protected] etc]# date Thu Apr 23 11:34:36 CDT 2020 [[email protected] etc]#

CentOS Linux release 7.7.1908 (Core). We just fixed the issue by “rerunning” the timezone. The log showed bad characters at the end, America/Chicago^? (^?) . We ran tzselect and then chose options 2, 49, 11 for Chicago. Gracefully rebooted the server “shutdown -r now” Thank you, Aaron, for your reply. Reply

@Ketan Great! Many thanks for sharing the solution. This will be of benefit to readers in the future. Reply

The formatting on your «%Z %z» example is a bit off. When your text is displayed, the usual, plain double quotes («) are replaced with “pretty” ones (”) . When I pasted your example into my terminal, I only received errors. You should format your example as:

(Here’s hoping that when I post this comment, the website doesn’t replace my simple quotes with pretty ones!) Reply

Yes, my simple quotes were replaced with pretty ones. That’s not good for displaying things on a technical website. You should see about disabling that feature. Reply

On Centos 7 there’s no “clock” under /etc/sysconfig. And neither is there a /etc/timezone, which, from what you’re saying, should exist on most or all linux distributions. Reply

@lethargo Yap, you can use /etc/localtime instead of /etc/timezone. Thanks for mentioning that. Reply

Indeed, but /etc/localtime is not plain text, it is a binary file. To be more specific, it is a symbolic link to a binary file found in /usr/share/zoneinfo. So you couldn’t simply grep it in Centos 🙂 You simply create a symbolic link to whatever timezone you want and then you can use date or timedatectl to see the current clock. Reply

@lethargos Once again, many thanks for the vital insight into checking and managing timezone on RHEL/CentOS 7, this will be very helpful to users out there. Reply

You’re welcome, but shouldn’t you update the article accordingly, so that other users actually know? There’s a higher chance that they’ll read the article than the comments.

@lethargos As you usefully suggested, we have updated the article to include the correct way of setting and checking timezone in REHL 7/CentOS 7/Fedora 25-22 systems. Many thanks for always following us and offering constructive thoughts.

Hello, How can I change the timezone and how can i set the time period of 12 hrs instead of 24hrs? Reply

Источник

Читайте также:  Mac os samba linux
Оцените статью
Adblock
detector