Run script with arguments as userHow to run a specific program as root without a password prompt?Is there ever a good reason to run sudo su?Prompt for sudo password and programmatically elevate privilege in bash script?Using sudo in openSUSE without actually changing to root user (i.e., like in Ubuntu)How to transition into another domain when invoking sudo?Ubuntu CentOS RHEL cross bash script su supportHow to prevent the caller's shell from being used in sudoRun su -s with argumentslogin with apache userThis user is currently not available - but allow to run script by this usersudo -u not getting path appended in $HOME/.profilesu - user -c env not getting environmental variable without -l added
How to sort human readable size
In a list with unique pairs A, B, how can I sort them so that the last B is the first A in the next pair?
Is this series for Pi correct? And who has done it before?
How "fast" do astronomical events occur?
Run 2 runs, run 2 (can "runs" be dropped in colloquial language) [cricket]
Large-n limit of the distribution of the normalized sum of Cauchy random variables
What could be the physiological mechanism for a biological Geiger counter?
Do details of my undergraduate title matter?
How would one carboxylate CBG into it's acid form, CBGA?
sudo passwd username keeps asking for the current password
reverse a call to mmap()
How would you explain #1 and #2 below using standard quotes?
Synaptic Static - when to roll the d6?
Old time bike. Can I put a rear derailleur?
My student in one course asks for paid tutoring in another course. Appropriate?
Why do you need to heat the pan before heating the olive oil?
What is this word in a sample of blackletter script?
Scaling an object to change its key
Why one uses 了 and the other one doesn’t?
Justifying Affordable Bespoke Spaceships
"Correct me if I'm wrong"
What is this plant I saw for sale at a Romanian farmer's market?
I calculated that we should be able to see the sun well beyond the observable universe. Where did I go wrong?
Explicit song lyrics checker
Run script with arguments as user
How to run a specific program as root without a password prompt?Is there ever a good reason to run sudo su?Prompt for sudo password and programmatically elevate privilege in bash script?Using sudo in openSUSE without actually changing to root user (i.e., like in Ubuntu)How to transition into another domain when invoking sudo?Ubuntu CentOS RHEL cross bash script su supportHow to prevent the caller's shell from being used in sudoRun su -s with argumentslogin with apache userThis user is currently not available - but allow to run script by this usersudo -u not getting path appended in $HOME/.profilesu - user -c env not getting environmental variable without -l added
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to make sure when a script is run it is run as a specific user without having to su to that user before the script is run. Also the script is run with a couple of flags for example
./myscript.sh -e dev -v 1.9
I have tried the following
[ `whoami` = myuser ] || exec sudo -S su - myuser -c "bash `pwd`/`basename $0` $@"
But the -v flag which is supposed to be an input to my script is being fed as input to su. So it complains of an invalid option, is there a way to correct the above?
NB: The person running the script has sudo privileges.
sudo users su
|
show 2 more comments
I am trying to make sure when a script is run it is run as a specific user without having to su to that user before the script is run. Also the script is run with a couple of flags for example
./myscript.sh -e dev -v 1.9
I have tried the following
[ `whoami` = myuser ] || exec sudo -S su - myuser -c "bash `pwd`/`basename $0` $@"
But the -v flag which is supposed to be an input to my script is being fed as input to su. So it complains of an invalid option, is there a way to correct the above?
NB: The person running the script has sudo privileges.
sudo users su
3
The[and]must have spaces after and before them, respectively. Why do you needsudo su? Is notsudoenough? Do you really need to start an interactive or login shell? And if the user hassudoaccess, why use-S(used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?
– Kusalananda♦
Jun 10 at 8:36
The spaces was a typo. And I've found with justsudo suit asks for password input whereas-Sdoesn't
– PDStat
Jun 10 at 8:40
No, it still asks for one with-S, that just allows you to do something likeecho password | sudo -S command. If it didn't ask for a password, that's because you've recently run anothersudocommand so it still remembers the password. Runsudo -kto forget it and try again and it will ask for a password.
– terdon♦
Jun 10 at 8:45
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use-S
– PDStat
Jun 10 at 8:59
1
@Kusalananda no, but you can set upsuto be run withsudo suwithout needing a password throughsudoers. That said, I have absolutely no idea how or why the-Sflag could possibly be relevant.
– terdon♦
Jun 10 at 9:03
|
show 2 more comments
I am trying to make sure when a script is run it is run as a specific user without having to su to that user before the script is run. Also the script is run with a couple of flags for example
./myscript.sh -e dev -v 1.9
I have tried the following
[ `whoami` = myuser ] || exec sudo -S su - myuser -c "bash `pwd`/`basename $0` $@"
But the -v flag which is supposed to be an input to my script is being fed as input to su. So it complains of an invalid option, is there a way to correct the above?
NB: The person running the script has sudo privileges.
sudo users su
I am trying to make sure when a script is run it is run as a specific user without having to su to that user before the script is run. Also the script is run with a couple of flags for example
./myscript.sh -e dev -v 1.9
I have tried the following
[ `whoami` = myuser ] || exec sudo -S su - myuser -c "bash `pwd`/`basename $0` $@"
But the -v flag which is supposed to be an input to my script is being fed as input to su. So it complains of an invalid option, is there a way to correct the above?
NB: The person running the script has sudo privileges.
sudo users su
sudo users su
edited Jun 10 at 10:10
Jeff Schaller♦
47k1167152
47k1167152
asked Jun 10 at 8:29
PDStatPDStat
1233
1233
3
The[and]must have spaces after and before them, respectively. Why do you needsudo su? Is notsudoenough? Do you really need to start an interactive or login shell? And if the user hassudoaccess, why use-S(used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?
– Kusalananda♦
Jun 10 at 8:36
The spaces was a typo. And I've found with justsudo suit asks for password input whereas-Sdoesn't
– PDStat
Jun 10 at 8:40
No, it still asks for one with-S, that just allows you to do something likeecho password | sudo -S command. If it didn't ask for a password, that's because you've recently run anothersudocommand so it still remembers the password. Runsudo -kto forget it and try again and it will ask for a password.
– terdon♦
Jun 10 at 8:45
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use-S
– PDStat
Jun 10 at 8:59
1
@Kusalananda no, but you can set upsuto be run withsudo suwithout needing a password throughsudoers. That said, I have absolutely no idea how or why the-Sflag could possibly be relevant.
– terdon♦
Jun 10 at 9:03
|
show 2 more comments
3
The[and]must have spaces after and before them, respectively. Why do you needsudo su? Is notsudoenough? Do you really need to start an interactive or login shell? And if the user hassudoaccess, why use-S(used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?
– Kusalananda♦
Jun 10 at 8:36
The spaces was a typo. And I've found with justsudo suit asks for password input whereas-Sdoesn't
– PDStat
Jun 10 at 8:40
No, it still asks for one with-S, that just allows you to do something likeecho password | sudo -S command. If it didn't ask for a password, that's because you've recently run anothersudocommand so it still remembers the password. Runsudo -kto forget it and try again and it will ask for a password.
– terdon♦
Jun 10 at 8:45
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use-S
– PDStat
Jun 10 at 8:59
1
@Kusalananda no, but you can set upsuto be run withsudo suwithout needing a password throughsudoers. That said, I have absolutely no idea how or why the-Sflag could possibly be relevant.
– terdon♦
Jun 10 at 9:03
3
3
The
[ and ] must have spaces after and before them, respectively. Why do you need sudo su? Is not sudo enough? Do you really need to start an interactive or login shell? And if the user has sudo access, why use -S (used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?– Kusalananda♦
Jun 10 at 8:36
The
[ and ] must have spaces after and before them, respectively. Why do you need sudo su? Is not sudo enough? Do you really need to start an interactive or login shell? And if the user has sudo access, why use -S (used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?– Kusalananda♦
Jun 10 at 8:36
The spaces was a typo. And I've found with just
sudo su it asks for password input whereas -S doesn't– PDStat
Jun 10 at 8:40
The spaces was a typo. And I've found with just
sudo su it asks for password input whereas -S doesn't– PDStat
Jun 10 at 8:40
No, it still asks for one with
-S, that just allows you to do something like echo password | sudo -S command. If it didn't ask for a password, that's because you've recently run another sudo command so it still remembers the password. Run sudo -k to forget it and try again and it will ask for a password.– terdon♦
Jun 10 at 8:45
No, it still asks for one with
-S, that just allows you to do something like echo password | sudo -S command. If it didn't ask for a password, that's because you've recently run another sudo command so it still remembers the password. Run sudo -k to forget it and try again and it will ask for a password.– terdon♦
Jun 10 at 8:45
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use
-S– PDStat
Jun 10 at 8:59
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use
-S– PDStat
Jun 10 at 8:59
1
1
@Kusalananda no, but you can set up
su to be run with sudo su without needing a password through sudoers. That said, I have absolutely no idea how or why the -S flag could possibly be relevant.– terdon♦
Jun 10 at 9:03
@Kusalananda no, but you can set up
su to be run with sudo su without needing a password through sudoers. That said, I have absolutely no idea how or why the -S flag could possibly be relevant.– terdon♦
Jun 10 at 9:03
|
show 2 more comments
3 Answers
3
active
oldest
votes
The current user is already in the variable $USER. So all you need is:
[ "$USER" = "myuser" ] || sudo -u myuser $0 "$@"
There is no need for sudo su, sudo can do everything you require. You also don't need pwd or basename, the $0 variable already has the full path to the script.
Your original command was starting a login shell (su -). If that's really needed (which seems strange), you can do:
[ "$USER" = "myuser" ] || sudo -iu myuser $0 "$@"
Doing justsudo -u myuserwill ask for the password. I don't wish for this to happen
– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
@PDStat there is no option ofsudoorsuthat will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try runningsudo -kand then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in thesudoersfile.
– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can dosudo -kfollowed bysudo su - myuserwithout the prompt for a password. That's not the case however from a script without the-Sflag.
– PDStat
Jun 10 at 8:58
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the-Swould ever be relevant.
– terdon♦
Jun 10 at 9:05
add a comment |
There are two problems with your script:
1) You forgot the spaces in the test command [`whoami` = myuser]
2) Due to the expansion of the variable "$@" in two steps the quoting is lost.
The following seems to work on my system:
[ `whoami` = myuser ] || exec sudo -S -u myuser bash -- "$0" "$@"
This just gives meinvalid option: --e
– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
add a comment |
If the whole script would need to be run by a specific user, then I would leave it up to the user running the script to arrange with changing into the correct user identity in any way that they see fit (whether through su, sudo, or some other means).
This would simplify your script and would make it easier to use. In particular, it would avoid having to "correct" for the inability of the user to assume the right identity. Personally, I would treat this as an error in its invocation, similar to failing to use the correct command line options.
The script could still check to make sure that it's being run by the correct user, obviously:
if [ "$USER" != "correctuser" ]; then
echo 'Must be run by "correctuser"' >&2
exit 1
fi
An ordinary user would then run the script using
sudo -u correctuser ./script.sh -e dev -v 1.9
while the root user may want to do
su correctuser -c ./script.sh -e dev -v 1.9
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523953%2frun-script-with-arguments-as-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The current user is already in the variable $USER. So all you need is:
[ "$USER" = "myuser" ] || sudo -u myuser $0 "$@"
There is no need for sudo su, sudo can do everything you require. You also don't need pwd or basename, the $0 variable already has the full path to the script.
Your original command was starting a login shell (su -). If that's really needed (which seems strange), you can do:
[ "$USER" = "myuser" ] || sudo -iu myuser $0 "$@"
Doing justsudo -u myuserwill ask for the password. I don't wish for this to happen
– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
@PDStat there is no option ofsudoorsuthat will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try runningsudo -kand then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in thesudoersfile.
– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can dosudo -kfollowed bysudo su - myuserwithout the prompt for a password. That's not the case however from a script without the-Sflag.
– PDStat
Jun 10 at 8:58
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the-Swould ever be relevant.
– terdon♦
Jun 10 at 9:05
add a comment |
The current user is already in the variable $USER. So all you need is:
[ "$USER" = "myuser" ] || sudo -u myuser $0 "$@"
There is no need for sudo su, sudo can do everything you require. You also don't need pwd or basename, the $0 variable already has the full path to the script.
Your original command was starting a login shell (su -). If that's really needed (which seems strange), you can do:
[ "$USER" = "myuser" ] || sudo -iu myuser $0 "$@"
Doing justsudo -u myuserwill ask for the password. I don't wish for this to happen
– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
@PDStat there is no option ofsudoorsuthat will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try runningsudo -kand then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in thesudoersfile.
– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can dosudo -kfollowed bysudo su - myuserwithout the prompt for a password. That's not the case however from a script without the-Sflag.
– PDStat
Jun 10 at 8:58
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the-Swould ever be relevant.
– terdon♦
Jun 10 at 9:05
add a comment |
The current user is already in the variable $USER. So all you need is:
[ "$USER" = "myuser" ] || sudo -u myuser $0 "$@"
There is no need for sudo su, sudo can do everything you require. You also don't need pwd or basename, the $0 variable already has the full path to the script.
Your original command was starting a login shell (su -). If that's really needed (which seems strange), you can do:
[ "$USER" = "myuser" ] || sudo -iu myuser $0 "$@"
The current user is already in the variable $USER. So all you need is:
[ "$USER" = "myuser" ] || sudo -u myuser $0 "$@"
There is no need for sudo su, sudo can do everything you require. You also don't need pwd or basename, the $0 variable already has the full path to the script.
Your original command was starting a login shell (su -). If that's really needed (which seems strange), you can do:
[ "$USER" = "myuser" ] || sudo -iu myuser $0 "$@"
edited Jun 10 at 8:56
answered Jun 10 at 8:41
terdon♦terdon
137k33277458
137k33277458
Doing justsudo -u myuserwill ask for the password. I don't wish for this to happen
– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
@PDStat there is no option ofsudoorsuthat will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try runningsudo -kand then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in thesudoersfile.
– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can dosudo -kfollowed bysudo su - myuserwithout the prompt for a password. That's not the case however from a script without the-Sflag.
– PDStat
Jun 10 at 8:58
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the-Swould ever be relevant.
– terdon♦
Jun 10 at 9:05
add a comment |
Doing justsudo -u myuserwill ask for the password. I don't wish for this to happen
– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
@PDStat there is no option ofsudoorsuthat will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try runningsudo -kand then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in thesudoersfile.
– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can dosudo -kfollowed bysudo su - myuserwithout the prompt for a password. That's not the case however from a script without the-Sflag.
– PDStat
Jun 10 at 8:58
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the-Swould ever be relevant.
– terdon♦
Jun 10 at 9:05
Doing just
sudo -u myuser will ask for the password. I don't wish for this to happen– PDStat
Jun 10 at 8:51
Doing just
sudo -u myuser will ask for the password. I don't wish for this to happen– PDStat
Jun 10 at 8:51
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
@PDStat How do you wish to provide the password (which has to be provided)?
– Kusalananda♦
Jun 10 at 8:53
1
1
@PDStat there is no option of
sudo or su that will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try running sudo -k and then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in the sudoers file.– terdon♦
Jun 10 at 8:53
@PDStat there is no option of
sudo or su that will let you do this without a password. Your approach also asks for a password. If it didn't that's because your user had already authenticated. Try running sudo -k and then your command again and you'll see it will ask for a password. If you really need to do it without a password, you will need to set that up in the sudoers file.– terdon♦
Jun 10 at 8:53
It looks like the sudoers files is already set like that as I can do
sudo -k followed by sudo su - myuser without the prompt for a password. That's not the case however from a script without the -S flag.– PDStat
Jun 10 at 8:58
It looks like the sudoers files is already set like that as I can do
sudo -k followed by sudo su - myuser without the prompt for a password. That's not the case however from a script without the -S flag.– PDStat
Jun 10 at 8:58
1
1
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the
-S would ever be relevant.– terdon♦
Jun 10 at 9:05
@PDStat that... makes no sense to me. I believe you, I just don't have any idea how that could possibly happen. I cannot imagine how the
-S would ever be relevant.– terdon♦
Jun 10 at 9:05
add a comment |
There are two problems with your script:
1) You forgot the spaces in the test command [`whoami` = myuser]
2) Due to the expansion of the variable "$@" in two steps the quoting is lost.
The following seems to work on my system:
[ `whoami` = myuser ] || exec sudo -S -u myuser bash -- "$0" "$@"
This just gives meinvalid option: --e
– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
add a comment |
There are two problems with your script:
1) You forgot the spaces in the test command [`whoami` = myuser]
2) Due to the expansion of the variable "$@" in two steps the quoting is lost.
The following seems to work on my system:
[ `whoami` = myuser ] || exec sudo -S -u myuser bash -- "$0" "$@"
This just gives meinvalid option: --e
– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
add a comment |
There are two problems with your script:
1) You forgot the spaces in the test command [`whoami` = myuser]
2) Due to the expansion of the variable "$@" in two steps the quoting is lost.
The following seems to work on my system:
[ `whoami` = myuser ] || exec sudo -S -u myuser bash -- "$0" "$@"
There are two problems with your script:
1) You forgot the spaces in the test command [`whoami` = myuser]
2) Due to the expansion of the variable "$@" in two steps the quoting is lost.
The following seems to work on my system:
[ `whoami` = myuser ] || exec sudo -S -u myuser bash -- "$0" "$@"
edited Jun 10 at 9:15
answered Jun 10 at 8:37
user000001user000001
1,129715
1,129715
This just gives meinvalid option: --e
– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
add a comment |
This just gives meinvalid option: --e
– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
This just gives me
invalid option: --e– PDStat
Jun 10 at 9:12
This just gives me
invalid option: --e– PDStat
Jun 10 at 9:12
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
@PDStat: Try with the updated version, if it doesn't work edit your question with the entire script you are running, it could be a problem somewhere else.
– user000001
Jun 10 at 9:17
add a comment |
If the whole script would need to be run by a specific user, then I would leave it up to the user running the script to arrange with changing into the correct user identity in any way that they see fit (whether through su, sudo, or some other means).
This would simplify your script and would make it easier to use. In particular, it would avoid having to "correct" for the inability of the user to assume the right identity. Personally, I would treat this as an error in its invocation, similar to failing to use the correct command line options.
The script could still check to make sure that it's being run by the correct user, obviously:
if [ "$USER" != "correctuser" ]; then
echo 'Must be run by "correctuser"' >&2
exit 1
fi
An ordinary user would then run the script using
sudo -u correctuser ./script.sh -e dev -v 1.9
while the root user may want to do
su correctuser -c ./script.sh -e dev -v 1.9
add a comment |
If the whole script would need to be run by a specific user, then I would leave it up to the user running the script to arrange with changing into the correct user identity in any way that they see fit (whether through su, sudo, or some other means).
This would simplify your script and would make it easier to use. In particular, it would avoid having to "correct" for the inability of the user to assume the right identity. Personally, I would treat this as an error in its invocation, similar to failing to use the correct command line options.
The script could still check to make sure that it's being run by the correct user, obviously:
if [ "$USER" != "correctuser" ]; then
echo 'Must be run by "correctuser"' >&2
exit 1
fi
An ordinary user would then run the script using
sudo -u correctuser ./script.sh -e dev -v 1.9
while the root user may want to do
su correctuser -c ./script.sh -e dev -v 1.9
add a comment |
If the whole script would need to be run by a specific user, then I would leave it up to the user running the script to arrange with changing into the correct user identity in any way that they see fit (whether through su, sudo, or some other means).
This would simplify your script and would make it easier to use. In particular, it would avoid having to "correct" for the inability of the user to assume the right identity. Personally, I would treat this as an error in its invocation, similar to failing to use the correct command line options.
The script could still check to make sure that it's being run by the correct user, obviously:
if [ "$USER" != "correctuser" ]; then
echo 'Must be run by "correctuser"' >&2
exit 1
fi
An ordinary user would then run the script using
sudo -u correctuser ./script.sh -e dev -v 1.9
while the root user may want to do
su correctuser -c ./script.sh -e dev -v 1.9
If the whole script would need to be run by a specific user, then I would leave it up to the user running the script to arrange with changing into the correct user identity in any way that they see fit (whether through su, sudo, or some other means).
This would simplify your script and would make it easier to use. In particular, it would avoid having to "correct" for the inability of the user to assume the right identity. Personally, I would treat this as an error in its invocation, similar to failing to use the correct command line options.
The script could still check to make sure that it's being run by the correct user, obviously:
if [ "$USER" != "correctuser" ]; then
echo 'Must be run by "correctuser"' >&2
exit 1
fi
An ordinary user would then run the script using
sudo -u correctuser ./script.sh -e dev -v 1.9
while the root user may want to do
su correctuser -c ./script.sh -e dev -v 1.9
edited Jun 11 at 6:37
answered Jun 10 at 8:58
Kusalananda♦Kusalananda
151k18291478
151k18291478
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523953%2frun-script-with-arguments-as-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
The
[and]must have spaces after and before them, respectively. Why do you needsudo su? Is notsudoenough? Do you really need to start an interactive or login shell? And if the user hassudoaccess, why use-S(used for passing a password over standard input)? Related: Is there ever a good reason to run sudo su?– Kusalananda♦
Jun 10 at 8:36
The spaces was a typo. And I've found with just
sudo suit asks for password input whereas-Sdoesn't– PDStat
Jun 10 at 8:40
No, it still asks for one with
-S, that just allows you to do something likeecho password | sudo -S command. If it didn't ask for a password, that's because you've recently run anothersudocommand so it still remembers the password. Runsudo -kto forget it and try again and it will ask for a password.– terdon♦
Jun 10 at 8:45
As per my comment to terdon it looks like the sudoers file is set to not require a password prompt for su. That isn't the case however for doing an su from a script, it still prompts unless I use
-S– PDStat
Jun 10 at 8:59
1
@Kusalananda no, but you can set up
suto be run withsudo suwithout needing a password throughsudoers. That said, I have absolutely no idea how or why the-Sflag could possibly be relevant.– terdon♦
Jun 10 at 9:03