1

I'd like to give a particular user the ability to log on as any normal user (e.g. not root or daemon) and execute commands as that user.

How can I do that safely?

Currently my User sudo rights on a bash script like that:

if [ "$1" = "run" ]; then

  sudo -u ${2} ${3};

fi

Which works but, security wasnt a focus.

Thanks guys.

2
  • Do you need access to the other users /home and other dirs or merely a 'run as admin'-ish access? Also which distro is this one as some have extra goodies like selinux that can be used as additional safeties. Jul 15, 2016 at 14:16
  • Debian, I need just run this sudo commans within /home yea
    – Neoon
    Jul 16, 2016 at 10:34

1 Answer 1

1

Make a new script, /usr/bin/runasuser

#!/bin/sh
newuser="${1}" && shift
/usr/bin/sudo su - "${newuser}" -c "$@"

Make it executable

chmod +x /usr/bin/runasuser

Edit sudoers with command visudo and add this line:

alice ALL=(ALL)         /usr/bin/runasuser

This will allow Alice to run anything as Bob if she does this:

sudo /usr/bin/runasuser bob /opt/application/for/bob

Assuming your sudo environment includes /usr/bin by default, you can just run:

sudo runasuser bob /opt/application/for/bob
2
  • Do you even read? Thats basically the same stuff I already got, but not what I basically want.
    – Neoon
    Jul 16, 2016 at 12:24
  • Can you add some basic shell scripting to limit what username is accepted, or would you like that explained?
    – bgStack15
    Jul 16, 2016 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .