0

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
Community
  • 1
  • 1
AnilJ
  • 1,951
  • 2
  • 33
  • 60

1 Answers1

2

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

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • It worked with a minor change: config.vm.provision :shell, :path => "vm_provision/provision-node01.sh", privileged:false – AnilJ Mar 30 '17 at 19:09
  • Little unrelated question: can you tell me how do I change the default user name from 'vagrant' to say 'user1' and also set the password for it? I tried config.ssh.username and config.ssh.password, but I guess I am still missing something. – AnilJ Mar 30 '17 at 19:12
  • thanks, I fixed the quote thing - for the ssh username, you need to have user1 defined. If you build a box with packer, you would have a user creation script in packer where you could change the name. Otherwise you need to first create the user in vagrant and only after then you can change the `config.ssh.username` value – Frederic Henri Mar 30 '17 at 19:21
  • No, I did not use packer myself, but I will find out the way create the user from vagrant itself. Thanks for the pointer. – AnilJ Mar 30 '17 at 19:25
  • Hi Henri, I found this post http://stackoverflow.com/questions/22643177/ssh-onto-vagrant-box-with-different-username. But it appears that we have to create this user explicitly once the VM is created and then (re)provision through Vegrant to be able to do ssh. Have I understood is correctly? There does not seems to be a way to automate this process. – AnilJ Mar 31 '17 at 20:35
  • yes you're right - If you want the user to be available when booting the VM you would need to create the box using packer yourself, you can then change the script to create user and create with your own – Frederic Henri Apr 01 '17 at 05:23