Run part of a bash script as a different user
Is there a way to make part of a script run as a different (non-root) user? If it helps , the part to be run as a different user occurs at the end of the script
Edit :
OS -> Ubuntu 9.04
linux bash user
add a comment |
Is there a way to make part of a script run as a different (non-root) user? If it helps , the part to be run as a different user occurs at the end of the script
Edit :
OS -> Ubuntu 9.04
linux bash user
1
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35
add a comment |
Is there a way to make part of a script run as a different (non-root) user? If it helps , the part to be run as a different user occurs at the end of the script
Edit :
OS -> Ubuntu 9.04
linux bash user
Is there a way to make part of a script run as a different (non-root) user? If it helps , the part to be run as a different user occurs at the end of the script
Edit :
OS -> Ubuntu 9.04
linux bash user
linux bash user
edited Jan 9 '10 at 9:41
Manish Mathai
asked Jan 9 '10 at 4:46
Manish MathaiManish Mathai
3612511
3612511
1
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35
add a comment |
1
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35
1
1
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35
add a comment |
6 Answers
6
active
oldest
votes
Use the sudo command in the script.
In the form:
sudo -u username command
the sudo command runs command as the user username.
If the script is being run as root, I don't think it will prompt for a password. Otherwise, this article discusses how to use sudo with password in one command line?, and this article discusses how to use sudo without password?
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executablechmod +x script.sh
and then justsudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include thei
option if you want to acquire the user's environment
– neoDev
Nov 21 '17 at 6:17
add a comment |
This answer is good, but the serverfault advice is slightly dangerous - would allow anyone to run anything as root! So I'm posting here because I can't format the comment.
I would recommend using visudo to give the permissions you need as precisely as you can. Type visudo
and add a line like:
username hostname = NOPASSWD: /full/path/to/command1, full/path/to/command2
If you do need to run this same thing on many hosts, you could open it up with:
username ALL = NOPASSWD: /full/path/to/command1, full/path/to/command2
But I would **not* use either:
username ALL=(ALL) NOPASSWD: ALL
or
username hostname = ALL
The sudoer man page has lots of gory details
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replaceusername
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in thesudo -u username commandline
line of the script.
– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
add a comment |
# I=like:
#test if running bash as a different user works
sudo -u nobody bash -c : && RUNAS="sudo -u nobody"
echo 1: $USER
#Runs bash with commands between '_' as nobody if possible
$RUNAS bash<<_
echo 2: $USER
_
echo 3: $USER
# ./run
1: root
2: nobody
3: root
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
add a comment |
For sonarqube:
sudo -u sonar /usr/bin/sonar start
where sonar
is the name of user used to run the command /usr/bin/sonar start
add a comment |
not so sure about it, but if you want that ONLY the end of that script will run as a different user, you could add su someuser
before the end of the script.
Am I missing something?
Hope that helps,
Regards
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
add a comment |
This way, end of a script will be executed by different user (root).
Please note the $[LINENO+2]
and exit $?
calls. These are required to make the end of the script to execute just once and to preserve the exit code of the sudo
call.
#!/bin/bash
echo $USER
# pipe the rest of this script via a sudo call
tail -n +$[LINENO+2] $0 | exec sudo bash
exit $?
echo $USER
exit 1
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f93385%2frun-part-of-a-bash-script-as-a-different-user%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the sudo command in the script.
In the form:
sudo -u username command
the sudo command runs command as the user username.
If the script is being run as root, I don't think it will prompt for a password. Otherwise, this article discusses how to use sudo with password in one command line?, and this article discusses how to use sudo without password?
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executablechmod +x script.sh
and then justsudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include thei
option if you want to acquire the user's environment
– neoDev
Nov 21 '17 at 6:17
add a comment |
Use the sudo command in the script.
In the form:
sudo -u username command
the sudo command runs command as the user username.
If the script is being run as root, I don't think it will prompt for a password. Otherwise, this article discusses how to use sudo with password in one command line?, and this article discusses how to use sudo without password?
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executablechmod +x script.sh
and then justsudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include thei
option if you want to acquire the user's environment
– neoDev
Nov 21 '17 at 6:17
add a comment |
Use the sudo command in the script.
In the form:
sudo -u username command
the sudo command runs command as the user username.
If the script is being run as root, I don't think it will prompt for a password. Otherwise, this article discusses how to use sudo with password in one command line?, and this article discusses how to use sudo without password?
Use the sudo command in the script.
In the form:
sudo -u username command
the sudo command runs command as the user username.
If the script is being run as root, I don't think it will prompt for a password. Otherwise, this article discusses how to use sudo with password in one command line?, and this article discusses how to use sudo without password?
edited Apr 13 '17 at 12:14
Community♦
1
1
answered Jan 9 '10 at 4:53
pcapademicpcapademic
2,59332329
2,59332329
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executablechmod +x script.sh
and then justsudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include thei
option if you want to acquire the user's environment
– neoDev
Nov 21 '17 at 6:17
add a comment |
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executablechmod +x script.sh
and then justsudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include thei
option if you want to acquire the user's environment
– neoDev
Nov 21 '17 at 6:17
3
3
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
This is nice and easy, but is there a way to do this for a group of commands instead of one at a time?
– Andrew
Dec 5 '13 at 23:08
write a bash script, make it executable
chmod +x script.sh
and then just sudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
write a bash script, make it executable
chmod +x script.sh
and then just sudo -u username script.sh
– NiCU
Jun 11 '14 at 15:05
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
@Andrew I was wondering this myself and the following worked for me: sudo -u ic sh -c 'cmd1 && cmd2'
– Karussell
May 29 '17 at 14:26
Also include the
i
option if you want to acquire the user's environment– neoDev
Nov 21 '17 at 6:17
Also include the
i
option if you want to acquire the user's environment– neoDev
Nov 21 '17 at 6:17
add a comment |
This answer is good, but the serverfault advice is slightly dangerous - would allow anyone to run anything as root! So I'm posting here because I can't format the comment.
I would recommend using visudo to give the permissions you need as precisely as you can. Type visudo
and add a line like:
username hostname = NOPASSWD: /full/path/to/command1, full/path/to/command2
If you do need to run this same thing on many hosts, you could open it up with:
username ALL = NOPASSWD: /full/path/to/command1, full/path/to/command2
But I would **not* use either:
username ALL=(ALL) NOPASSWD: ALL
or
username hostname = ALL
The sudoer man page has lots of gory details
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replaceusername
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in thesudo -u username commandline
line of the script.
– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
add a comment |
This answer is good, but the serverfault advice is slightly dangerous - would allow anyone to run anything as root! So I'm posting here because I can't format the comment.
I would recommend using visudo to give the permissions you need as precisely as you can. Type visudo
and add a line like:
username hostname = NOPASSWD: /full/path/to/command1, full/path/to/command2
If you do need to run this same thing on many hosts, you could open it up with:
username ALL = NOPASSWD: /full/path/to/command1, full/path/to/command2
But I would **not* use either:
username ALL=(ALL) NOPASSWD: ALL
or
username hostname = ALL
The sudoer man page has lots of gory details
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replaceusername
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in thesudo -u username commandline
line of the script.
– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
add a comment |
This answer is good, but the serverfault advice is slightly dangerous - would allow anyone to run anything as root! So I'm posting here because I can't format the comment.
I would recommend using visudo to give the permissions you need as precisely as you can. Type visudo
and add a line like:
username hostname = NOPASSWD: /full/path/to/command1, full/path/to/command2
If you do need to run this same thing on many hosts, you could open it up with:
username ALL = NOPASSWD: /full/path/to/command1, full/path/to/command2
But I would **not* use either:
username ALL=(ALL) NOPASSWD: ALL
or
username hostname = ALL
The sudoer man page has lots of gory details
This answer is good, but the serverfault advice is slightly dangerous - would allow anyone to run anything as root! So I'm posting here because I can't format the comment.
I would recommend using visudo to give the permissions you need as precisely as you can. Type visudo
and add a line like:
username hostname = NOPASSWD: /full/path/to/command1, full/path/to/command2
If you do need to run this same thing on many hosts, you could open it up with:
username ALL = NOPASSWD: /full/path/to/command1, full/path/to/command2
But I would **not* use either:
username ALL=(ALL) NOPASSWD: ALL
or
username hostname = ALL
The sudoer man page has lots of gory details
edited Mar 20 '17 at 10:17
Community♦
1
1
answered Jan 9 '10 at 6:33
DaveParilloDaveParillo
13k3444
13k3444
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replaceusername
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in thesudo -u username commandline
line of the script.
– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
add a comment |
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replaceusername
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in thesudo -u username commandline
line of the script.
– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
Would it work for a command available to a particular user only ?
– Manish Mathai
Jan 9 '10 at 9:42
You can specify that only particular users can run the command (in the examples above, replace
username
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in the sudo -u username commandline
line of the script.– James Polley
Jan 9 '10 at 10:31
You can specify that only particular users can run the command (in the examples above, replace
username
with the username who should be able to run the command). If the executable you want to run is only executable by one particular user, that's fine too - just pass that username in the sudo -u username commandline
line of the script.– James Polley
Jan 9 '10 at 10:31
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
@Manish. Yes. What the sudoers file says is "Allow this username on this host to run command1 without having to provide a password.
– DaveParillo
Jan 9 '10 at 17:23
add a comment |
# I=like:
#test if running bash as a different user works
sudo -u nobody bash -c : && RUNAS="sudo -u nobody"
echo 1: $USER
#Runs bash with commands between '_' as nobody if possible
$RUNAS bash<<_
echo 2: $USER
_
echo 3: $USER
# ./run
1: root
2: nobody
3: root
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
add a comment |
# I=like:
#test if running bash as a different user works
sudo -u nobody bash -c : && RUNAS="sudo -u nobody"
echo 1: $USER
#Runs bash with commands between '_' as nobody if possible
$RUNAS bash<<_
echo 2: $USER
_
echo 3: $USER
# ./run
1: root
2: nobody
3: root
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
add a comment |
# I=like:
#test if running bash as a different user works
sudo -u nobody bash -c : && RUNAS="sudo -u nobody"
echo 1: $USER
#Runs bash with commands between '_' as nobody if possible
$RUNAS bash<<_
echo 2: $USER
_
echo 3: $USER
# ./run
1: root
2: nobody
3: root
# I=like:
#test if running bash as a different user works
sudo -u nobody bash -c : && RUNAS="sudo -u nobody"
echo 1: $USER
#Runs bash with commands between '_' as nobody if possible
$RUNAS bash<<_
echo 2: $USER
_
echo 3: $USER
# ./run
1: root
2: nobody
3: root
edited Mar 29 '13 at 15:58
answered Mar 28 '13 at 18:38
AXE-LabsAXE-Labs
4271510
4271510
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
add a comment |
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
8
8
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
Could you add some explanatory text?
– Kazark
Mar 28 '13 at 19:21
add a comment |
For sonarqube:
sudo -u sonar /usr/bin/sonar start
where sonar
is the name of user used to run the command /usr/bin/sonar start
add a comment |
For sonarqube:
sudo -u sonar /usr/bin/sonar start
where sonar
is the name of user used to run the command /usr/bin/sonar start
add a comment |
For sonarqube:
sudo -u sonar /usr/bin/sonar start
where sonar
is the name of user used to run the command /usr/bin/sonar start
For sonarqube:
sudo -u sonar /usr/bin/sonar start
where sonar
is the name of user used to run the command /usr/bin/sonar start
edited Jan 6 at 4:07
Community♦
1
1
answered Oct 8 '13 at 7:53
burtsevygburtsevyg
21122
21122
add a comment |
add a comment |
not so sure about it, but if you want that ONLY the end of that script will run as a different user, you could add su someuser
before the end of the script.
Am I missing something?
Hope that helps,
Regards
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
add a comment |
not so sure about it, but if you want that ONLY the end of that script will run as a different user, you could add su someuser
before the end of the script.
Am I missing something?
Hope that helps,
Regards
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
add a comment |
not so sure about it, but if you want that ONLY the end of that script will run as a different user, you could add su someuser
before the end of the script.
Am I missing something?
Hope that helps,
Regards
not so sure about it, but if you want that ONLY the end of that script will run as a different user, you could add su someuser
before the end of the script.
Am I missing something?
Hope that helps,
Regards
answered Jan 9 '10 at 21:37
dag729dag729
1,58111935
1,58111935
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
add a comment |
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
1
1
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
I think this is the most appropriate answer, having all other answers suggesting sudo, which is often not installed by default on some minimal linux installations.
– Tim
Jan 7 '18 at 3:04
add a comment |
This way, end of a script will be executed by different user (root).
Please note the $[LINENO+2]
and exit $?
calls. These are required to make the end of the script to execute just once and to preserve the exit code of the sudo
call.
#!/bin/bash
echo $USER
# pipe the rest of this script via a sudo call
tail -n +$[LINENO+2] $0 | exec sudo bash
exit $?
echo $USER
exit 1
add a comment |
This way, end of a script will be executed by different user (root).
Please note the $[LINENO+2]
and exit $?
calls. These are required to make the end of the script to execute just once and to preserve the exit code of the sudo
call.
#!/bin/bash
echo $USER
# pipe the rest of this script via a sudo call
tail -n +$[LINENO+2] $0 | exec sudo bash
exit $?
echo $USER
exit 1
add a comment |
This way, end of a script will be executed by different user (root).
Please note the $[LINENO+2]
and exit $?
calls. These are required to make the end of the script to execute just once and to preserve the exit code of the sudo
call.
#!/bin/bash
echo $USER
# pipe the rest of this script via a sudo call
tail -n +$[LINENO+2] $0 | exec sudo bash
exit $?
echo $USER
exit 1
This way, end of a script will be executed by different user (root).
Please note the $[LINENO+2]
and exit $?
calls. These are required to make the end of the script to execute just once and to preserve the exit code of the sudo
call.
#!/bin/bash
echo $USER
# pipe the rest of this script via a sudo call
tail -n +$[LINENO+2] $0 | exec sudo bash
exit $?
echo $USER
exit 1
answered Nov 29 '13 at 14:07
dparalendparalen
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f93385%2frun-part-of-a-bash-script-as-a-different-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
1
possible duplicate of Howto switch / chage user id witin a bash script to execute commands in the same script?
– Dan Dascalescu
Jul 11 '14 at 10:35