Time Zone is getting different over ssh command & normal ssh

7,169

Solution 1

On unix, the timezone for an individual process can be set by setting the environment variable TZ. Each process can have a different value for TZ and thus show a different timezone. If TZ isn't set, there's a system-wide default.

In your second example, you ran ssh with specifying a command to run on the remote server. So ssh set up an ordinary interactive session, and your shell on remote system did all of the initialization that it does for interactive sessions.

In the first example (ssh [email protected] 'date'), you told ssh to run a particular command on the remote host. In this case, ssh ran the command without a TTY. When the remote session doesn't have a TTY, the remote instance of your shell doesn't do all of the initialization it would normally do for an interactive session. So it skipped sourcing some files like your .profile or the systemwide profile.

It appears that one of your shell's initialization files sets TZ to one of the US pacific timezones. When you log into the system interactively, this initialization takes place and you see times in PDT. When you log in non-interactively, this step is being skipped and you're getting a different timezone.

You can set the timezone when running commands non-interactively:

ssh [email protected] 'TZ=US/Pacific date'

Or you can force ssh to allocate a TTY, which ought to cause your remote shell to perform additional initialization:

ssh -t [email protected] 'date'

But what you really ought to do is correct your shell initialization files. If you want your timezone to be set every time, then you should move the commands which set TZ to be executed every time, not just for interactive sessions.

Solution 2

The wrong TZ is probably set via .profile, .bash_profile or bashrc, thus overriding the machine-wide TZ setting in /etc/timezone

Share:
7,169

Related videos on Youtube

Sriharsha Kalluru
Author by

Sriharsha Kalluru

Linux Administrator

Updated on September 19, 2022

Comments

  • Sriharsha Kalluru
    Sriharsha Kalluru 3 months

    Whenever I run a date command over SSH the timezone shows different and if I login to server with SSH and if I run the same command it is completely different.

    Running command with SSH

    >ssh [email protected] 'date'
    [[email protected]]# date
    Mon Sep 22 03:06:33 EDT 2014
    

    Running command after login with SSH.

    [[email protected] ~]# date
    Mon Sep 22 00:07:28 PDT 2014
    [[email protected] ~]#
    

    The timezone of my second case is correct.

    • Jan
      Jan over 8 years
      Which of both is the correct one?
    • Dims
      Dims over 8 years
      Type TZ variables on both computers too and also type time on your client computer. Are you computers in the same timezone physically?
    • Dims
      Dims over 8 years
      @Jan both times are correct since it is (approximetely) the same time expressed for different timezones. EDT is 3 hours further than PDT, see here: worldtimebuddy.com/edt-to-pdt-converter
    • Jan
      Jan over 8 years
      @Dims: Let me rephrase: Which of both timezones is the correct one?
    • Dims
      Dims over 8 years
      @Jan it can be both correct if two computers are in different timezones physically.
    • Jan
      Jan over 8 years
      You misunderstood the question, there is only a single computer involved here!
    • Dims
      Dims over 8 years
      Prompts are different. Unprobable it is the same computer. But it CAN be so -- then let author confirm.
    • Sriharsha Kalluru
      Sriharsha Kalluru over 8 years
      These are not two servers, It is single server but getting different outputs in different cases. Ideally it should be same.
    • Dims
      Dims over 8 years
      Isn't there additionally a client computer, which you connect to the server via ssh from?
    • Sriharsha Kalluru
      Sriharsha Kalluru over 8 years
      Yes I use linux machine to run the ssh command.
    • Dims
      Dims over 8 years
      So you have two different machines. Timezone of the client machine also can affect. See SendEnv and AcceptEnv directives of SSH config. You can check timezone variable with echo $TZ on client and with ssh [email protected] 'echo $TZ' on server. Also you can login to server with ssh separatedly and do echo $TZ there. You should find variable difference and know, where it came from.
    • Sriharsha Kalluru
      Sriharsha Kalluru over 8 years
      Both are in same timezone and echo $TZ is giving same o/p in both the servers. My actual concern is why it is giving different outputs while running the command from ssh directly from client and connecting to server from client and executing date command.
    • Dims
      Dims over 8 years
      ssh [email protected] 'echo $TZ' also returns PDT?
    • Sriharsha Kalluru
      Sriharsha Kalluru over 8 years
      It is returning empty. If I login it is showing PST8PDT and in the client where I am connecting it is also having same time zone and showing as PST8PDT. I have one more server which shows the same time either if I run date command from ssh directly or even after logging in. But this server also ssh server 'echo $TZ' is showing empty, but after login it is showing PST8PDT .
    • Sriharsha Kalluru
      Sriharsha Kalluru over 8 years
    • Dims
      Dims over 8 years
      This means that @Jan's version is correct. The reason is where you caught it. Check /etc/timezone file. Probably it is empty or absent.
  • Sriharsha Kalluru
    Sriharsha Kalluru over 8 years
    I am using same user and there is no local variable.
  • Jan
    Jan over 8 years
    It doesn't matter if you're using the same user or not, interactive (non-login) shells do things differently, see here wiki.bash-hackers.org/scripting/bashbehaviour
  • Jan
    Jan over 8 years
    "both computers should be CORRECT"? There is only a single machine involved here...
  • Dims
    Dims over 8 years
    @Jan prompt on first computer is > and prompt on second is [[email protected] ~]#. It CAN be same computer, but it is not evident and unprobable.
  • Jan
    Jan over 8 years
    It is evident that this question is about a single computer, and not about 2 different machines, perhaps you misunderstood the question?
  • pabouk - Ukraine stay strong
    pabouk - Ukraine stay strong over 8 years
    @Jan The question suggests that there are two computers involved: the server (SSH connection destination) and the client (SSH connection origin). The time zone settings of the client could possibly be transferred into the SSH session to the server.
  • Dims
    Dims over 8 years
    Also I would suggest to add TZ to SSH configs SendEnv and AcceptEnv entries along with LANG and LC_*.
  • Jan
    Jan over 8 years
    machine1 sshs into machine2 and executes date. User asks why the output differs, if machine2 is accessed on the console rather than via ssh.
  • Sriharsha Kalluru
    Sriharsha Kalluru over 8 years
    Yes we may do that but I have two servers where on one server only date command is showing while connecting from ssh ther one os showing properly and both the server configurations are idle.
  • jww
    jww about 5 years
    I think you identified the problem. How does one set the time zone in a login script? Is it a simple export TZ=US/Pacific? If not, where do we find the string that TZ accepts? (This does nto work because I am a guest on the remote system: Setting the Timezone with an automated script).
  • Kenster
    Kenster about 5 years
    @jww Yes, if you want to override the system default for a session, you can set the TZ environment variable as you've indicated. export is sh/bash syntax; for csh/tcsh I think you'd use setenv. I'd expect the tz names in this list to work.

Related