12

If I start a tmux session locally and leave it open, then ssh in remotely and start a new tmux session (not attaching to the old one, a brand spanking new session) X11 forwarding seems to break. I've tried this workaround for a similar issue with screen but to no avail...

The error message when I try to launch a GUI app (like xeyes) is something to the effect of can't open display: wrong authentication. I actually don't know what it is for sure because it seems like it's outputting a carriage return and overwriting part of the error message with a new one...

For now I just go through and manually close all the tmux sessions that I had started locally before starting my new remote session. Is there any way to get X11 forwarding working with mixed local/remote tmux sessions?

1
  • #tmuxnotideal Also my iterm click to move cursor doesn't work. Aug 11 at 21:28

2 Answers 2

15

Not 100% sure it'll work for you, but that link didn't work for me either and I just copied the DISPLAY variable from the initial terminal and wrote export DISPLAY=${copied from outside tmux} which worked fine - i.e.

log in ...
echo $DISPLAY # remember this
tmux a
export DISPLAY=${variable from memory}

the function from that link gave me a completely different DISPLAY which didn't work

3
  • Still failed due to "Wrong Authentication"
    – mgalgs
    Jul 15, 2013 at 19:27
  • thanks, this allowed me to open pdfs from tmux with xdg-open file.pdf Mar 23, 2018 at 13:46
  • Did you try ssh -Y instead of ssh -X ?
    – feedMe
    Sep 27, 2018 at 9:32
4

I had a similar problem. In my situation, setting DISPLAY wasn't enough. By default, some of the environment is set when starting the tmux server (when starting the first session), not just any new session. The following works for me, but I doubt it's optimal.

In .tmux.conf, make tmux remember the variables that you want to keep from the outside environment. I used ones that differed between locally using Gnome Terminal and remotely using Terminal.app. Some of them probably aren't necessary, but I'm not sure which. The -g flag might not be correct.

set -g update-environment "\
  CLUTTER_IM_MODULE \
  DBUS_SESSION_BUS_ADDRESS \
  DBUS_SESSION_BUS_ADDRESS \
  DEFAULTS_PATH \
  DESKTOP_AUTOSTART_ID \
  DESKTOP_SESSION \
  DISPLAY \
  GDMSESSION \
  GNOME_DESKTOP_SESSION_ID \
  GPG_AGENT_INFO \
  GTK_IM_MODULE \
  GTK_MODULES \
  SESSION_MANAGER \
  SSH_AGENT_PID \
  SSH_ASKPASS \
  SSH_AUTH_SOCK \
  SSH_CONNECTION \
  WINDOWID \
  WINDOWPATH \
  XAUTHORITY \
  XDG_CONFIG_DIRS \
  XDG_CURRENT_DESKTOP \
  XDG_DATA_DIRS \
  XDG_MENU_PREFIX \
  XDG_RUNTIME_DIR \
  XDG_SEAT \
  XDG_SESSION_DESKTOP \
  XDG_SESSION_ID \
  XDG_SESSION_TYPE \
  XDG_VTNR \
  XMODIFIERS \
  "

Despite the name "update-environment", the setting doesn't seem to actually change the environment. You can, however, retrieve it using tmux show-environment. So I used it to update the environment in my .zshrc:

zshrc_restore_x11_vars()
{
  if [[ -z $TMUX ]]
  then
    return 0
  fi

  local tmux_env="$(tmux show-environment)"

  for var_line in ${(f)tmux_env}
  do
    if [[ $var_line == -[A-Za-z_]* ]]
    then
      local var=${var_line#-}
      unset $var

    elif [[ $var_line == [A-Za-z_]*'='* ]]
    then
      export $var_line

    fi
  done
}

precmd()
{
  zshrc_restore_x11_vars
}

preexec()
{
  zshrc_restore_x11_vars
}

Using tmux show-environment -s probably would have been better. But I didn't know about that until I re-read part of man tmux to write this answer.

Still, I have to detach and reattach sometimes, when using an existing session. Though it is much less annoying than before.

Full disclosure: The idea of using tmux show-environment in theprecmd and preexec hooks wasn't my own, but I forget where I saw it. Thank you, to whoever wrote that, wherever you wrote it.

1
  • Worked for me without the shell script. Very pleased thank you.
    – wbg
    Sep 12, 2019 at 21:21

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.