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?

Dan Puzey
  • 31,916
  • 3
  • 71
  • 95
Gopinagh.R
  • 4,458
  • 4
  • 43
  • 58

2 Answers2

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
Thor
  • 39,032
  • 10
  • 106
  • 121
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?

Community
  • 1
  • 1
rollstuhlfahrer
  • 3,762
  • 9
  • 21
  • 37