Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant: config.vm.provision does not allow me to copy a file to etc/nginx/conf.d?

I am working with a Nginx server. I want to copy a configuration file to /etc/nginx/conf.d with the Vagrantfile. The command I use is:

config.vm.provision "file", source: "./bolt.local.conf", destination: "/etc/nginx/conf.d/bolt.local.conf"

The error I receive is:

Failed to upload a file to the guest VM via SCP due to a permissions
error. This is normally because the SSH user doesn't have permission
to write to the destination location. Alternately, the user running
Vagrant on the host machine may not have permission to read the file.

I am using the bento/ubuntu-16.04 box.

I tried to search for a way to change the permissions for the provision command but I only found ways to change the owner for the config.vm.share_folder command.

Do you know the answer?

like image 964
Hendrik D Avatar asked Jan 07 '19 00:01

Hendrik D


People also ask

What does Vagrant provision do?

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.

How do I transfer files to Vagrant box?

Using SCP. Another way to copy files and folders from host to guest in Vagrant is to use to SCP. Firstly, make sure you have scp installed on your host. If you don't have it, you can install the openssh-clients package if you're using Linux or install Cygwin if you're using Windows.

What is the provision that used in Vagrant by default?

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy , unless the --provision flag is set, as noted above.


1 Answers

As the error message suggest and also from the documentation:

The file uploads by the file provisioner are done as the SSH or PowerShell user. This is important since these users generally do not have elevated privileges on their own. If you want to upload files to locations that require elevated privileges, we recommend uploading them to temporary locations and then using the shell provisioner to move them into place.

So the vagrant user(if not modified) is used to scp the file but you can't access /etc/ with it.

To make it work you need to upload it to a temporary location and then use a shell provisioner to move it to target directory:

config.vm.provision "file", 
  source: "./bolt.local.conf", 
  destination: "/tmp/bolt.local.conf"

config.vm.provision "shell",
  inline: "mv /tmp/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"

This work because the privileged option is true by default on shell provisioners. But it is a bit convoluted to have two provisioners just to copy a configuration file, right ?

Well if the file is already inside your share folder you can just use a shell provisioner to copy it in the nginx directory so you'll end up with something like this:

# This is the default and serve just as a reminder
config.vm.synced_folder ".", "/vagrant"
config.vm.provision "shell",
  inline: "cp /vagrant/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"
like image 119
lee-pai-long Avatar answered Sep 21 '22 10:09

lee-pai-long