Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am writing a Java application where i need to do a command line execution and get a result back, but when i execute the command, it ask for sudo password. So far i tried:

$ sudo -s
$ vim /etc/sudoers
# User privilege specification
root         ALL=(ALL:ALL) NOPASSWD: ALL
javauser     ALL=(ALL:ALL) NOPASSWD: ALL

:wq
$ 4 -r--r-----   1 root root     615 2011-10-26 09:23 sudoers

Once i execute the command it again asks "[javauser] password for javauser:". But i already mentioned noPASSWD.

whoami returns alex and I am adding it as this in the sudoers file

# User privilege specification
root    ALL=(ALL:ALL) ALL
alex ALL=NOPASSWD: ALL


# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

Running keeps asking me for my password, any ideas?

share|improve this question
    
Looks like there's a very similar question already: askubuntu.com/questions/39281/… - have you tried the answer from there? –  Sergey Oct 26 '11 at 8:06
    
Make sure that in your java code you use "sudo /absolute/path/to/command" in your system call. Btw. for security reasons you should use sudo visudo instead of vim to edit the sudoers file. That way if you make a mistake the program warns you about it. –  con-f-use Oct 26 '11 at 8:08
    
Can be found here what exactly i tried: gist.github.com/1315951 –  YumYumYum Oct 26 '11 at 10:29

1 Answer 1

up vote 8 down vote accepted

You need to do the following, on the terminal type sudo visudo and add a line like this at the end of the file specifying the commands you want to run without typing the sudo password (I suggest you use each command you want to use in the program and not just allow all programs to be executed by this)

<yourusername> ALL=NOPASSWD: <command1>, <command2>

Now you can run the specified commands without password as long as you type that command with sudo.

ie: lets say you want to run shutdown -r now without having to type sudo password every time and your username is 'joedoe'

  1. type sudo visudo on a terminal

  2. Add joedoe ALL=NOPASSWD: /usr/sbin/shutdown -r now as a new line at the end of the file, use absolute paths to the program you are trying to use.

  3. on your program you can then use sudo shutdown -r now without having to type the sudo password.

You can find the absolute path to a program by using which <program name> on a terminal.

Its a very dirty trick wish leaves your system open for other dangers but I am guessing you know what you are doing and want this.

Edit

You really need to make sure that the permitions you are setting are at the end of the file so that nothing is overwritten by the groups permissions.

share|improve this answer
    
Seems like he already did that. –  con-f-use Oct 26 '11 at 8:11
    
If he already did that why are you explaining him that he should use sudo visudo and not vim to edit the sudoers file? If he did this it would work. –  Bruno Pereira Oct 26 '11 at 8:15
    
@goOgle are you typing sudo in front of the the command inside your code? you need to do it even if you forced nopasswd in sudoers file. ie: use sudo <command> not only <command>. –  Bruno Pereira Oct 26 '11 at 8:39
1  
For me cmd="whoami" returns bruno and cmd="sudo whoami" returns root –  Bruno Pereira Oct 26 '11 at 10:42
2  
Found it! Can reproduce this if my line is not on the end to the end of the sudoers the file. After the groups assignment, etc. Really the last line! Move the line to be the last one in the file! (hope this was it). –  Bruno Pereira Oct 26 '11 at 12:18

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.