Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
I'm loving tmux, thanks! – Matt Alexander Feb 24 '12 at 15:43
For a somewhat less manual process, check out – xpt Jan 23 at 19:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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