shell script: use sudo inside it vs run it with sudo?

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
When writing a shell script, in which some but not all commands in it need superuser privileges, shall I
add sudo to those commands which need superuser privileges, and run the shell script without sudo, or
don't add sudo to those commands which need superuser privileges, but run the shell script with sudo?
In the second way I will only need to provide my password once, but all the commands in the script will be run with superuser privilleges, including those commands which don't need.
In the first way I may need to provide my password multiple times for different sudo commands, while the superuser privileges are granted only to those commands which need them.
From security concern, the first way is better. For convenience, the second way is better.
I have been thinking of adopting the first way. So I have to deal with the inconvenience of providing my passwords to multiple sudo commands in the shell script.
Stephen Harris wrote at https://unix.stackexchange.com/a/478786/674:
A well written script would detect if it was running with the right permissions and not call sudo at all, but there's a lot of bad scripts
So should I use the second way? If so,
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
Thanks.
shell-script scripting sudo root
add a comment |Â
up vote
1
down vote
favorite
When writing a shell script, in which some but not all commands in it need superuser privileges, shall I
add sudo to those commands which need superuser privileges, and run the shell script without sudo, or
don't add sudo to those commands which need superuser privileges, but run the shell script with sudo?
In the second way I will only need to provide my password once, but all the commands in the script will be run with superuser privilleges, including those commands which don't need.
In the first way I may need to provide my password multiple times for different sudo commands, while the superuser privileges are granted only to those commands which need them.
From security concern, the first way is better. For convenience, the second way is better.
I have been thinking of adopting the first way. So I have to deal with the inconvenience of providing my passwords to multiple sudo commands in the shell script.
Stephen Harris wrote at https://unix.stackexchange.com/a/478786/674:
A well written script would detect if it was running with the right permissions and not call sudo at all, but there's a lot of bad scripts
So should I use the second way? If so,
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
Thanks.
shell-script scripting sudo root
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
When writing a shell script, in which some but not all commands in it need superuser privileges, shall I
add sudo to those commands which need superuser privileges, and run the shell script without sudo, or
don't add sudo to those commands which need superuser privileges, but run the shell script with sudo?
In the second way I will only need to provide my password once, but all the commands in the script will be run with superuser privilleges, including those commands which don't need.
In the first way I may need to provide my password multiple times for different sudo commands, while the superuser privileges are granted only to those commands which need them.
From security concern, the first way is better. For convenience, the second way is better.
I have been thinking of adopting the first way. So I have to deal with the inconvenience of providing my passwords to multiple sudo commands in the shell script.
Stephen Harris wrote at https://unix.stackexchange.com/a/478786/674:
A well written script would detect if it was running with the right permissions and not call sudo at all, but there's a lot of bad scripts
So should I use the second way? If so,
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
Thanks.
shell-script scripting sudo root
When writing a shell script, in which some but not all commands in it need superuser privileges, shall I
add sudo to those commands which need superuser privileges, and run the shell script without sudo, or
don't add sudo to those commands which need superuser privileges, but run the shell script with sudo?
In the second way I will only need to provide my password once, but all the commands in the script will be run with superuser privilleges, including those commands which don't need.
In the first way I may need to provide my password multiple times for different sudo commands, while the superuser privileges are granted only to those commands which need them.
From security concern, the first way is better. For convenience, the second way is better.
I have been thinking of adopting the first way. So I have to deal with the inconvenience of providing my passwords to multiple sudo commands in the shell script.
Stephen Harris wrote at https://unix.stackexchange.com/a/478786/674:
A well written script would detect if it was running with the right permissions and not call sudo at all, but there's a lot of bad scripts
So should I use the second way? If so,
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
Thanks.
shell-script scripting sudo root
shell-script scripting sudo root
edited 1 hour ago
Vlastimil
7,0791152126
7,0791152126
asked 2 hours ago
Tim
24.4k69238426
24.4k69238426
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
To address your first issue:
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
There is a simple and POSIX check for root:
#!/bin/sh
AmIRoot()
[ "$(id -u)" -eq 0 ]
Alternatively, in Bash, more performance-driven coders might want to use:
#!/bin/bash
AmIRoot()
Note that I intentionally wrapped the code in functions for re-use.
To address your second issue:
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
You can't do much about this. At least nothing comes to my mind. If I saw the script, I might have suggestions. But since you did not include it in your question... If you run the whole script with sudo or as root, I see no way to control this.
sudo -u "$SUDO_USER" command...?
â Michael Homer
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
To address your first issue:
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
There is a simple and POSIX check for root:
#!/bin/sh
AmIRoot()
[ "$(id -u)" -eq 0 ]
Alternatively, in Bash, more performance-driven coders might want to use:
#!/bin/bash
AmIRoot()
Note that I intentionally wrapped the code in functions for re-use.
To address your second issue:
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
You can't do much about this. At least nothing comes to my mind. If I saw the script, I might have suggestions. But since you did not include it in your question... If you run the whole script with sudo or as root, I see no way to control this.
sudo -u "$SUDO_USER" command...?
â Michael Homer
1 hour ago
add a comment |Â
up vote
3
down vote
To address your first issue:
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
There is a simple and POSIX check for root:
#!/bin/sh
AmIRoot()
[ "$(id -u)" -eq 0 ]
Alternatively, in Bash, more performance-driven coders might want to use:
#!/bin/bash
AmIRoot()
Note that I intentionally wrapped the code in functions for re-use.
To address your second issue:
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
You can't do much about this. At least nothing comes to my mind. If I saw the script, I might have suggestions. But since you did not include it in your question... If you run the whole script with sudo or as root, I see no way to control this.
sudo -u "$SUDO_USER" command...?
â Michael Homer
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
To address your first issue:
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
There is a simple and POSIX check for root:
#!/bin/sh
AmIRoot()
[ "$(id -u)" -eq 0 ]
Alternatively, in Bash, more performance-driven coders might want to use:
#!/bin/bash
AmIRoot()
Note that I intentionally wrapped the code in functions for re-use.
To address your second issue:
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
You can't do much about this. At least nothing comes to my mind. If I saw the script, I might have suggestions. But since you did not include it in your question... If you run the whole script with sudo or as root, I see no way to control this.
To address your first issue:
how can I write "script would detect if it was running with the right permissions and not call sudo at all"?
There is a simple and POSIX check for root:
#!/bin/sh
AmIRoot()
[ "$(id -u)" -eq 0 ]
Alternatively, in Bash, more performance-driven coders might want to use:
#!/bin/bash
AmIRoot()
Note that I intentionally wrapped the code in functions for re-use.
To address your second issue:
how can I improve its security to avoid the problem of giving superuser privileges to commands which don't need them when running the script with sudo?
You can't do much about this. At least nothing comes to my mind. If I saw the script, I might have suggestions. But since you did not include it in your question... If you run the whole script with sudo or as root, I see no way to control this.
edited 1 hour ago
answered 1 hour ago
Vlastimil
7,0791152126
7,0791152126
sudo -u "$SUDO_USER" command...?
â Michael Homer
1 hour ago
add a comment |Â
sudo -u "$SUDO_USER" command...?
â Michael Homer
1 hour ago
sudo -u "$SUDO_USER" command...?â Michael Homer
1 hour ago
sudo -u "$SUDO_USER" command...?â Michael Homer
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479055%2fshell-script-use-sudo-inside-it-vs-run-it-with-sudo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
