Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am root user and suppose i want to run any application as another user. is it possible, without switching to another user.

Something like

# google-chrome user=abc

I am actually executing a cli program as a non root user. I have set the sticky bit on and i am using setuid. So the program runs with root privileges. Now i am using system() with in the program to invoke gui app. But i dont want to run it as root. so i want to temporarily drop root privileges only for that call.

share|improve this question
1  
Are you really running as root most of the time? –  Keith Sep 29 '12 at 13:38
1  
@Keith nothing in the question implies most of the time. –  kojiro Sep 29 '12 at 14:07
    
Or not, that's why I ask for clarification. –  Keith Sep 29 '12 at 18:46
    
Yes that is how the first program for each user is run. The first process on the system is run as root. There are a lot of ways to drop privilege, including all the ways that can escalate privilege, plus some more. –  richard Sep 29 '12 at 21:14
add comment

2 Answers

up vote 7 down vote accepted

A portable solution would be:

su abc -c google-chrome

However, as google-chrome is requiring X11 access, this will likely fail unless you unsecured it, which would be a very bad idea, especially while running as root.

If X11 tunelling/forwarding is allowed, a better way would be

ssh -X abc@localhost google-chrome

or

ssh -Y abc@localhost google-chrome
share|improve this answer
add comment

Short answer: "Yes, this is possible".

if you like to execute a non-X application then just use the following command:

sudo -u abc command

If you like to run some X application as another user but with your own desktop first you need to create a helper script, that will make your life simpler

  • create a bin folder under your home directory:

mkdir -p ~/bin

and using your favorite text editor create a file ~/bin/xsudo as follows:

#!/bin/bash
# (C) serge 2012
# The script is licensed to all users of StackExchange family free of charge
# Fixes/Enhancements to the script are greatly appreciated. 
# 
# SUDO_ASKPASS has to be set to the path of ssh-askpass
# fix the following two lines if your distribution does not match this autodetection
. /etc/profile.d/gnome-ssh-askpass.sh
export SUDO_ASKPASS="${SSH_ASKPASS}"

SUDOUSERNAME="$1"
shift
xauth nlist "${DISPLAY}"|sudo -HA -u $SUDOUSERNAME env --unset=XAUTHORITY \
bash -c "xauth nmerge - ; $*"

then make it executable:

chmod +x ~/bin/xsudo

and use it the same way as sudo but without any switches:

xsudo user application

Enjoy.

P.S. Starting xsession from the root account is strongly discouraged!

share|improve this answer
    
Did you try it ? I'm afraid this particular example can't work. –  jlliagre Sep 29 '12 at 10:56
    
Yes, because in order to start an X application from another user session you have to allow access to you display. But this is also possible. Unfortunately I do not remember how exactly this to be done. –  Serge Sep 29 '12 at 11:03
    
@jlliagre However, I remember how to start an X app on the same host in a tricky way: ssh -X abc@localhost google-chrome :) –  Serge Sep 29 '12 at 11:07
    
Hmm... I'm writing in comments what you already posted 22 mins ago... –  Serge Sep 29 '12 at 11:15
    
But you still have 6 up votes for a non working solution while I only got one for a correct one. StackExchange model is sometimes quite frustrating ... –  jlliagre Sep 29 '12 at 13:24
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.