When you do sudo to another user, you may have to do extra things to get X display working or any GUI working.

Many organizations do not allow root login so X window cookie does not get generated and hence GUI does not work.

I prayed to Google God for solutions and found many excellent solutions. I just took the cream of it and implemented in my Linux desktop to run X without any issue for any user that we login as sudo or su.

As soon as server is rebooted, it uses a different X cookie for authentication.

Their is no root login permitted in my Linux desktop and I need to do GUI stuff as root or any other user other than my login user.

I am a heavy user of GNOME-Terminal. For example, my login id is “pegasus”

So, I created in my home directory /home/pegasus, I did the following.

$ mkdir -p ./config/autostart

And, create this file:

$ cat gnome-terminal.desktop 
[Desktop Entry]
Exec=gnome-terminal -e '/home/pegasus/bin/setrootdisplay'
Name=GNOME Terminal
Comment=Use the command line

The contents of setrootdisplay is as follows:

xauth list | grep `uname -n` > /tmp/setxdisplay.cookie

So everytime server reboots, a new cookie file is generated for the login user automatically and stored in /tmp as setxdisplay.cookie.

Now, I created a file xsudo.sh in /etc/profile.d so that it will get executed automatically whosoever logins as sudo or su.

Contents of xsudo.sh

if [ -f /tmp/setxdisplay.cookie ] && [ "$USER" != "pegasus" ] ; then
   xauth add `cat /tmp/setxdisplay.cookie`

And, I am good as I can then run X display if I do sudo as root or any other user.

Of course, this solution may not be liked by those who are always paranoia about security and I do not care so it works best for me.

The above solution works well in RHEL 6.7 and I have not tested it on any other platform so do not know if it will work in other flavors of Linux or not.

But, you may give it a try.

I do not allow comments as I write things for my own reference and if they are useful to others like you, thanks for visiting my site.