Friday, May 10, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2251  / 1 Year ago, thu, february 16, 2023, 4:27:53

I am trying to allow a specific user to run a specific .sh file that has a sudo chmod inside of it.



I have followed this guide: How to allow a command to be executed for a particular user without password with sudoers file?



My sudoers file now looks like this:



the_user ALL=(ALL:ALL) NOPASSWD: /var/www/html/storage/fix_cache_permissions.sh


However, when I run the command:



sh /var/www/html/storage/fix_cache_permissions.sh


it dies with this error:



sudo: no tty present and no askpass program specified


Update



I tried both having sudo inside the .sh file and outside when running the command. Both results in the same error message.


More From » command-line

 Answers
7

You are missing something here. The sudoers file refers to the sudo command. By running sh yourprogram.sh you are not using the sudo command, you are using the sh one which in turn will use the sudo afterwards.



So the correct way to accomplish what you want is by marking the script as executable so that the user then can write



sudo /var/www/html/storage/fix_cache_permissions.sh


and this will not ask for the password.



Notes:




  1. Since you are getting this error that means that you are not running the command from some sort of terminal. If it doesn't ask for password then I think that this will not be a problem.


  2. The order of the commands in the sudoers file is important and they override one another. Meaning that if you write you command and afterwards in the file there is something like this:



    %sudo   ALL=(ALL:ALL) ALL


    This will override anything. So put your line in the last lines of the sudoers file.


  3. Using this method you can actually remove the sudo inside the file since all the script will run as root.



[#4861] Thursday, February 16, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anoalk

Total Points: 271
Total Questions: 90
Total Answers: 112

Location: Zambia
Member since Wed, Dec 16, 2020
3 Years ago
anoalk questions
Wed, Mar 15, 23, 04:06, 1 Year ago
Fri, Sep 24, 21, 02:59, 3 Years ago
Sat, Mar 5, 22, 02:33, 2 Years ago
Tue, Mar 22, 22, 07:19, 2 Years ago
;