Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 3 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
add comment

Your Answer

 
discard

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.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.3.12.1448\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Super User works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscrip"], "copyright__copyright__copyright": ["xers, 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.\n\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}