How do I run X windows program as normal user?

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 -
Password
# xeyes

It will bump you back with an error:
(program:15082): Gtk-WARNING **: cannot open display:
OR
** 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 $@'

Add above alias to your bash startup script

$ echo "alias runas='su -c $@'" >> .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

vivek@debian:~$ su -
debian:~#

Step # 2 Setup variables

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

Step # 3 Execute X program as a root user

# xeyes
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: How do I find out what network services are running or listing under Linux?

Next post: How do I find the url for my cgi-bin?