Thursday, September 21, 2006

xauth, su and sudo with ssh X tunneling

Some xauth notes to su from root but keep the same ssh tunnel for X11
# save the MIT-MAGIC-COOKIE for the current X display to a file (/tmp/xauth.out)
xauth extract /tmp/xauth.out $DISPLAY
# save the DISPLAY variable to a file (remember env is lost)
echo $DISPLAY > /tmp/DISPLAY
# make sure the su'd id can read the files
chmod 666 /tmp/xauth.out /tmp/DISPLAY
# su
su - oracle
# setup a new XAUTHORITY file
export XAUTHORITY=/tmp/xauth
# load DISPLAY
export DISPLAY=$(cat /tmp/DISPLAY)
# Load the MIT-MAGIC-COOKIE for root's display
xauth merge /tmp/xauth.out
# run the X application
/usr/bin/X11/xclock
Or as a single command
xauth extract /tmp/xauth.out $DISPLAY; echo $DISPLAY > /tmp/DISPLAY; \
chmod 666 /tmp/xauth.out /tmp/DISPLAY; su - oracle -c \
"export XAUTHORITY=/tmp/xauth; export DISPLAY=$(cat /tmp/DISPLAY); \
xauth merge /tmp/xauth.out ; /usr/bin/X11/xclock"

No comments: