15

I know that (for example in Debian) one could use dpkg-reconfigure tzdata to change the timezone of the system but I am wondering how one could change the user timezone (if Linux/Unix supports such an idea at all).

I normally work on the server remotely through ssh and given that the server works with UTC internally (files, ...) and timezones are rather superficial, I would like to see the times in my desired timezone depending on where I am connecting from. Is this possible?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Loax
  • 345
  • 2
  • 3
  • 8
  • What did you try? Did you even search for "ssh passing on TZ information" on the web at all? – Anthon Mar 03 '14 at 10:46

2 Answers2

20

Use the TZ environment variable. E.g.:

bash$ export TZ=US/Pacific
bash$ date
Mon Mar  3 00:31:17 PST 2014
bash$ export TZ=US/Eastern
bash$ date
Mon Mar  3 03:33:06 EST 2014

The possible values for TZ are in the directory /usr/share/zoneinfo (see, for example, the existence of /usr/share/zoneinfo/US/Pacific)

samiam
  • 3,616
  • 1
  • 14
  • 15
  • In my case, `date` gives a different time-zose and time than `uname -a`. Why would that be? – Geremia Jul 08 '17 at 22:16
  • 1
    `uname` doesn't list the current timezone or anything regarding time. If you run `uname -v`, that will print the version of the kernel currently running which might include a timestamp listing when the kernel was built. That's a static piece of text that is not affected by your `TZ` setting. – JM0 Mar 25 '21 at 18:47
9

First, you need some configs for ssh server and ssh client.

In Server, in /etc/ssh/sshd_config, make sure you accept TZ variable:

AcceptEnv LANG LC_* TZ

In Client, in /etc/ssh/ssh_config or ~/.ssh/config, make sure you send TZ variable:

SendEnv TZ

(The defaults are usually to send none from the client, and accept none on the server.)

Then make alias for ssh command to use your current TZ in ssh session. Add this line to .bashrc file:

alias ssh='TZ=${TZ:-"$(cat /etc/timezone)"} ssh'

or use this for system doesn't have /etc/timezone:

alias ssh='TZ=${TZ:-"$(date +%Z)"} ssh'

Then every time you ssh to remote server, the time on server will display base on you local timezone.

cuonglm
  • 150,973
  • 38
  • 327
  • 406
  • You can also use `TZ=$(date +%Z)` with most versions (Linux/*BSD/Solaris) of date, this will work better on systems where `/etc/timezone` isn't the done thing. – mr.spuratic Mar 03 '14 at 09:33
  • 1
    Friendly reminder. Remember to restart sshd for changes to take effect. On ubuntu "service ssh restart" – cavalcade Feb 22 '15 at 05:26