I’m running a bash srcipt using “sudo script.sh” beacause some of the commands inside script requires privileged access. How can I remove sudo while running the script but give access to commands inside the script?
1 Answer
Instead of running the script with sudo, use sudo within the script for those specific commands.
The first command will prompt the user for their password. But sudo keeps an authentication cache, so it won't ask again for several minutes on the same terminal. As long as the script doesn't go too long between commands that need privilege, it shouldn't ask multiple times.
18 Comments
Devops things
Thanks....Can I remove a “sudo required command” from a for loop and call it from another script
Barmar
Not sure what you mean by that, but why would it make a difference which script it's in?
Devops things
I want to create script only with specific commands that require sudo
Barmar
What does that have to do with a
for loop? Maybe you should ask a new question that shows the details of what you're asking about.Barmar
You can put
sudo script2.sh inside script.sh. Everything in script2.sh will be run as root. |