After tunneling X over an SSH connection using screen on the remote, how can I reattach the screen session from another machine and tunnel X to that other machine?

I either get "cannot open display" type errors, or the app tunnels to the first computer. I assume it has something to to with ENV vars in the screen session, but I'm not sure how to fix the error.


When using screen, you will have to copy the required environment variables manually.

Detach from the screen session, run echo $DISPLAY and copy the value.

Now reattach and run export DISPLAY="copied value".

When you create a Screen session, the screen server automatically inherits a copy of the current environment, which is further inherited by the shells or other processes that you start in new Screen windows inside the session.

xterm                        (DISPLAY=":0")
└── bash                     (DISPLAY=":0")
    └── screen (client)      (DISPLAY=":0")
        └── SCREEN (server)  (DISPLAY=":0")
            ├── irssi        (DISPLAY=":0")
            ├── mutt         (DISPLAY=":0")
            └── bash         (DISPLAY=":0")

When you connect over SSH with X11 forwarding enabled, the SSH server sets the $DISPLAY environment variable pointing to your X11 server.

However, when you reattach to a Screen session, the processes running inside that session will not see it; they are still holding copies of the old environment.

sshd                         (DISPLAY="localhost:12")
└── bash                     (DISPLAY="localhost:12")
    └── screen (client)      (DISPLAY="localhost:12")

SCREEN (server)              (DISPLAY=":0")
├── irssi                    (DISPLAY=":0")
├── mutt                     (DISPLAY=":0")
└── bash                     (DISPLAY=":0")

This is because a process is not allowed to update environments of other processes; only of itself. Even though the screen client process has the new environment, it cannot give it to the screen server; the protocol used by Screen simply does not have such a function.

Some other multiplexers, such as tmux, allow clients to send a copy of its environment to the server upon reattaching. This is not completely effective; it only allows newly opened tmux windows to inherit the new environment, but still cannot do anything about already running windows.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.