Running python script as root

68,876

So you want the script to run as root, even without sudo? For that you would need to set the setuid bit on the script with sudo chmod u+s program. However, most Unix distributions allow this only for binaries, and not for scripts, for security reasons. In general it's really not a good idea to do that.

If you want to run this script as root, you will have to run as sudo. Or, you have to create a binary that runs your script, so that you can set the setuid bit on this binary wrapper. This related question explains more.

It's also a good idea to check the effective uid, and if it's not root then stop running. For that, add this near the top (thanks @efirvida for the tip!)

if not os.geteuid() == 0:
    sys.exit("\nOnly root can run this script\n")

ORIGINAL ANSWER

Maybe your user and root use a different version of python, with different python path, and different set of libraries.

Try this:

command -v python
sudo command -v python

If the two commands don't give the same result then you either need to change the setup of the users to use the same version of python (the one that has the ALSA libs), or hardcode the python version the first line of the script.

Also try adding a print sys.path line in the script, and run with your user and with sudo and compare. Probably you'll get different results. You may need to tweak the PYTHONPATH variable of your user.

It shouldn't be necessary to make the owner of the script root, and to run it with sudo. You just need to configure python and PYTHONPATH correctly.

Share:
68,876
moesef
Author by

moesef

Mohammad Samman recently earned his B.S. in Bioengineering from UC San Diego, a top ten school for this degree. Now that he has graduated, he is working to gain some good experience in the Biotechnology industry. Mohammad Samman is happy with his current position at iHear medical, but is always open to networking and making industry connections. While not at work, Mohammad Samman is teaching himself various programming languages to have some experience in web and mobile development, which Mohammad feels are necessary skills to for anyone in the technology field, and believes it will help him in pursuing his goals of one day starting his own tech company. Mohammad Samman plans to one day return to school to gain a dual Master's in Computer Science and is excited to for a long and fulfilling career.

Updated on January 30, 2022

Comments

  • moesef
    moesef over 2 years

    I have the following script:

    #!/usr/bin/env python                                                           
    
    import sys                                                                      
    import pyttsx                                                                   
    
    def main():                                                                     
            print 'running speech-text.py...'                                       
            engine = pyttsx.init()                                                  
            str = "Hi..."                                    
            if len(sys.argv) > 1:                                                   
                    str = sys.argv[1]                                               
            engine.say(str)                                                         
            engine.runAndWait()                                                     
    
    if __name__ == '__main__':                                                      
            main() 
    

    and I have placed it in /usr/bin/speech-test.py

    I have also given it executable permissions and ownership to root:

    sudo chown root:root /usr/bin/speech-test.py
    sudo chmod 4755 /usr/bin/speech-test.py
    

    However, this script will only run correctly if I run as sudo speec-test.py. If I try to run it as just speech-test.py it complains about not finding a bunch of ALSA lib files.

    Am I missing something to have my script run with root privileges?

  • m.wasowski
    m.wasowski about 10 years
    That's not true if you have sticky bit set, as OP tries.
  • Hot.PxL
    Hot.PxL about 10 years
    Oh yeah I forgot about that... Sorry
  • jww
    jww over 4 years
    which was changed to command -v. There are several reasons to use command -v over which. Please revert if you don't want the change. Also see How to check if command exists in a shell script? and How to check if a program exists from a Bash script?
  • PeterT
    PeterT about 3 years
    Unless packages are installed globally (accessible to sudo) this won't work, and there is a trend to keep packages local.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • lonelyjoe
    lonelyjoe about 2 years
    (Ubuntu 22.04) This didn't work for me. I get the same "Permission Denied" error.
  • hDmtP
    hDmtP about 2 years
    Do you have multiple python interpreters? If yes, then u may need to specify the version like python3