Timezone on remote server
There is a well-established idea that servers don’t have a timezone. They all are UTC. Period.
But… When you log in, you want to see a reasonable time. Yes, you can see current time and do some estimations, but it’s tedious. Today I realized, that GNU C library had provided us with a solution.
- Keep servers at UTC. They don’t need to think about that madness with repeated 3AM during daytime shifts, or missing hours, or other craziness.
- There is an environment variable ‘TZ’ which forces most of software, dealing with dates, to use specified timezone.
- You can pass your environment variable TZ into your ssh session (if your ssh server permits this, and your ssh client does).
Where it’s can be used?
dmesg -T
start to show your local time. To you only. Other users on the server are unaffected. Unfortunately, -T is very buggy.journalctl
does so too. You can see your time in journald logs.
Where it’s useless?
Every text-based log (/var/log/syslog
) is keeping original time of the server.
Where it mess things up?
If you start any server-like application from screen, or do any ‘server-wide’ writing, you get timezoned output (at best) or mistimed (at worst, when TZ information is stripped away, but date is in timezone).
How to?
There are two ways. Fist: do it manually every time. I find it’s safer, as it’s opt-in approach. You need to know your timezone. Mine is Asia/Nicosia
.
You can find your in output of find /usr/share/zoneinfo
. (Keep only last one or two things, f.e. Europe/Ljubljana
or Turkey
or America/Chicago
.
Just type in server prompt (I show example for myself for Cyprus):
Manual mode
export TZ=Asia/Nicosia
And you have time-localized output.
Automatic mode
You need to configure both sides:
server, inside `/etc/ssh/sshd_config
`:
AcceptEnv TZ
client, inside ~/.ssh/config
, or inside /etc/ssh/ssh_config
Host *
SendEnv TZ
You need to find existing section, if present, or create a new one.
After that you can confirm, that all servers (which accepts TZ variable) shows your local timezone
date
Tue 20 Jul 2021 01:13:47 PM EESTjournalctl |tail -1
Jul 20 13:18:14 trafi sshd[4948]: Disconnected from invalid user admin 91.241.19.42 port 21157 [preauth]
I wanted to show dmesg -T
, but it’s buggy beyond timzone things.