2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

yes i know but for securities reasons this cannot be done
What are the security reasons? Doing it differently is usually less secure. For example having the cleartext password of a user in a script is not secure at all. I added a setuid solution as well.
the reason is the user which run the script have no permissions to switch or run anything and i will not be able to change its configuration and when i switch the user i will read the password from anther file not as a text in the script it self so i need to switch user and enter password without stopping the script from running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.