0

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

ohhiloveyouu
  • 101
  • 3
  • 8
  • 1
    "How do I get `sudo` not to request a password" is a configuration issue, not a programming question. [This page](http://www.howtogeek.com/116757/8-ways-to-tweak-and-configure-sudo-on-ubuntu/) has some info you might like. – kindall Jul 12 '13 at 17:33
  • well i'm wondering what code i'll need to get around needing a password... like admin ALL = NOPASSWD: ALL @kindall – ohhiloveyouu Jul 12 '13 at 17:34

3 Answers3

1

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

Stephan
  • 16,509
  • 7
  • 35
  • 61
  • I don't know how to add to my sudoers file via python? I understand what I need to add, but I don't understand all this sudoers stuff – ohhiloveyouu Jul 12 '13 at 17:48
  • click on my link for a more detailed explanation. You aren't doing this in python, you are doing this in linux so that your python will work – Stephan Jul 12 '13 at 17:52
  • Okay, I understand that. My last question would be IS there anyway to do it in python? So, in my script can I code for my command to bypass a password WITHOUT having to mess with my systems configuration file or whatever? – ohhiloveyouu Jul 12 '13 at 18:04
  • You don't **need** to use visudo, but it's a nice way of doing it. Personally, I would open with `vi /etc/sudoers`, edit whatever, and then save with `:w !sudo tee /dev/null %`. – mazunki Jun 16 '19 at 21:40
1

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
Atari911
  • 183
  • 1
  • 6
  • Sorry if this question is dumb/doesn't make sense, but would I put that right before the dd command (above it)? or at the beginning of my whole code. I don't know all the lingo yet, so I don't even know if I am in a file.. – ohhiloveyouu Jul 12 '13 at 17:40
  • It goes in your `sudoers` file (i.e., your system's configuration file for the `sudo` command) not in your script. This is not a programming question. – kindall Jul 12 '13 at 17:47
  • As @kindal said... you will want to edit the file named 'sudoers' that is located in the etc/ directory on your machine. This will tell the sudo program to not prompt this user for a password when the user attempts to run the specified program using the sudo command. – Atari911 Jul 12 '13 at 17:56
  • Is there anyway to get around a password in my script? As in, is there any code I can write in my script to get around a password and NOT have to go into my system's configuration? – ohhiloveyouu Jul 12 '13 at 17:57
  • 1
    It would kind of defeat the entire point of `sudo` if you could get around it by writing a script. – kindall Jul 12 '13 at 18:01
  • I see what you are asking though: you want to make the script portable between machines? – Atari911 Jul 12 '13 at 18:02
  • @Atari911 I didn't see your comment before I posted my last one. The way you described it just made a little more sense to me. I'm using only one machine, and do not believe my code needs to be 'portable'.. I'll keep reading about it until I figure it out though. Thanks for your comment!! – ohhiloveyouu Jul 12 '13 at 18:11
  • yea, then you should be good just editing the file (not your script) as shown. Your script should then run without prompting for a pw. – Atari911 Jul 12 '13 at 18:13
0

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) 
Plenka
  • 1,099
  • 7
  • 17
  • I already tried that (originally), and I got this message- dd: opening '/dev/sdb': Permission denied – ohhiloveyouu Jul 12 '13 at 18:07
  • What is the exact commando then? Sounds like you want to write something to /dev/sdb, which is a hard disk block device file. Are you trying to wipe a hard drive or something like that? – Plenka Jul 12 '13 at 18:31