Got tired of trying to explain to users /cough/dba’s/cough/ how to use X11 forwarding in conjunction w/ sudo/su and finally sat down and created this little script that to generate output that just needs to be copied before using sudo/su and pasted afterwards. So far it seems to work pretty well.
su-x11.sh
#!/bin/bash
## Helper script for X11 forwarding in conjunction w/ su/sudo
##
## Written by cybrhippy
##
## 2013.12.10 v1.00 Initial release
if [ "$DISPLAY" = "" ]; then
echo -e '\n$DISPLAY is not set and/or X11 forwarding is not enabled.\n'
exit 1
else
echo -e 'Copy the below text...\n'
echo -e 'export DISPLAY='$DISPLAY
echo 'xauth add '`xauth list | grep "\`uname -n\`\/unix\:\`echo | awk '{ print substr("'"${DISPLAY}"'", index("'"${DISPLAY}"'",":")+1,2) }'\`"`
echo -e '\n'
fi
exit 0
This has been tested on RHEL [5,6].x and Solaris 10 [sparc,x86] systems so far.
One thought on “Script for making X11 forwarding and sudo/su easier”
Comments are closed.