Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to start xephyr xorg server and desktop session under another user
#!/usr/bin/env bash
set -xe
target_user="$1"
target_home=$(getent passwd $target_user | cut -f6 -d:)
# Find free display number
for ((display_number=1 ; display_number <= 100 ; display_number++)) ; do
[ -e /tmp/.X11-unix/X$display_number ] || break
done
xcache_folder=/tmp/Xephyr_X$display_number
xsocket=/tmp/.X11-unix/X$display_number
xserver_cookie=$xcache_folder/.Xauthority
xclient_cookie=$target_home/.Xauthority
clip_forwarder_pid=-1
xephir_pid=-1
[ -e "$xcache_folder" ] && rm -R "$xcache_folder"
mkdir -p $xcache_folder
# create new XAUTHORITY cookie file
touch $xserver_cookie
xauth add :$display_number . $(mcookie)
cookie=$(xauth nlist ":$display_number" | sed -e 's/^..../ffff/')
echo $cookie | xauth -f "$xserver_cookie" nmerge -
function cleanup ()
{
kill $clip_forwarder_pid || true > /dev/null 2>/dev/null
kill $xephir_pid || true > /dev/null 2>/dev/null
}
trap cleanup EXIT
function clipForward()
{
CLIP1=""
CLIP2=`xsel -o -b --display $2`
while true; do
CLIP1NEW=`xsel -o -b --display $1`
CLIP2NEW=`xsel -o -b --display $2`
if [ "x$CLIP1" != "x$CLIP1NEW" ]; then
xsel -o -b --display $1 | XAUTHORITY=$3 xsel -i -b --display $2
CLIP1=$CLIP1NEW
fi;
if [ x"$CLIP2" != x"$CLIP2NEW" ]; then
XAUTHORITY=$3 xsel -o -b --display $2 | xsel -i -b --display $1
CLIP2=$CLIP2NEW
fi
sleep 1
done
}
Xephyr :$display_number \
-auth $xserver_cookie \
-extension MIT-SHM \
-nolisten tcp \
-screen 800x600 \
-resizeable &
xephir_pid=$!
sleep 1
clipForward ":0" ":$display_number" "$xserver_cookie" &
clip_forwarder_pid=$!
sudo /run/current-system/sw/bin/systemd-run \
--property PAMName=login \
--property User=$target_user \
--property StandardInput=tty \
--property Environment=DISPLAY=:$display_number \
--property Environment=XAUTHORITY=$xclient_cookie \
--uid=$target_user \
-t bash -c "echo $cookie | xauth -f $xclient_cookie nmerge - && ~/.xsession" > $HOME/log 2>$HOME/logerr
cleanup
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment