Skip to content

Instantly share code, notes, and snippets.

@lentschi
Last active July 29, 2020 22:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lentschi/605b031ea32d7c5c0655b5008ed8ee2a to your computer and use it in GitHub Desktop.
Save lentschi/605b031ea32d7c5c0655b5008ed8ee2a to your computer and use it in GitHub Desktop.
Run a program requiring xserver as another user by passing on the xauth cookie
#!/bin/bash -e
# Run a program requiring xserver as another user by passing on the xauth cookie
# s. http://askubuntu.com/questions/871092/failed-to-connect-to-mir-failed-to-connect-to-server-socket-no-such-file-or-di?newreg=811c7a1d637341c5b294d8b515e8b6e7
# s. http://serverfault.com/questions/51005/how-to-use-xauth-to-run-graphical-application-via-other-user-on-linux
# AUTHOR: Florian Lentsch <office@florian-lentsch.at>
# TESTED: on Ubuntu 16.04 only
#
# USAGE: xsudo <username> <command>
if [ "$#" -ne 2 ]; then
>&2 echo "Usage: $0 <username> <command>"
exit 1
fi
# Create temporary binary:
temp_bin="$(mktemp)"
cat > $temp_bin <<- EOF
#!/bin/bash -e
# read xauth param from stdin:
read xauth_param
touch ~/.Xauthority # <- just in case the user has never logged into an xsession before
export DISPLAY="\$(echo \$xauth_param | grep -o '^[^ ]\+')"
# Add the first user's xauth cookie:
xauth add \$xauth_param
# Run the actual command:
\$1
# Clean up:
xauth remove \$DISPLAY
EOF
chmod a+rx $temp_bin
# Get current user's xauth cookie (best guess):
cookie="$(xauth list | grep `uname -n` | grep 'unix:0' | grep -o '[^ ]\+$' | head -1)"
# Run the command using pkexec:
set +e
echo "$DISPLAY . $cookie" | pkexec --user $1 $temp_bin "$2"
cmd_exit_code=$?
set -e
# Clean up:
rm $temp_bin
exit $cmd_exit_code
@pavemelan
Copy link

./xsudo.sh pol psensor
xauth:  file /root/.Xauthority does not exist
xauth: (argv):1:  bad "add" command line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment