I have 2 users: usr1 and usr2. Neither is a root user.

usr1 starts a bash script. And from the script, I want to run some commands as usr2. I understand that the way to do it is:

su -l <usr2> -c "command"

The issue is with passing the password. These are 2 different users with different privileges, so, skipping the password for usr2 is not an option.

This script can go interactive, and ask the user for the password. Is there a way to do this in bash script ?

  • Security is of utmost importance, and so NOPASSWD is NOT an option – Manoj Sep 20 '18 at 15:17
  • When I put su -l $anotheruser -c whoami in a bash script and run it, I am prompted for the password, then whoami runs as the 2nd user. Is that not what you require? – JigglyNaga Sep 20 '18 at 15:47
  • @JigglyNaga this works when I am manually running it, but how can i interact with the user to enter password from a bash script when su asks for password ? – Manoj Sep 20 '18 at 16:09
  • What else do you mean by "interact with the user to enter a password"? The script gives control to su, which prompts the user directly. Do you want it to pop up a dialogue box instead? – JigglyNaga Sep 20 '18 at 16:14
  • @JigglyNaga Thanks. That works, but i think I should've been more precise on my question. In actual, I have like 30-40 commands which needs to run as usr2. That means I have to enter password everytime. I am trying to find a way user enters the usr2 password once, and the script can use it then on. – Manoj Sep 20 '18 at 16:20

Why not have your sudoers file setup so that user 1 can sudo to user 2 BUT only run that one command (with no password)? Does that still meet your security concerns?

I think the sudoers would be something like

username hostname = NOPASSWD: /path/to/command

  • Thanks for the answer. But, that wont meet the security requirement. Ideally, usr2 is a totally different person with no knowledge of usr1 credentials and vice versa. – Manoj Sep 20 '18 at 16:09
  • @Manoj If usr1 has no knowledge of usr2's credentials, how/why are they expected to know each other's passwords? – JigglyNaga Sep 20 '18 at 16:26
  • @JigglyNaga They wont. When the script is run, and when it's time for usr2 commands to run, user-2 has to come and type in the usr2 password. – Manoj Sep 20 '18 at 16:39
  • I am re-reading all of this, your specific question is that there are several commands and you don't want the user to enter the password over and over. Why don't you put all of the commands one after another in your original script: sudo -l <usr2> -c "command1 ; command2 ; command3" – number9 Sep 20 '18 at 20:31
  • @number9 thanks. I think I will go with the option of putting all the usr2 commands to a different sh file, and then call that from the primary file using su – Manoj Sep 20 '18 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.