Persistent X

From Thom Group Wiki
Revision as of 17:41, 16 June 2020 by Ajwt3 (talk | contribs)
Jump to navigation Jump to search

If your internet connection is a bit flaky, it can be a pain to ssh in again and re-create your work environment. A combination of tmux, autossh and xpra saves the day. Make sure tmux and xpra are installed on your department workstation, and autossh and xpra (and a shell and an X Server (e.g. if not using a linux box, Windows Subsystem for Linux running Ubuntu and VcXsrv)) are installed at home.

Replace instances of WORKSTATION with your workstation name (e.g. hypatia), and USER with your UserID This assumes you have setup a tunnel through citadel (via your .ssh/config) along with something like ssh-agent to handle automatic forwarding of keys, such that you can type

user@HOME$ ssh WORKSTATION

to connect directly to your workstation (including X forwarding) without any faff with keys. Now run xpra on your home machine

user@HOME$ xpra start ssh:WORKSTATION --start-child=xterm

This will bring up an xterm on your workstation, forwarded through an ssh tunnel so the window comes up on your home machine. You can freely start X applications. Try

user@WORKSTATION$ xeyes &

This should follow your mouse cursor around.

Now test the resilience of your connection by turning off your internet. You should see 'disconnected' spinners appear on the xterm and xeyes windows.

Now turn on your internet connection again. The xpra session will probably break at this point, as it doesn't know how to automatically reconnected. You can do this manually with

xpra attach ssh:WORKSTATION

This should bring up things where you left off. If you're even more lazy and want it to automatically reconnect, use autossh:

xpra --ssh="autossh" attach ssh:WORKSTATION

For the more advanced user, who will want ssh-authentication to persist after disconnecting and reconnecting, you have to do a little more tweaking. First, create (or append to) a .ssh/rc file with

 #!/bin/bash
      
 #First we need to call xauth which is no-longer called when there's a .ssh/rc file.  This allows x-forwarding, passing the cookie that ssh puts into STDIN into the ~/.XAuthority file.
 xauth add $DISPLAY `cat`
   
   
 #Make a persistent ssh_auth socket
 if test "$SSH_AUTH_SOCK"; then
   if [ ! -e "$SSH_AUTH_SOCK" ]; then
     ln -sf "SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
   fi
 fi

This will create a new link in ~/.ssh/ssh_auth_sock which is updated whenever a new ssh logs in as you with agent forwarding, AND the existing link no longer exists. The net effect is that whenever autossh re-logs-in the connection, it will update the persistent link.

When starting your xpra session for the first time you'll need to tell it to use this, so start your sessions with:

xpra start --ssh=autossh --sharing=yes --env="SSH_AUTH_SOCK=/home/USER/ssh_auth_sock" --start-child=xterm ssh:WORKSTATION

I have customised my sessions a little using:

xpra start --ssh=autossh --sharing=yes --desktop-scaling=1446x2560 --border=lime,2 --env="SSH_AUTH_SOCK=/home/ajwt3/ssh-auth_sock" --start-child=xterm ssh:liminal

Bugs

Clipboards are not working consistently.