Run shell provisioning script as custom user

Hi all,

I would like to run a shell provisioning script as a custom non-privileged user.

If I use something like this:

# Vagrantfile
config.vm.provision "shell", path: "whoami.sh"

# whoami.sh
sudo su - bob
id
whoami

the user does not change to bob, it's root:

default: uid=0(root) gid=0(root) groups=0(root)
default: root

Why it doesn't work? It works fine when I ssh to my box.

I could switch to user bob when I use a script from the sync folder calling it inside of an inline block, but I would like to do it without putting anything into the sync folder.

Any ideas?

1 thought on “Run shell provisioning script as custom user”

  1. sudo su – bob

    starts a new shell interactive shell, but there’s no input, so the shell (the `sudo` command) ends. The parent `whoami.sh` shell then continues.

    You might want to create a separate `bob_provision.sh` and start it from the `whoami.sh` script. And plain `su` will do:

    su – bob -c ./bob_provision.sh

    trying this out in a shell would have given the same result

    Reply

Leave a Comment