Is there any way to undo the sudo
within the script?
like..
sudo ./script.sh
commands 1,2,3,4,5 with sudo
rights
commands 6,7,8,9 without sudo
rights
Is there any way to undo the sudo
within the script?
like..
sudo ./script.sh
commands 1,2,3,4,5 with sudo
rights
commands 6,7,8,9 without sudo
rights
If you have to run your script with sudo, you can still run some commands as a normal user using:
sudo -u $SUDO_USER <command>
If you run the following script with sudo
:
#!/bin/bash
whoami
sudo -u $SUDO_USER whoami
It will output:
root
sylvain
See the manual for sudo:
-k [command]
When used alone, the -k (kill) option to sudo invalidates the user's cached credentials. The next time sudo is run a password will be required.
So put a sudo -k
before every sudo
command you want to re-ask for a password. Or behind every command where you want the next commands not to be sudo
.
By the way: if you ask for a password inside the script you should use ...
./script.sh
and not with sudo
in front of it.