How to login as different user inside shell script and execute a set of commands?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.
example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.
bash shell-script
add a comment |Â
up vote
1
down vote
favorite
My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.
example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.
bash shell-script
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to usessh otheruser@localhost
with keypair for authentication.
â Philippos
Apr 26 '17 at 5:55
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.
example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.
bash shell-script
My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.
example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.
bash shell-script
bash shell-script
asked Apr 26 '17 at 4:32
Girish Sunkara
3132613
3132613
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to usessh otheruser@localhost
with keypair for authentication.
â Philippos
Apr 26 '17 at 5:55
add a comment |Â
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to usessh otheruser@localhost
with keypair for authentication.
â Philippos
Apr 26 '17 at 5:55
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use
ssh otheruser@localhost
with keypair for authentication.â Philippos
Apr 26 '17 at 5:55
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use
ssh otheruser@localhost
with keypair for authentication.â Philippos
Apr 26 '17 at 5:55
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
You can use several ways:
- Using
su
. Via su you can exec the command on this way:
su user -c "command"
or
su - user -c "command"
the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user
- Using
sudo
:
sudo -u user "command"
With this command you execute it with or without password (your password)
- Using
ssh
:
With ssh you can exec the command with password for user, password for the key (if any)
ssh user@localhost "command"
P.S. There are also other possible methods which are rarely possible like rsh
add a comment |Â
up vote
0
down vote
Using the command su [username]
allows you to run a command in the context of that user, from the current location. Using the following command su - [username]
switches you to that users home directory.
If you omit the username you are switching to root.
And the password? Do you want to source it plain from the script? You'd need tosudo
the script to avoid be promted for the password.
â Philippos
Apr 26 '17 at 5:58
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
You can use several ways:
- Using
su
. Via su you can exec the command on this way:
su user -c "command"
or
su - user -c "command"
the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user
- Using
sudo
:
sudo -u user "command"
With this command you execute it with or without password (your password)
- Using
ssh
:
With ssh you can exec the command with password for user, password for the key (if any)
ssh user@localhost "command"
P.S. There are also other possible methods which are rarely possible like rsh
add a comment |Â
up vote
4
down vote
You can use several ways:
- Using
su
. Via su you can exec the command on this way:
su user -c "command"
or
su - user -c "command"
the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user
- Using
sudo
:
sudo -u user "command"
With this command you execute it with or without password (your password)
- Using
ssh
:
With ssh you can exec the command with password for user, password for the key (if any)
ssh user@localhost "command"
P.S. There are also other possible methods which are rarely possible like rsh
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You can use several ways:
- Using
su
. Via su you can exec the command on this way:
su user -c "command"
or
su - user -c "command"
the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user
- Using
sudo
:
sudo -u user "command"
With this command you execute it with or without password (your password)
- Using
ssh
:
With ssh you can exec the command with password for user, password for the key (if any)
ssh user@localhost "command"
P.S. There are also other possible methods which are rarely possible like rsh
You can use several ways:
- Using
su
. Via su you can exec the command on this way:
su user -c "command"
or
su - user -c "command"
the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user
- Using
sudo
:
sudo -u user "command"
With this command you execute it with or without password (your password)
- Using
ssh
:
With ssh you can exec the command with password for user, password for the key (if any)
ssh user@localhost "command"
P.S. There are also other possible methods which are rarely possible like rsh
answered Apr 26 '17 at 6:46
Romeo Ninov
4,47311625
4,47311625
add a comment |Â
add a comment |Â
up vote
0
down vote
Using the command su [username]
allows you to run a command in the context of that user, from the current location. Using the following command su - [username]
switches you to that users home directory.
If you omit the username you are switching to root.
And the password? Do you want to source it plain from the script? You'd need tosudo
the script to avoid be promted for the password.
â Philippos
Apr 26 '17 at 5:58
add a comment |Â
up vote
0
down vote
Using the command su [username]
allows you to run a command in the context of that user, from the current location. Using the following command su - [username]
switches you to that users home directory.
If you omit the username you are switching to root.
And the password? Do you want to source it plain from the script? You'd need tosudo
the script to avoid be promted for the password.
â Philippos
Apr 26 '17 at 5:58
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using the command su [username]
allows you to run a command in the context of that user, from the current location. Using the following command su - [username]
switches you to that users home directory.
If you omit the username you are switching to root.
Using the command su [username]
allows you to run a command in the context of that user, from the current location. Using the following command su - [username]
switches you to that users home directory.
If you omit the username you are switching to root.
answered Apr 26 '17 at 5:42
Graham Harris
111
111
And the password? Do you want to source it plain from the script? You'd need tosudo
the script to avoid be promted for the password.
â Philippos
Apr 26 '17 at 5:58
add a comment |Â
And the password? Do you want to source it plain from the script? You'd need tosudo
the script to avoid be promted for the password.
â Philippos
Apr 26 '17 at 5:58
And the password? Do you want to source it plain from the script? You'd need to
sudo
the script to avoid be promted for the password.â Philippos
Apr 26 '17 at 5:58
And the password? Do you want to source it plain from the script? You'd need to
sudo
the script to avoid be promted for the password.â Philippos
Apr 26 '17 at 5:58
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%2f361327%2fhow-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands%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
Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use
ssh otheruser@localhost
with keypair for authentication.â Philippos
Apr 26 '17 at 5:55