Skip to content

Instantly share code, notes, and snippets.

@belminf
Created August 18, 2015 12:57
Show Gist options
  • Save belminf/e9f0ea251591f7b50754 to your computer and use it in GitHub Desktop.
Save belminf/e9f0ea251591f7b50754 to your computer and use it in GitHub Desktop.
Export xauth stuff when switching users
#!/bin/sh
# Help
usage() {
echo -e "Usage: $0 <user to switch to>"
}
# Check if we have enough args
if [ $# -ne 1 ]
then
usage
exit 1
fi
# User sudo'ing to
sudo_user=$1
# Variables needed
display_name=`sed -e 's/^.*://' -e 's/\.[0-9]*//' <<< $DISPLAY`
auth_entry=`xauth list | grep ":$display_name"`
auth_name=`echo $auth_entry | cut -d' ' -f1`
default_shell=`getent passwd $sudo_user | cut -d: -f7`
# Open new shell with xauth stuff exported
sudo su - $sudo_user --session-command "xauth add $auth_entry; DISPLAY=$DISPLAY $default_shell -; xauth remove $auth_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment