0

During my travel abroad, I will connect to the SSH server in my home country. I can find my connection times by:

last user
last | grep user

but these two commands give me a connection time based on my country time, not local time. How can I get connection time based on local time?

water77
  • 13

2 Answers2

0

Locally invoke date +%Z, date +%:z (see man 1 date) or another command to learn the timezone you're in. Or maybe you already know it's e.g. Asia/Hong_Kong. Then on the server set TZ accordingly when you invoke last. Example:

# on server
TZ=Asia/Hong_Kong last

The server must know the timezone. Alternatively:

# on server
TZ=foo-8 last
# or even
TZ=bar-08:45 last

(foo and bar are arbitrary 3-letter strings here).

Note there is a quirk: if (local) date +%:z tells you +08:00 then you need to set TZ=foo-08:00 or TZ=foo-8. If it tells you -08:00 then TZ=foo+08:00.


You can set TZ and export it in the remote shell, so it affects your entire session.

If I read man 1 ssh right, the server will put TZ in your environment if the variable is in the environment of the daemon (sshd). To use your local TZ automatically the server needs to be configured. (And before you test various ideas note your local TZ may or may not be set in the first place).

A somewhat cumbersome way to pass your local timezone to the remote side is to embed variable assignment in the command itself. Example:

# locally
ssh user@server "TZ='foo$(date +%:z | tr +- -+)' last"

date +%:z | tr +- -+ is executed locally (tr because of the quirk).

0

Once you are logged into the server in your home country, run

export TZ=$(tzselect)

and follow the prompts. It will ask you which continent you are in, then which country, and maybe even finer details. Eventually it will end up telling you that it is using something like Europe/Madrid, and the times used by most programs will now show in your PC time.

In future logins you can just use

 export TZ="Europe/Madrid"

to avoid the questions. You can put this line in your ~/.bashrc if you are going to be there for any length of time.

icarus
  • 17,920
  • Interesting: I wonder how you pass local TZ to the remote when you connect. – ctrl-alt-delor Feb 20 '20 at 23:05
  • 1
    @ctrl-alt-delor as the manual page tells you, you can set up a ~/.ssh/environment file to pass over any environment variables you want, but it requires the server to be configured with "PermitUserEnvironmet yes" and most servers are not set up that way (for good reasons). I was suggesting setting a remote TZ variable instead. – icarus Feb 20 '20 at 23:47