Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I can't run firefox from a sudoed python script that drops privileges to normal user. If i write

# sudo python
>>> import os
>>> import pwd, grp
>>> uid = pwd.getpwnam('norby')[2]
>>> gid = grp.getgrnam('norby')[2]
>>> os.setegid(gid)
>>> os.seteuid(uid)
>>> import webbrowser
>>> webbrowser.get('firefox').open('www.google.it')
True
>>> # It returns true but doesn't work
>>> from subprocess import Popen,PIPE
>>> p = Popen('firefox www.google.it', shell=True,stdout=PIPE,stderr=PIPE)
>>> # Doesn't execute the command

I think that is not a python problem, but firefox/iceweasel/debian configuration problem. Maybe firefox read only UID and not EUID, and doesn't execute process because UID is equal 0. What do you think about?

share|improve this question
    
But why are you trying to write a python script to be ran as root to do this. What is the objective? –  theotherreceive Jul 15 '09 at 20:27
    
The script works with network in a low level layer, so it must run as root. I think there is nothing wrong to drop privileges in the right way, and then execute firefox. But it doesn't works. –  Emilio Jul 15 '09 at 20:39
    
Does Python's Popen work with gui programs? Perhaps the problem is not where you think it is. –  Telemachus Jul 15 '09 at 20:55
    
Firefox will run as root, so the concern you have in the last part of your question isn't the issue. –  Avery Payne Jul 15 '09 at 20:56
    
Is it a problem if I cross post it on stackoverflow? –  Emilio Jul 15 '09 at 22:01

6 Answers 6

up vote 1 down vote accepted

You have to export environment variable named DISPLAY with value ':0.0'. That might make it work. Ask on stackoverflow if you don't know how to export environment variables using python.

share|improve this answer

The problem is it can't access the display on the X server, is it running? I wouldn't recommend running firefox with root permissions, that'd be like running IE on a windows box.

You said in a comment that you was launching it in a lower run level. The error is because you're launching firefox in that lower level before X comes up with an active display. Delay it's running until X is up.

share|improve this answer
1  
+1 for "like IE on windows" –  theotherreceive Jul 15 '09 at 20:31
    
X server is running, and i'm not running firefox from root privilege, but i've dropped it to 'norby' normal user. –  Emilio Jul 15 '09 at 20:43
    
is it running at execution time? –  Malfist Jul 15 '09 at 20:45
    
sure! =) I'm not a linux newbie –  Emilio Jul 15 '09 at 20:46
    
Can the user launch it normally? Can you launch it with fakeroot? –  Malfist Jul 15 '09 at 20:48

You really really shouldn't do this. Like, really.

Why are you trying to?

share|improve this answer

First, you would normally want to use gksu or gksudo or sux to get root privileges for an X application. Second, why are you using sudo on Debian? (I mean is sudo even enabled? It's not by default on Debian, as opposed to Ubuntu or OS X.) Third, I can't think of any circumstance where you actually need to run a browser with root privileges.

There are cases when you need a gui application as root - say, you want to edit a system file (/etc/network/interfaces for example), and you're most comfortable editing with gedit. That's not an unreasonable thing to want to do. But you would want to use gksu or a similar utility to transfer the privileges.

share|improve this answer
    
yes, sudo is enabled, but no one is a sudoer –  Malfist Jul 15 '09 at 20:28
    
@Malfist: 6 of one; half-dozen of the other. I take your point, but since in fact many Debian users don't install the Desktop task at all, sudo is often not even installed. Nevertheless, I grant that if you install the Gnome (or KDE or XFCE, I suppose) packages, then sudo gets installed. It's still not the right tool to get gui privileges. –  Telemachus Jul 15 '09 at 20:31
    
Sudo is installed in debian system. I have to execute a browser from a python script that must run as root. I'm only trying to drop privilege and run the browser. Nothing dangerous. –  Emilio Jul 15 '09 at 20:32
    
@Emilio: First, the whole Python script-which-must-be-root -> Firefox idea still sounds like a bad idea. Second, are you sure that your regular user is in the sudoers file? Again, Debian is not Ubuntu. Even if you run Gnome, I don't think that regular users are automatically added to the list of users who can run sudo. I believe that out of the box in Debian only root can. –  Telemachus Jul 15 '09 at 20:34
    
Sudo is not the problem (the user will execute the program as he want, with sudo, gtksudo, kdesudo, etc.), but the problem is that the browser don't works even if i drop privileges in the right way.. –  Emilio Jul 15 '09 at 20:37

I use sux.

  1. apt-get installl sux
  2. sux firefox

Description: wrapper around su which will transfer your X credentials Sux is a wrapper around the standard su command which will transfer your X credentials to the target user.

share|improve this answer
    
I prefer to find a solution not based on what *su program is used, but to try to run firefox from the script executed by root, but with dropped privileges –  Emilio Jul 15 '09 at 20:47

I'd guess, you'll have to specify an X-server. I'm afraid I have no idea how to actually do that in python though :-)

share|improve this answer

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.