How do I correctly (re)set $DISPLAY?
Solution 1
DISPLAY
is used by X clients (application programs) to find the corresponding X server to connect. It's of the form hostname:displaynr.screennr
, but usually you only see something like :0
, which means the first display of the X server running on localhost
.
Using a hostname in there is not secure, because the X protocol is not encrypted. So ssh
with X forwarding piggybacks on this schema by finding a free display number, usually 10 or larger, and pretending to be an X server at this display number. But in reality it just forwards the X protocol over the ssh connection to the X server running on the host where the ssh client was called.
So that's why you get different DISPLAY
contents when you reconnect with ssh
- each connection with ssh
sets this variable, possibly to a different value.
This new correct value is visible before you attach to tmux. It's also visible when you open a new terminal, because a new terminal will copy the freshly set DISPLAY
variable, while an old terminal will keep the DISPLAY
variable it already has.
So if instead of attaching to tmux you run a self-written script, which reads out DISPLAY
, attaches to tmux, and then sets DISPLAY
in the existing sessions, you can automate what you need to do.
However, doing that with tmux doesn't seem to be straightforward. Here is another question with a few suggestions how to do that that may work for you (or not).
Solution 2
After you attach tmux, by default it will update DISPLAY
in the session environment, but it can only apply this to new panes. To apply it to an existing pane you should be able to do something like:
eval "$(tmux showenv -s DISPLAY)"
StoneThrow
Updated on September 18, 2022Comments
-
StoneThrow over 1 year
I connect from a Windows PC to a Linux PC with ssh using MobaXTerm.
Within the ssh session, I have a tmux session with a few windows and panes.
The ssh session will typically disconnect after a few hours of inactivity (I've tried toying with what looked like relevant keep-alive settings in MobaXTerm, but it's never worked).
First-world problem: After starting a new ssh session and reattaching to my existing tmux sessions, the
$DISPLAY
variable will sometimes be set "incorrectly" - by which I mean, when I launch a GUI that uses X-Windows (e.g. Firefox), I'll get "cannot open display" error messages. E.g.:$ firefox & [1] 23077 $ Unable to init server: Broadway display type not supported: localhost:11.0 Error: cannot open display: localhost:11.0 [1]+ Exit 1 firefox $ echo $DISPLAY localhost:11.0
Usually when I open a new terminal, I'll get the updated/correct value of
$DISPLAY
- from this terminal, I'll be able to successfully launch X-Window-using GUIs.Question: is there any way I can dynamically "update" the value of
$DISPLAY
in an existing terminal (i.e. a terminal that's been alive since before the ssh disconnection)? I.e. I'd like to try avoid having to launch a new terminal just for the purpose of getting/discovering the new value of$DISPLAY
.I don't really have a solid understanding of what
$DISPLAY
represents - so I'd be grateful if someone could explain what it represents and does in the context of the described.