1

I need to switch between two users using a shell script.

I used su and sudo for switching between users. The bottom line is that, it's prompting for user password every time, and I do not want that to happen.

I know the password; is there a way I can hard code it in the script itself, so that it will not prompt the user for a password?

3
  • show what you have done and tried? Commented Aug 14, 2012 at 12:53
  • su root systemctl restart postgresql.service part of my code Commented Aug 14, 2012 at 12:56
  • see unix.stackexchange.com/questions/7747/… Commented Aug 14, 2012 at 13:07

2 Answers 2

2

Wouldn't a NOPASSWD clause in sudoers work? For example:

user1 ALL=(ALL:ALL) NOPASSWD: /bin/su user2

Allows user1 to su to user2 without password. If you only need to run a certain command as user2, add that to sudoers (through visudo) explicitly:

user1 ALL=(user2) NOPASSWD: /path/to/command

Then as user1 run:

sudo -k user2 /path/to/command
Sign up to request clarification or add additional context in comments.

Comments

1

With the -S parameter sudo accepts the password from Standard Input. See: How to pass the password to su/sudo/ssh without overriding the TTY?

Comments

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.