How can I change user inside function
Clash Royale CLAN TAG#URR8PPP
I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo
works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh
test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...
test
sudo -Eu root bash
echo "outside $(whoami)"
Running this as
ssh centos@myhost.com 'bash -s' < script.sh
Prints:
outside root
inside centos
root
user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?
bash sudo function
add a comment |
I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo
works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh
test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...
test
sudo -Eu root bash
echo "outside $(whoami)"
Running this as
ssh centos@myhost.com 'bash -s' < script.sh
Prints:
outside root
inside centos
root
user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?
bash sudo function
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script usingsudo
. Alsotest
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system withoutsudo
).
– Kusalananda
Feb 21 at 20:19
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
Your script will start first one interactivebash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.
– Kusalananda
Feb 21 at 20:33
@Kusalananda My bad, forgot to mention, I execute this script through ssh.ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.
– tsh
Feb 21 at 20:38
add a comment |
I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo
works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh
test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...
test
sudo -Eu root bash
echo "outside $(whoami)"
Running this as
ssh centos@myhost.com 'bash -s' < script.sh
Prints:
outside root
inside centos
root
user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?
bash sudo function
I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo
works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh
test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...
test
sudo -Eu root bash
echo "outside $(whoami)"
Running this as
ssh centos@myhost.com 'bash -s' < script.sh
Prints:
outside root
inside centos
root
user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?
bash sudo function
bash sudo function
edited Feb 21 at 21:08
ctrl-alt-delor
12k42461
12k42461
asked Feb 21 at 20:16
tshtsh
123
123
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script usingsudo
. Alsotest
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system withoutsudo
).
– Kusalananda
Feb 21 at 20:19
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
Your script will start first one interactivebash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.
– Kusalananda
Feb 21 at 20:33
@Kusalananda My bad, forgot to mention, I execute this script through ssh.ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.
– tsh
Feb 21 at 20:38
add a comment |
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script usingsudo
. Alsotest
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system withoutsudo
).
– Kusalananda
Feb 21 at 20:19
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
Your script will start first one interactivebash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.
– Kusalananda
Feb 21 at 20:33
@Kusalananda My bad, forgot to mention, I execute this script through ssh.ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.
– tsh
Feb 21 at 20:38
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo
. Also test
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo
).– Kusalananda
Feb 21 at 20:19
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo
. Also test
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo
).– Kusalananda
Feb 21 at 20:19
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
Your script will start first one interactive
bash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.– Kusalananda
Feb 21 at 20:33
Your script will start first one interactive
bash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.– Kusalananda
Feb 21 at 20:33
@Kusalananda My bad, forgot to mention, I execute this script through ssh.
ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.– tsh
Feb 21 at 20:38
@Kusalananda My bad, forgot to mention, I execute this script through ssh.
ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.– tsh
Feb 21 at 20:38
add a comment |
2 Answers
2
active
oldest
votes
Don't do this.
Getting the two lines of output in the wrong order should have been a hint that something was wrong.
When you execute your script using
ssh centos@myhost.com 'bash -s' < script.sh
the following happens:
- The script is passed to
bash -s
on the shells standard input stream. - The script defines the
test
function and calls it. - The
sudo bash
command in the function starts a root shell. - This shell inherits the standard input stream, which is the script.
- The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.
Now you have a root shell started from within the test
function, which is executing instructions after the test
call.
- It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).
You now have a centos
shell executing a root shell, executing a root shell.
- The second root shell executes
echo "outside $(whoami)"
outputtingoutside root
, which is the last line of the script. - There is nothing more to read, so the second root shell terminates.
- So does the first root shell.
- The original
bash -s
shell executesecho "inside $(whoami)"
(because it's part of the function that it started to execute earlier), outputtinginside centos
. - The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.
sudo
is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo
to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).
To execute a set of commands as root in a script, you must give those commands to the sudo
invocation. For example:
sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'
After the sudo bash -c
command exits, you are back as your original user. Always.
add a comment |
You need to execute the commands in the shell that you start with sudo
.
$ f()
sudo -Eu root bash -c whoami
$ f
root
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%2f502167%2fhow-can-i-change-user-inside-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't do this.
Getting the two lines of output in the wrong order should have been a hint that something was wrong.
When you execute your script using
ssh centos@myhost.com 'bash -s' < script.sh
the following happens:
- The script is passed to
bash -s
on the shells standard input stream. - The script defines the
test
function and calls it. - The
sudo bash
command in the function starts a root shell. - This shell inherits the standard input stream, which is the script.
- The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.
Now you have a root shell started from within the test
function, which is executing instructions after the test
call.
- It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).
You now have a centos
shell executing a root shell, executing a root shell.
- The second root shell executes
echo "outside $(whoami)"
outputtingoutside root
, which is the last line of the script. - There is nothing more to read, so the second root shell terminates.
- So does the first root shell.
- The original
bash -s
shell executesecho "inside $(whoami)"
(because it's part of the function that it started to execute earlier), outputtinginside centos
. - The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.
sudo
is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo
to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).
To execute a set of commands as root in a script, you must give those commands to the sudo
invocation. For example:
sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'
After the sudo bash -c
command exits, you are back as your original user. Always.
add a comment |
Don't do this.
Getting the two lines of output in the wrong order should have been a hint that something was wrong.
When you execute your script using
ssh centos@myhost.com 'bash -s' < script.sh
the following happens:
- The script is passed to
bash -s
on the shells standard input stream. - The script defines the
test
function and calls it. - The
sudo bash
command in the function starts a root shell. - This shell inherits the standard input stream, which is the script.
- The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.
Now you have a root shell started from within the test
function, which is executing instructions after the test
call.
- It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).
You now have a centos
shell executing a root shell, executing a root shell.
- The second root shell executes
echo "outside $(whoami)"
outputtingoutside root
, which is the last line of the script. - There is nothing more to read, so the second root shell terminates.
- So does the first root shell.
- The original
bash -s
shell executesecho "inside $(whoami)"
(because it's part of the function that it started to execute earlier), outputtinginside centos
. - The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.
sudo
is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo
to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).
To execute a set of commands as root in a script, you must give those commands to the sudo
invocation. For example:
sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'
After the sudo bash -c
command exits, you are back as your original user. Always.
add a comment |
Don't do this.
Getting the two lines of output in the wrong order should have been a hint that something was wrong.
When you execute your script using
ssh centos@myhost.com 'bash -s' < script.sh
the following happens:
- The script is passed to
bash -s
on the shells standard input stream. - The script defines the
test
function and calls it. - The
sudo bash
command in the function starts a root shell. - This shell inherits the standard input stream, which is the script.
- The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.
Now you have a root shell started from within the test
function, which is executing instructions after the test
call.
- It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).
You now have a centos
shell executing a root shell, executing a root shell.
- The second root shell executes
echo "outside $(whoami)"
outputtingoutside root
, which is the last line of the script. - There is nothing more to read, so the second root shell terminates.
- So does the first root shell.
- The original
bash -s
shell executesecho "inside $(whoami)"
(because it's part of the function that it started to execute earlier), outputtinginside centos
. - The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.
sudo
is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo
to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).
To execute a set of commands as root in a script, you must give those commands to the sudo
invocation. For example:
sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'
After the sudo bash -c
command exits, you are back as your original user. Always.
Don't do this.
Getting the two lines of output in the wrong order should have been a hint that something was wrong.
When you execute your script using
ssh centos@myhost.com 'bash -s' < script.sh
the following happens:
- The script is passed to
bash -s
on the shells standard input stream. - The script defines the
test
function and calls it. - The
sudo bash
command in the function starts a root shell. - This shell inherits the standard input stream, which is the script.
- The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.
Now you have a root shell started from within the test
function, which is executing instructions after the test
call.
- It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).
You now have a centos
shell executing a root shell, executing a root shell.
- The second root shell executes
echo "outside $(whoami)"
outputtingoutside root
, which is the last line of the script. - There is nothing more to read, so the second root shell terminates.
- So does the first root shell.
- The original
bash -s
shell executesecho "inside $(whoami)"
(because it's part of the function that it started to execute earlier), outputtinginside centos
. - The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.
sudo
is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo
to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).
To execute a set of commands as root in a script, you must give those commands to the sudo
invocation. For example:
sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'
After the sudo bash -c
command exits, you are back as your original user. Always.
edited Feb 21 at 21:25
answered Feb 21 at 20:53
KusalanandaKusalananda
136k17257425
136k17257425
add a comment |
add a comment |
You need to execute the commands in the shell that you start with sudo
.
$ f()
sudo -Eu root bash -c whoami
$ f
root
add a comment |
You need to execute the commands in the shell that you start with sudo
.
$ f()
sudo -Eu root bash -c whoami
$ f
root
add a comment |
You need to execute the commands in the shell that you start with sudo
.
$ f()
sudo -Eu root bash -c whoami
$ f
root
You need to execute the commands in the shell that you start with sudo
.
$ f()
sudo -Eu root bash -c whoami
$ f
root
answered Feb 21 at 20:24
TomaszTomasz
10.2k53068
10.2k53068
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%2f502167%2fhow-can-i-change-user-inside-function%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
sudo
runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script usingsudo
. Alsotest
is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system withoutsudo
).– Kusalananda
Feb 21 at 20:19
@Kusalananda But I changed user to root outside function.
– tsh
Feb 21 at 20:26
Your script will start first one interactive
bash
session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.– Kusalananda
Feb 21 at 20:33
@Kusalananda My bad, forgot to mention, I execute this script through ssh.
ssh centos@myhost.com 'bash -s' < script.sh
this way it produces result, shown above. I've edited my question.– tsh
Feb 21 at 20:38