If your goal is to su
without password, I recommend using sudo su
. Allow the user to use su
without password in /etc/sudoers
with
[username] ALL=(ALL) NOPASSWD: /bin/su
or
[username] ALL=(ALL) NOPASSWD: ALL
since the user can already do whatever he/she wants with root access. Benefit over passing cleartext password from script is that the root
password isn't leaked out. Remember that the password may be readable after deleting the script.
EDIT:Sollution below doesn't work after all, su
treats stdin
as a command and exits after it. echo 'passwd'|sudo -S [command]
works for most commands other than su
but if you use it, note that stdin
still has the password if sudo
didn't ask for it because timestamp was still fresh. You can use sudo -k
first to force password query.sudo -k && echo 'passwd'|sudo -S [cmd]
. Still, better use NOPASSWD
in /etc/sudoers
.
EDIT: Also, instead of using NOPASSWD:
, you can also use sudo -S
to read the sudo
password from stdin
and use something like echo [password]|sudo -S su
. sudo
takes the user password and needs permission to run /bin/su
or ALL
like above. Like all sudo
commands, password expected is the user's password, not root
's