Linux – Can Root Execute Command as Non-Root User?

linuxsuuser

I am a root user, and suppose I want to run any application as another user. Is this 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() within the program to invoke a GUI app. But I don't want to run it as root, so I want to temporarily drop root privileges only for that call.

Best Answer

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
Related Question