0

I'm trying to run a script without ask for root password. So i have a script.sh with several sudo commands inside and I've modified the file sudoers with "sudo visudo" to be able to run the script without ask for root password :

%sudo ALL=(ALL:ALL) ALL root ALL=(ALL) NOPASSWD: /home/user/script.sh

as well I've change the user and group of my script (sudo chown root.root script.sh) and change the as well (sudo chmod 777 script.sh) , but always that I try to run my script it ask for root password, and I don't idea what can it be

Note: I tried as well change this > user ALL=(ALL) NOPASSWD: /home/user/script.sh , and the user,group to (user) and I still have the same issue.

1

1 Answer 1

1

remove all sudo commands in your script. Just put the below lines at the beginning of your script

#!/bin/bash
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Aborting: run as root user!"
    exit 1
fi

#... your program

This makes the whole script to be run with super user. and none of the commands in your script needs sudo prefix.

Sign up to request clarification or add additional context in comments.

5 Comments

with you code when try to run the script with your cide doesnt allow me run it, and when i remove the code the command inside in the script show me the error : sed: can't read /etc/sudoers: Permission denied, i've remove all sudo commands inside
Its not allowing you to run because, Initially you are asking the user to run your script with super user privilage. i.e. if your script file is test.sh and needs sudo privilege to run few commands. By adding the given lines, instead of ./test.sh , the script has to be run with sudo ./test.sh and provide the password initially.
yes, but the thing is that i need to run this script at startup on ubuntu without that the user run the script , for that reason i've added it to sudoers file, but it doesnt works yet
Oh! at startup, okay check this answer askubuntu.com/questions/290099/… hope it will solve your problem.
the link works, it execute any script at startup, but it has issues when the script has sudo commands inside or any command that needs to be run as sudo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.