X Forwarding Sudo SSH Session


Many environments make use of sudo to delegate access to administrative or application user accounts. This can make ssh X forwarding tricky because your environment changes as you sudo to another user. This is a workaround that will allow you to continue your ssh X forwarding session while running sudo as another user.

How to SSH X Forward Sudo

First you will need a working X11 environment on your local system. There are a number of different options available depending on your OS.

Once you have your local X11 environment working and tested you’re ready to proceed x forwarding sudo.

you@local$ ssh -XC server
 
you@server$ xauth list
[output]
 
you@server$ sudo su - otheruser
 
otheruser@server$ xauth add [paste output from "xauth list"]
 
otheruser@server$ xterm (or other X application)

For more information please refer to the xauth, sudo and ssh man pages.

For x11 on a non-linux OS:

Xming for windows http://sourceforge.net/projects/xming/
Xquartz for OSX http://xquartz.macosforge.org/landing/

2 Responses to “X Forwarding Sudo SSH Session”

  1. VIKAS Says:

    Just to add my inputs too. I have used xshell (or xmanager) for years without any issues on Windows. It is an awesome piece of software.

    Regards,
    Vikas

    [Reply]

  2. Greg O Says:

    I suggest using “sudo su otheruser” and leave out the hyphen, so the DISPLAY variable isn’t wiped.

    [Reply]

Join the Conversation