Running X11 Apps When Using sudo

So you have your nice shiny linux box, and you are properly not allowing remote root logins, but you still need to be able to run X11 apps as root through ssh. But when you do, you get the dreaded error like:

Gtk-WARNING **: 19:41:13.383: cannot open display: localhost:10.0

Well that is a pain. This is because X11 has an authorization token to prevent other users from hijacking your session. And annoying as it can be, it is a good thing. Anyone old enough to have used X11 back before they implemented it will remember roaches or snow randomly appearing on their screens.

Well, here is a quick and dirty solution. Just run this little snippet of code once you have used “sudo -i” to get your root shell. It will figure out who you logged in as, and then extract the token(s) from your .Xauthority file, and put it into root’s so you can now run X11 apps as root in your unprivileged X11 session.

su $( who am i | cut -f 1 ) -c "xauth list" | while read USERKEY
	xauth add $USERKEY

You can add this to your root shell’s profile to have it done automatically. Keep in mind that this solution will allow anyone with sudo privileges to send X11 windows to your session.

Leave a Comment