Time Zone is getting different over ssh command & normal ssh
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
Related videos on Youtube

Comments
-
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 over 8 yearsWhich of both is the correct one?
-
Dims over 8 yearsType
TZ
variables on both computers too and also type time on your client computer. Are you computers in the same timezone physically? -
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 over 8 years@Dims: Let me rephrase: Which of both timezones is the correct one?
-
Dims over 8 years@Jan it can be both correct if two computers are in different timezones physically.
-
Jan over 8 yearsYou misunderstood the question, there is only a single computer involved here!
-
Dims over 8 yearsPrompts are different. Unprobable it is the same computer. But it CAN be so -- then let author confirm.
-
Sriharsha Kalluru over 8 yearsThese are not two servers, It is single server but getting different outputs in different cases. Ideally it should be same.
-
Dims over 8 yearsIsn't there additionally a client computer, which you connect to the server via ssh from?
-
Sriharsha Kalluru over 8 yearsYes I use linux machine to run the ssh command.
-
Dims over 8 yearsSo you have two different machines. Timezone of the client machine also can affect. See
SendEnv
andAcceptEnv
directives of SSH config. You can check timezone variable withecho $TZ
on client and withssh [email protected] 'echo $TZ'
on server. Also you can login to server withssh
separatedly and doecho $TZ
there. You should find variable difference and know, where it came from. -
Sriharsha Kalluru over 8 yearsBoth 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 over 8 years
ssh [email protected] 'echo $TZ'
also returnsPDT
? -
Sriharsha Kalluru over 8 yearsIt 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 over 8 yearsLet us continue this discussion in chat.
-
Dims over 8 yearsThis 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 over 8 yearsI am using same user and there is no local variable.
-
Jan over 8 yearsIt 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 over 8 years"both computers should be CORRECT"? There is only a single machine involved here...
-
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 over 8 yearsIt 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 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 over 8 yearsAlso I would suggest to add
TZ
to SSH configsSendEnv
andAcceptEnv
entries along withLANG
andLC_*
. -
Jan over 8 yearsmachine1 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 over 8 yearsYes 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 about 5 yearsI 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 thatTZ
accepts? (This does nto work because I am a guest on the remote system: Setting the Timezone with an automated script). -
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 usesetenv
. I'd expect the tz names in this list to work.