Search code examples
vagrantvagrantfilevagrant-provision

Vagrant: How to set the default owner and group permissions


While creating a Ubuntu VM, I am providing a provisioning script to install some of the packages once the guest OS is installed.

config.vm.provision :shell, :path => "vm_provision/provision-node01.sh"

The bash script contains:

#!/usr/bin/env bash

# Intended for Ubuntu 14.04 (Trusty)

# Update the Ubuntu
sudo apt-get -y update
sudo apt-get -y install build-essential

mkdir zookeeper
https://github.com/apache/zookeeper

The VM gets created as well as above shell script commands are also executed fine. However, the default owner and the group permission to the folder 'zookeeper' is both set to the root.

vagrant@ubuntu-14:~$ ls -lrt
total 4
drwxr-xr-x 4 root root 4096 Mar 30 08:55 zookeeper

I want it to be vagrant vagrant so that I do not have change to super user all the time.

There are steps provided for setting the owner/group permission for the shared folder, but that is set via Vagrantfile. Vagrant sync folder creation

In my provisioning shell script, I tried changing the user (like below) before creating the folder and cloning the github repo, but that did not help.

su - vagrant

Solution

  • by default vagrant runs the provisioner as root, you can change this behavior with following option

    config.vm.provision :shell, :path => "vm_provision/provision-node01.sh", privileged: false
    

    when running with privileged: false (see https://www.vagrantup.com/docs/provisioning/shell.html#privileged) the provisioner will be run with vagrant user. In your case this is fine as your script as already sudo for running apt command