How do I run X windows program as normal user?

Posted on in Categories last updated August 28, 2006

Microsoft windows XP has runas command which allows a user to run specific tools and programs with different permissions than the user’s current logon provides. Linux and other UNIX like operating system provides the su or sudo command for same purpose. However, su/sudo command is not so useful when it comes to X program. For example when you logged in as a normal user, you need to run an X window application as root. If I run application as follows:

$ su -
# xeyes

It will bump you back with an error:
(program:15082): Gtk-WARNING **: cannot open display:
** WARNING ** cannot open display

However both KDE and Gnome come with tools to deal with this problem.

Method # 1:
If you are using KDE then use following command at shell prompt:
kdesu command-name

$ kdesu xeyes

Method # 2:
If you are using Gnome then use following command at shell prompt:

$ gksuexec

Or use GUI itself, click on Applications > System tools > Select Run as different user
Method # 3:
Create runas alias as follows:

$ alias runas='su -c [email protected]'

Add above alias to your bash startup script

$ echo "alias runas='su -c [email protected]'" >> .bash_profile

You can now use alias as follows to start any x program

$ runas program-name
$ runas xeyes

Method # 4: The old way
The problem is with two environment variable DISPLAY and XAUTHORITY. You need to setup them correctly to run X windows program as a root user while logged in as a normal user. So how do you fix this problem? Simply set these two variables to point to current logged in users environment variable. Let us assume you are currently login as vivek user.

Step # 1 Become super-use

[email protected]:~$ su -

Step # 2 Setup variables

# export DISPLAY=0:0
# export XAUTHORITY=/home/vivek/.Xauthority

Step # 3 Execute X program as a root user

# xeyes

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment