Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:43:29+00:00 2026-05-22T22:43:29+00:00

I have been playing around with subprocess lately. As I do more and more;

  • 0

I have been playing around with subprocess lately. As I do more and more; I find myself needing root access. I was wondering if there is an easy way to enter the root password for a command that needs it with subprocess module. So when I am prompted for the password my script and provide it and run the command. I know this is bad practice by where the code will be running is sandboxed and separate from the rest of the system; I also dont want to be running as root.

I would really appreciate small example if possible. I know you can do this with expect, but i am looking something more python centric. I know pexpect exsists but its a bit overkill for this simple task.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:43:30+00:00Added an answer on May 22, 2026 at 10:43 pm

    It would probably be best to leverage sudo for the user running the Python program. You can specify specific commands and arguments that can be run from sudo without requiring a password. Here is an example:

    There are many approaches but I prefer the one that assigns command sets to groups. So let’s say we want to create a group to allow people to run tcpdump as root. So let’s call that group tcpdumpers.

    First you would create a group called tcpdumpers. Then modify /etc/sudoers (using the visudo command):

    # Command alias for tcpdump
    Cmnd_Alias      TCPDUMP = /usr/sbin/tcpdump
    
    # This is the group that is allowed to run tcpdump as root with no password prompt
    %tcpdumpers     ALL=(ALL) NOPASSWD: TCPDUMP
    

    Now any user added to the tcpdumpers group will be able to run tcpdump like this:

    % sudo tcpdump 
    

    From there you could easily run this command as a subprocess.

    This eliminates the need to hard-code the root password into your program code, and it enables granular control over who can run what with root privileges on your system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing around with the twisted framework for about a week now(more
I have been playing around lately with head.js but I have found a stranger
I have been playing around with the concept of 'module' that some mvc frameworks
I have been playing around with some debugging and wrote some C code that
I have been playing around with these CSS3 properties and I've been able to
I have been playing around with node.js, and coming from a Java background, I
I have been playing around with haskell and I found out that if I
I have been playing around with the WPF GridView control (DesktopUI not Silverlight) and
so I have been playing around with django for a bit and I really
So I have been playing around with the facebook like button but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.