简体   繁体   中英

Sudo without password to run dd command Python

So i've searched through all the questions on this site (maybe not all, but most), and none of them have quite the right answer for what i'm looking for!

Part of my code, in Python, is setup as:

specialstring = special

if input == "T"
    trash = commandline("sudo dd if=zero blahblah%blah" % specialstring) 

Every time I run the command, it asks for a password. I do not want it to request a password! I don't need a lecture on how unsafe it is to run a root without a password (or however your phrase it)... I would just like to know what to do to have my code not need a password to run the command, and then exit root after the command so I can continue on normally with the rest of my code. THANKS!

-NOOB

As mentioned in the comments of your previous question, you need to add to your sudoers file like this for the sake of simplicity just turn off the passwords and you will be able to run this without a password

admin ALL = NOPASSWD: ALL

the file will be found in /etc/sudoers

Then your python script will run fine and not require a password

Here is a tutorial on how to use visudo which is what you will need to use to edit sudo password settings

You can make a "user" not have to enter in the password when using sudo for a specific program by editing the /etc/sudoers file.

Open up the file and edit it to include this line (where is the name of the user):

<user> ALL = NOPASSWD: /bin/dd

Why would you need to run 'dd' as root anyway? The 'dd' program is available to all users, so running the same command without 'sudo' in front would be sufficient. The only reason I can think of is if the file where you write the output of 'dd' to is only writable by root or another user.

So, try this:

specialstring = special

if input == "T"
    trash = commandline("dd if=zero blahblah%blah" % specialstring) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM