Hossam Hassan
Hossam Hassan

Reputation: 21

Switching user in bash shell script

I'm using Solaris SunOS 5.10 and I need to switch user while executing a bash shell script providing su or sudo with user-name and password I have tried the below command

    echo -e "mqm\n" | sudo -S "command"

But i didn't get how this command will know the user i want to switch to and also when i try it it says wrong password

What is really want to do is to switch the user within my script then run anther script after switching because the first user doesn't have permission to run mq commands

Thanks

Upvotes: 2

Views: 933

Answers (1)

neuhaus
neuhaus

Reputation: 4094

You can give a user permission to run certain commands without requiring a password with sudo. See man 5 sudoers for details.

Here is an example sudoers entry for a user with username "user10":

user10 ALL=NOPASSWD: /usr/bin/apt-get update, /usr/bin/apt-get -u upgrade

This will allow the user to run "/usr/bin/apt-get update" and "/usr/bin/apt-get -u upgrade" as root without requiring a password.

PS Sudo does not read the password from STDIN, that's why piping the password will not work. It reads it directly from the pseudo terminal.


There is a different solution using the set-UID bit. It is hard to get it as secure as the sudo solution given above.

Create a binary that is owned by the user that has sufficient access rights to run the program properly. Set the setuid bit of the binary (chmod u+s program_name). Then when another user executes this binary it will run as the owner of the file, not as the user executing it.

Upvotes: 1

Related Questions