Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Created December 30, 2015 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/81878bb68a86b88c0c82 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/81878bb68a86b88c0c82 to your computer and use it in GitHub Desktop.
Need to use X as a user on a box after Sudo? Try this. Both files need putting somewhere in the path, and need chmod 755 owned by root.
#! /bin/bash
if [ -n "$DISPLAY" ]; then
CURRENTDISPLAY="`echo $DISPLAY | cut -d ':' -f 2 | cut -d '.' -f 1 | sed -e s/^/:/`"
DPYNAME="`xauth list | grep "$CURRENTDISPLAY" | cut -d ' ' -f 1`"
PROTONAME="`xauth list | grep "$CURRENTDISPLAY" | cut -d ' ' -f 3`"
HEXKEY="`xauth list | grep "$CURRENTDISPLAY" | cut -d ' ' -f 5`"
fi
if [ -z "$1" ] && [ `id -u root` == '0' ]; then
SU_TO="root"
C1="$1"
C2="$2"
C3="$3"
C4="$4"
elif [ -z "$1" ]; then
echo "Unable to determine the root user. Please specify"
else
SU_TO="$1"
C1="$2"
C2="$3"
C3="$4"
C4="$5"
fi
if [ -z "$2" ]; then
sudo -u "$SU_TO" -i
elif [ -z "$DISPLAY" ]; then
sudo -u "$SU_TO" -i -- "$C1" "$C2" "$C3" "$C4"
else
sudo -u "$SU_TO" -i -- xauth_exec.sh "$DPYNAME" "$PROTONAME" "$HEXKEY" "$C1" "$C2" "$C3" "$C4"
fi
#! /bin/bash
xauth add $1 $2 $3
"$4" "$5" "$6" "$7"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment