Automatically add xauth coockie and DISPLAY env when su over ssh

From Urandom
Jump to navigation Jump to search

What use it is?

If you need to run X programs over SSH and can only su to the required user, for example oracle server installations where you have only root login but need to execute graphical programs as user "oracle"

The script

Edit a file

* vim /usr/bin/suX
#Automatically take X coockie and DISPLAY env with you when doing a su
XNR=`env | grep DISPLAY | awk -F: '{print $2}' | awk -F. '{print $1}'`
XMAGIC=`xauth list | grep :$XNR`
echo $XMAGIC
su $1 -c bash -c "export DISPLAY=`env | grep DISPLAY | awk -F= '{print $2}'` && xauth add $XMAGIC && bash"

Dont forget to make it executable

* chmod +x /usr/bin/suX


user@localhost$ ssh -X user@remote
user@remote$ suX oracle
oracle@remote$ xclock