Friday, April 26, 2024
32
rated 0 times [  32] [ 0]  / answers: 1 / hits: 57563  / 1 Year ago, sat, january 14, 2023, 6:03:13

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?


More From » command-line

 Answers
3

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.


[#42554] Sunday, January 15, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
herriail

Total Points: 11
Total Questions: 122
Total Answers: 118

Location: Peru
Member since Tue, Mar 16, 2021
3 Years ago
herriail questions
Tue, May 18, 21, 02:11, 3 Years ago
Sun, Dec 12, 21, 06:06, 2 Years ago
Wed, May 24, 23, 01:42, 1 Year ago
Sun, Jan 29, 23, 00:48, 1 Year ago
;