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
  • 33,626
  • 4
  • 73
  • 96
Gopinagh.R
  • 4,826
  • 4
  • 44
  • 60

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
  • 45,082
  • 11
  • 119
  • 130
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,988
  • 9
  • 25
  • 38