Aug 10

Pass DISPLAY when using su and not -p

I was aggravated. I recently switched distros, and by default su didn’t pass the DISPLAY variable, and when I used su -p it didn’t cd to their $HOME it cd to my home, the ‘parent’ shell’s home.

So I concentrated on other things. Got my sound card set up, Installed flash, mplayer, and quite a few other things.

Then it was time. It bothers you when that 1 little thing isn’t working right.

su was my thing.

After spending some time in chat rooms, and asking questions, I decided that google was the next best guess. Honestly I thought the solution was something as simple as changing a line in some config file in /etc. I was wrong….

I found some obscure pdf file that had most of the answers.

From there I filled in the blanks.

I added these lines

/etc/bash.bashrc

alias su='suwrap.sh'

if [ -e "/tmp/.tmp.x.key" ] 
then
	TMP_KEY=`cat /tmp/.tmp.x.key`
	TMP_DISPLAY=`cat /tmp/.tmp.x.display`
	if [ -n "$TMP_KEY" ]
	then
		if [ -n "$TMP_DISPLAY" ]
		then
			xauth merge /tmp/.tmp.x.key
			export DISPLAY=`cat /tmp/.tmp.x.display`
		fi
	fi
	# clean it up we don't want anyone else getting into the display
	echo -n > /tmp/.tmp.x.key
	echo -n > /tmp/.tmp.x.display
	rm /tmp/.tmp.x.key -f &>/dev/null
	rm /tmp/.tmp.x.display -f &>/dev/null
fi

Then I wote a wrapper.

/bin/suwrap.sh

#!/bin/sh

if [ -n "$DISPLAY" ]
then
	xauth extract /tmp/.tmp.x.key $DISPLAY
	echo -n $DISPLAY > /tmp/.tmp.x.display
else 
	echo -n > /tmp/.tmp.x.key
	echo -n > /tmp/.tmp.x.display
fi
chmod 777 /tmp/.tmp.x.key &>/dev/null
chmod 777 /tmp/.tmp.x.display &>/dev/null
if [ -n "$*" ]
then
	su "$*"
else 
	su
fi
echo -n '' > /tmp/.tmp.x
echo -n '' > /tmp/.tmp.x.display
rm /tmp/.tmp.x -f &>/dev/null
rm /tmp/.tmp.x.display -f &>/dev/null


See that wasn’t that hard was it?

I’ve run a few tests and su -c “make install” works fine. As well as su -c ‘kate’
So I now have my display, and I can run whatever I want in X without having to add su -p and have it mess up my $USER and $HOME variables.

It might seem a little stupid to try and remove, and echo to it twice, but seriously … do you want to take any chances?

I’m not sure if this is really secure, but who knows, it works.

Erm

One Response to “Pass DISPLAY when using su and not -p”

  1. erm says:

    There is a MUCH easier way.

    ~/.bashrc
    xhost +local:$DISPLAY

    if you don’t like the message every time

    ~/.bashrc
    xhost +local:$DISPLAY &>/dev/null

Leave a Reply

You must be logged in to post a comment.